Class: J2119::RoleFinder
- Inherits:
-
Object
- Object
- J2119::RoleFinder
- Defined in:
- lib/j2119/role_finder.rb
Overview
This is about figuring out which roles apply to a node and
potentially to its children in object and array valued fields
Instance Attribute Summary collapse
-
#field_value_roles ⇒ Object
readonly
for debugging.
Instance Method Summary collapse
- #add_child_role(role, field_name, child_role) ⇒ Object
- #add_field_presence_role(role, field_name, new_role) ⇒ Object
- #add_field_value_role(role, field_name, field_value, new_role) ⇒ Object
- #add_grandchild_role(role, field_name, child_role) ⇒ Object
- #add_is_a_role(role, other_role) ⇒ Object
-
#find_child_roles(roles, field_name) ⇒ Object
A node has a role, and one of its fields might be object-valued and that value is given a role.
-
#find_grandchild_roles(roles, field_name) ⇒ Object
A node has a role, and one of its field is an object or an array whose fields or elements are given a role.
-
#find_more_roles(node, roles) ⇒ Object
Consider a node which has one or more roles.
-
#initialize ⇒ RoleFinder
constructor
A new instance of RoleFinder.
Constructor Details
#initialize ⇒ RoleFinder
Returns a new instance of RoleFinder.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/j2119/role_finder.rb', line 24 def initialize # roles of the form: If an object with role X has field Y which # is an object, that object has role R @child_roles = {} # roles of the form: If an object with role X has field Y which # is an object/array, the object-files/array-elements have role R @grandchild_roles = {} # roles of the form: If an object with role X has a field Y with # value Z, it has role R # map[role][field_name][field_val] => child_role @field_value_roles = {} # roles of the form: If an object with role X has a field Y, then # it has role R # map[role][field_name] => child_role @field_presence_roles = {} # roles of the form: A Foo is a Bar @is_a_roles = {} end |
Instance Attribute Details
#field_value_roles ⇒ Object (readonly)
for debugging
22 23 24 |
# File 'lib/j2119/role_finder.rb', line 22 def field_value_roles @field_value_roles end |
Instance Method Details
#add_child_role(role, field_name, child_role) ⇒ Object
65 66 67 68 |
# File 'lib/j2119/role_finder.rb', line 65 def add_child_role(role, field_name, child_role) @child_roles[role] ||= {} @child_roles[role][field_name] = child_role end |
#add_field_presence_role(role, field_name, new_role) ⇒ Object
60 61 62 63 |
# File 'lib/j2119/role_finder.rb', line 60 def add_field_presence_role(role, field_name, new_role) @field_presence_roles[role] ||= {} @field_presence_roles[role][field_name] = new_role end |
#add_field_value_role(role, field_name, field_value, new_role) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/j2119/role_finder.rb', line 52 def add_field_value_role(role, field_name, field_value, new_role) @field_value_roles[role] ||= {} @field_value_roles[role][field_name] ||= {} field_value = Deduce.value(field_value) @field_value_roles[role][field_name][field_value] = new_role end |
#add_grandchild_role(role, field_name, child_role) ⇒ Object
70 71 72 73 |
# File 'lib/j2119/role_finder.rb', line 70 def add_grandchild_role(role, field_name, child_role) @grandchild_roles[role] ||= {} @grandchild_roles[role][field_name] = child_role end |
#add_is_a_role(role, other_role) ⇒ Object
47 48 49 50 |
# File 'lib/j2119/role_finder.rb', line 47 def add_is_a_role(role, other_role) @is_a_roles[role] ||= [] @is_a_roles[role] << other_role end |
#find_child_roles(roles, field_name) ⇒ Object
A node has a role, and one of its fields might be object-valued
and that value is given a role
118 119 120 121 122 123 124 125 126 |
# File 'lib/j2119/role_finder.rb', line 118 def find_child_roles(roles, field_name) newroles = [] roles.each do |role| if @child_roles[role] && @child_roles[role][field_name] newroles << @child_roles[role][field_name] end end newroles end |
#find_grandchild_roles(roles, field_name) ⇒ Object
A node has a role, and one of its field is an object or an
array whose fields or elements are given a role
131 132 133 134 135 136 137 138 139 |
# File 'lib/j2119/role_finder.rb', line 131 def find_grandchild_roles(roles, field_name) newroles = [] roles.each do |role| if @grandchild_roles[role] && @grandchild_roles[role][field_name] newroles << @grandchild_roles[role][field_name] end end newroles end |
#find_more_roles(node, roles) ⇒ Object
Consider a node which has one or more roles. It may have more
roles based on the presence or value of child nodes. This method
addes any such roles to the "roles" list
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/j2119/role_finder.rb', line 79 def find_more_roles(node, roles) # find roles depending on field values roles.each do |role| per_field_name = @field_value_roles[role] if per_field_name per_field_name.each do |field_name, value_roles| value_roles.each do |field_value, child_role| if field_value == node[field_name] roles << child_role end end end end end # find roles depending on field presence roles.each do |role| per_field_name = @field_presence_roles[role] if per_field_name per_field_name.each do |field_name, child_role| if node.key? field_name roles << child_role end end end end # is_a roles roles.each do |role| other_roles = @is_a_roles[role] if other_roles other_roles.each { |o| roles << o } end end end |