Class: Entitlements::Extras::Orgchart::Logic

Inherits:
Object
  • Object
show all
Includes:
Contracts::Core
Defined in:
lib/entitlements/extras/orgchart/logic.rb

Constant Summary collapse

C =
::Contracts

Instance Method Summary collapse

Constructor Details

#initialize(people:) ⇒ Logic

Returns a new instance of Logic.



18
19
20
21
22
# File 'lib/entitlements/extras/orgchart/logic.rb', line 18

def initialize(people:)
  @people_hash = people.map { |uid, person| [uid.downcase, person] }.to_h
  @direct_reports_cache = nil
  @all_reports_cache = nil
end

Instance Method Details

#all_reports(manager) ⇒ Object



45
46
47
48
# File 'lib/entitlements/extras/orgchart/logic.rb', line 45

def all_reports(manager)
  manager_uid = manager.uid.downcase
  all_reports_cache.key?(manager_uid) ? all_reports_cache[manager_uid] : Set.new
end

#direct_reports(manager) ⇒ Object



32
33
34
35
# File 'lib/entitlements/extras/orgchart/logic.rb', line 32

def direct_reports(manager)
  manager_uid = manager.uid.downcase
  direct_reports_cache.key?(manager_uid) ? direct_reports_cache[manager_uid] : Set.new
end

#management_chain(person) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/entitlements/extras/orgchart/logic.rb', line 57

def management_chain(person)
  person_uid = person.uid.downcase

  @management_chain_cache ||= {}
  return @management_chain_cache[person_uid] if @management_chain_cache[person_uid].is_a?(Set)

  @management_chain_cache[person_uid] = Set.new
  if person.manager && person.manager != person.uid
    person_manager_uid = person.manager.downcase
    unless @people_hash.key?(person_manager_uid)
      # :nocov:
      raise ArgumentError, "Manager #{person.manager.inspect} for person #{person.uid.inspect} does not exist!"
      # :nocov:
    end
    # The recursive logic here will also define the management_chain_cache value for the manager,
    # and when calculating that it will define the management_chain_cache value for that manager's manager,
    # and so on. This ensures that each person's manager is only computed one time (when it's used) and
    # subsequent lookups are all faster.
    @management_chain_cache[person_uid].add @people_hash[person_manager_uid]
    @management_chain_cache[person_uid].merge management_chain(@people_hash[person_manager_uid])
  end
  @management_chain_cache[person_uid]
end