Class: IdPlease::AssignmentMap

Inherits:
Object
  • Object
show all
Defined in:
lib/id_please/assignment_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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)
  options = args.extract_options!
  options = {:direction => :down, :nested => true, :members_only => false}.merge(options)
  @subjects = options[:subjects] # || raise("You must provide a subject")
  @subjects = [@subjects] unless @subjects.kind_of?(Array)
  @direction = options[:direction]
  @assign_class = options[:assignment_class] # || raise("You must provide the assignment class through :assignment_class")
  @nested = options[:nested] 
  @member_role = options[:member_role] || :member
  @members_only = options[:members_only]
  @assignments = []
  @calculated = false
  add_all_assignments(@subjects)
end

Instance Attribute Details

#calculatedObject (readonly)

Returns the value of attribute calculated.



24
25
26
# File 'lib/id_please/assignment_map.rb', line 24

def calculated
  @calculated
end

#subjectsObject (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.role_authorizable_eq(*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.authorizable }.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

#mapObject



129
130
131
# File 'lib/id_please/assignment_map.rb', line 129

def map
  return self
end

#role_hashObject



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_assignmentsObject



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

#rolesObject



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_hashObject



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

#targetsObject



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_assignmentsObject



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