class Sanction::Node

Attributes

id[R]
type[R]

Public Class Methods

new(hash, parent=nil) click to toggle source
# File lib/sanction/node.rb, line 7
def initialize(hash, parent=nil)
  @parent = parent
  process_hash(hash)
end

Public Instance Methods

[](key) click to toggle source
# File lib/sanction/node.rb, line 60
def [](key)
  klass = subjects.select {|x| x.type?(key) }.any? ? array_class : null_array_class
  klass.new(subjects.select {|x| x.type?(key) }).tap do |x| 
    x.key = key
    x.parent = self
  end
end
add_scope(attribute) click to toggle source
# File lib/sanction/node.rb, line 97
def add_scope(attribute)
  clone_scope! if @scope.blank?
  @scope << attribute.to_sym
  sanitize_scope!
end
add_subject(hash) click to toggle source
# File lib/sanction/node.rb, line 46
def add_subject(hash)
  mode_class = hash[:mode] || mode
  children << "sanction/#{mode_class}/node".classify.constantize.new(hash, self)
end
array_class() click to toggle source

Virtual

# File lib/sanction/node.rb, line 52
def array_class
  raise NotImplementedError
end
change_type!(type) click to toggle source

Returns the new graph with the switched mode

# File lib/sanction/node.rb, line 33
def change_type! type
  hash = to_hash
  klass = "sanction/#{type}/node".classify.constantize
  if root?
    klass.new(hash)
  else
    node = klass.new(hash, parent)
    parent.children << node
    unlink
    node.root
  end
end
children() click to toggle source
# File lib/sanction/node.rb, line 128
def children
  @children ||= array_class.new
end
Also aliased as: subjects
children?() click to toggle source
# File lib/sanction/node.rb, line 120
def children?
  children.any?
end
find(type, id) click to toggle source
# File lib/sanction/node.rb, line 68
def find(type, id)
  out = root
  walk do |child|
    out = child if (child.type?(type) && child.id?(id))
  end
  out
end
has_scope?(scope_symbol) click to toggle source
# File lib/sanction/node.rb, line 76
def has_scope? scope_symbol
  scope.include? scope_symbol.to_sym
end
id?(id) click to toggle source
# File lib/sanction/node.rb, line 84
def id?(id)
  @id == id
end
mode() click to toggle source
# File lib/sanction/node.rb, line 116
def mode
  raise NotImplementedError
end
null_array_class() click to toggle source
# File lib/sanction/node.rb, line 56
def null_array_class
  raise NotImplementedError
end
permitted?() click to toggle source
# File lib/sanction/node.rb, line 12
def permitted?
  return false if wildcarded? && @parent.blacklist?
  return true  if wildcarded? && @parent.whitelist?
end
persisted?() click to toggle source
# File lib/sanction/node.rb, line 28
def persisted?
  true
end
remove_scope(attribute) click to toggle source
# File lib/sanction/node.rb, line 103
def remove_scope(attribute)
  @scope.reject! {|x| x == attribute.to_sym }
end
resources() click to toggle source
# File lib/sanction/node.rb, line 107
def resources
  return [] if (@resources.blank? && root?)
  @resources.blank? ? parent.resources : @resources
end
resources=(resource) click to toggle source
# File lib/sanction/node.rb, line 112
def resources=(resource)
  @resources = [resource].flatten.compact.map(&:to_sym)
end
scope() click to toggle source
# File lib/sanction/node.rb, line 88
def scope
  return @scope if (parent.blank? && root?)
  @scope.blank? ? parent.scope : @scope
end
scope=(attribute) click to toggle source
# File lib/sanction/node.rb, line 93
def scope=(attribute)
  @scope = [attribute].flatten.compact.map(&:to_sym)
end
subjects()
Alias for: children
to_hash() click to toggle source
# File lib/sanction/node.rb, line 17
def to_hash
  {
    id: @id,
    type: @type,
    mode: mode,
    scope: @scope,
    subjects: subjects.map {|x| x.to_hash},
    resources: @resources
  }.reject { |k, v| v.blank? }
end
type?(type) click to toggle source
# File lib/sanction/node.rb, line 80
def type?(type)
  @type == type.to_sym
end
wildcarded?() click to toggle source
# File lib/sanction/node.rb, line 124
def wildcarded?
  @id == '*'
end

Private Instance Methods

clone_scope!() click to toggle source
# File lib/sanction/node.rb, line 136
def clone_scope!
  @scope = parent.scope.dup
end
process_hash(hash) click to toggle source
# File lib/sanction/node.rb, line 144
def process_hash(hash)
  @id         = hash[:id]
  @scope      = [hash[:scope]].flatten.compact.map(&:to_sym) 
  @type       = hash[:type].to_sym if hash[:type]
  @resources  = []
  @resources  += hash[:resources].map(&:to_sym) unless hash[:resources].blank?
  hash[:subjects].each { |subject| add_subject(subject) } unless hash[:subjects].blank?
end
sanitize_scope!() click to toggle source
# File lib/sanction/node.rb, line 140
def sanitize_scope!
  @scope = @scope.flatten.compact.uniq.map(&:to_sym)
end