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
# File 'lib/id_please/assignment_map.rb', line 26

def initialize(*args)
  options = args.extract_options!
  options = {:direction => :down, :nested => true}.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
  @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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/id_please/assignment_map.rb', line 40

def add_all_assignments(subjects)
  
  @calculated = false
  if @direction == :down
    assignments = @assign_class.role_authorizable_eq(*subjects).all(:include => [:subject, {:role => :authorizable}])

    @assignments += assignments
    
    if @nested
      subjects_to_search = assignments.collect(&:subject).select { |s| s._auth_is_group }
      
      add_all_assignments(subjects_to_search) unless subjects_to_search.empty?
    end
  end
end

#mapObject



103
104
105
# File 'lib/id_please/assignment_map.rb', line 103

def map
  return self
end

#role_hashObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/id_please/assignment_map.rb', line 78

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



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/id_please/assignment_map.rb', line 91

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



73
74
75
76
# File 'lib/id_please/assignment_map.rb', line 73

def roles
  calculate! unless @calculated
  @roles = @assignment_links.collect(&:role).uniq
end

#subject_hashObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/id_please/assignment_map.rb', line 61

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