Class: Sanction::Node

Inherits:
Object
  • Object
show all
Includes:
Tree
Defined in:
lib/sanction/node.rb

Direct Known Subclasses

Blacklist::Node, Whitelist::Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tree

#ancestors, #depth, #descendants, #graft, #has_children?, #height, #leaf?, #only_child?, #parent, #prune, #root, #root?, #siblings, #unlink, #walk

Constructor Details

#initialize(hash, parent = nil) ⇒ Node

Returns a new instance of Node.



7
8
9
10
# File 'lib/sanction/node.rb', line 7

def initialize(hash, parent=nil)
  @parent = parent
  process_hash(hash)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/sanction/node.rb', line 5

def id
  @id
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/sanction/node.rb', line 5

def type
  @type
end

Instance Method Details

#[](key) ⇒ Object



60
61
62
63
64
65
66
# 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) ⇒ Object



97
98
99
100
101
# 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) ⇒ Object



46
47
48
49
# 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_classObject

Virtual

Raises:

  • (NotImplementedError)


52
53
54
# File 'lib/sanction/node.rb', line 52

def array_class
  raise NotImplementedError
end

#change_type!(type) ⇒ Object

Returns the new graph with the switched mode



33
34
35
36
37
38
39
40
41
42
43
44
# 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

#childrenObject Also known as: subjects



128
129
130
# File 'lib/sanction/node.rb', line 128

def children
  @children ||= array_class.new
end

#children?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/sanction/node.rb', line 120

def children?
  children.any?
end

#find(type, id) ⇒ Object



68
69
70
71
72
73
74
# 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) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/sanction/node.rb', line 76

def has_scope? scope_symbol
  scope.include? scope_symbol.to_sym
end

#id?(id) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/sanction/node.rb', line 84

def id?(id)
  @id == id
end

#modeObject

Raises:

  • (NotImplementedError)


116
117
118
# File 'lib/sanction/node.rb', line 116

def mode
  raise NotImplementedError
end

#null_array_classObject

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/sanction/node.rb', line 56

def null_array_class
  raise NotImplementedError
end

#permitted?Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/sanction/node.rb', line 12

def permitted?
  return false if wildcarded? && @parent.blacklist?
  return true  if wildcarded? && @parent.whitelist?
end

#persisted?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/sanction/node.rb', line 28

def persisted?
  true
end

#remove_scope(attribute) ⇒ Object



103
104
105
# File 'lib/sanction/node.rb', line 103

def remove_scope(attribute)
  @scope.reject! {|x| x == attribute.to_sym }
end

#resourcesObject



107
108
109
110
# File 'lib/sanction/node.rb', line 107

def resources
  return [] if (@resources.blank? && root?)
  @resources.blank? ? parent.resources : @resources
end

#resources=(resource) ⇒ Object



112
113
114
# File 'lib/sanction/node.rb', line 112

def resources=(resource)
  @resources = [resource].flatten.compact.map(&:to_sym)
end

#scopeObject



88
89
90
91
# File 'lib/sanction/node.rb', line 88

def scope
  return @scope if (parent.blank? && root?)
  @scope.blank? ? parent.scope : @scope
end

#scope=(attribute) ⇒ Object



93
94
95
# File 'lib/sanction/node.rb', line 93

def scope=(attribute)
  @scope = [attribute].flatten.compact.map(&:to_sym)
end

#to_hashObject



17
18
19
20
21
22
23
24
25
26
# 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) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/sanction/node.rb', line 80

def type?(type)
  @type == type.to_sym
end

#wildcarded?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/sanction/node.rb', line 124

def wildcarded?
  @id == '*'
end