Class: IdPlease::AssignmentMap
- Inherits:
-
Object
- Object
- IdPlease::AssignmentMap
- Defined in:
- lib/id_please/assignment_map.rb
Instance Attribute Summary collapse
-
#calculated ⇒ Object
readonly
Returns the value of attribute calculated.
-
#subjects ⇒ Object
readonly
Returns the value of attribute subjects.
Instance Method Summary collapse
- #add_all_assignments(subjects) ⇒ Object
-
#initialize(*args) ⇒ AssignmentMap
constructor
A new instance of AssignmentMap.
- #map ⇒ Object
- #role_hash ⇒ Object
- #role_with_assignments ⇒ Object
- #roles ⇒ Object
- #subject_hash ⇒ Object
- #targets ⇒ Object
- #targets_with_assignments ⇒ Object
Constructor Details
#initialize(*args) ⇒ AssignmentMap
Returns a new instance of AssignmentMap.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/id_please/assignment_map.rb', line 26 def initialize(*args) = args. = {:direction => :down, :nested => true, :members_only => false}.merge() @subjects = [:subjects] # || raise("You must provide a subject") @subjects = [@subjects] unless @subjects.kind_of?(Array) @direction = [:direction] @assign_class = [:assignment_class] # || raise("You must provide the assignment class through :assignment_class") @nested = [:nested] @member_role = [:member_role] || :member @members_only = [:members_only] @assignments = [] @calculated = false add_all_assignments(@subjects) end |
Instance Attribute Details
#calculated ⇒ Object (readonly)
Returns the value of attribute calculated.
24 25 26 |
# File 'lib/id_please/assignment_map.rb', line 24 def calculated @calculated end |
#subjects ⇒ Object (readonly)
Returns the value of attribute subjects.
24 25 26 |
# File 'lib/id_please/assignment_map.rb', line 24 def subjects @subjects end |
Instance Method Details
#add_all_assignments(subjects) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/id_please/assignment_map.rb', line 41 def add_all_assignments(subjects) @calculated = false search_proc = lambda { @direction == :down ? @assign_class.(*subjects) : @assign_class.subject_eq(*subjects) } search_with_role = lambda { |a| @members_only ? a.role_name_eq(@member_role.to_s) : a} assignments = search_with_role.call(search_proc.call).all(:include => [:subject, {:role => :authorizable}]) @assignments += assignments if @nested subjects_to_search = if @direction == :down assignments.select { |a| a.role.name.to_sym == @member_role}.collect(&:subject).select { |s| s._auth_is_group } else assignments.select { |a| a.role.name.to_sym == @member_role}.collect { |a| a.role. }.select { |s| s.respond_to?(:_auth_is_group) && s._auth_is_group } end add_all_assignments(subjects_to_search) unless subjects_to_search.empty? end end |
#map ⇒ Object
129 130 131 |
# File 'lib/id_please/assignment_map.rb', line 129 def map return self end |
#role_hash ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/id_please/assignment_map.rb', line 85 def role_hash calculate! unless @calculated result = {} @assignment_links.each do |a| result[a.role] ||= [] result[a.role] << a.subject end result.each_pair { |k,v| result[k] = v.uniq } return result end |
#role_with_assignments ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/id_please/assignment_map.rb', line 98 def role_with_assignments calculate! unless @calculated result = Hash.new { |h,k| h[k] = Hash.new() } @assignment_links.each do |a| result[a.role][a.subject] ||= [] result[a.role][a.subject] << a.target end result.each_key { |name| result[name].each_pair { |subject,v| result[name][subject] = v.uniq }} return result end |
#roles ⇒ Object
80 81 82 83 |
# File 'lib/id_please/assignment_map.rb', line 80 def roles calculate! unless @calculated @roles = @assignment_links.collect(&:role).uniq end |
#subject_hash ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/id_please/assignment_map.rb', line 68 def subject_hash calculate! unless @calculated result = {} @assignment_links.each do |a| result[a.subject] ||= [] result[a.subject] << a.role end result.each_pair { |k,v| result[k] = v.uniq } return result end |
#targets ⇒ Object
110 111 112 113 |
# File 'lib/id_please/assignment_map.rb', line 110 def targets calculate! unless @calculated @targets = @assignment_links.collect(&:target).uniq end |
#targets_with_assignments ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/id_please/assignment_map.rb', line 116 def targets_with_assignments calculate! unless @calculated result = Hash.new { |h,k| h[k] = Hash.new() } @assignment_links.each do |a| result[a.role][a.target] ||= [] result[a.role][a.target] << (a.via || a.subject) end result.each_key { |name| result[name].each_pair { |target,v| result[name][target] = v.compact.uniq }} return result end |