Class: Entitlements::Data::People::Combined

Inherits:
Object
  • Object
show all
Includes:
Contracts::Core
Defined in:
lib/entitlements.rb,
lib/entitlements/data/people/combined.rb

Constant Summary collapse

C =
::Contracts
PARAMETERS =
{
  "operator"   => { required: true, type: String },
  "components" => { required: true, type: Array },
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Contracts::Core

common, extended, included

Constructor Details

#initialize(operator:, components:) ⇒ Combined

Returns a new instance of Combined.



107
108
109
110
111
112
113
114
# File 'lib/entitlements/data/people/combined.rb', line 107

def initialize(operator:, components:)
  @combined = { operator: operator }
  @combined[:components] = components.map do |component|
    clazz = Entitlements::Data::People.class_for_config(component)
    clazz.new_from_config(component["config"])
  end
  @people = nil
end

Class Method Details

.fingerprint(config) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/entitlements/data/people/combined.rb', line 25

def self.fingerprint(config)
  # Fingerprint of the combined provider is the fingerprint of each constitutent provider. Then serialize
  # to JSON to account for the and/or operator that is part of this configuration. Note: this method might
  # end up being called recursively depending on the complexity of the combined configuration.
  fingerprints = config["components"].map do |component|
    Entitlements::Data::People.class_for_config(component).fingerprint(component.fetch("config"))
  end

  JSON.generate(config.fetch("operator") => fingerprints)
end

.new_from_config(config) ⇒ Object



45
46
47
48
49
50
# File 'lib/entitlements/data/people/combined.rb', line 45

def self.new_from_config(config)
  new(
    operator: config.fetch("operator"),
    components: config.fetch("components")
  )
end

.validate_config!(key, config) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/entitlements/data/people/combined.rb', line 60

def self.validate_config!(key, config)
  text = "Combined people configuration for data source #{key.inspect}"
  Entitlements::Util::Util.validate_attr!(PARAMETERS, config, text)

  unless %w[and or].include?(config["operator"])
    raise ArgumentError, "In #{key}, expected 'operator' to be either 'and' or 'or', not #{config['operator'].inspect}!"
  end

  component_spec = {
    "config" => { required: true, type: Hash },
    "name"   => { required: false, type: String },
    "type"   => { required: true, type: String },
  }

  if config["components"].empty?
    raise ArgumentError, "In #{key}, the array of components cannot be empty!"
  end

  config["components"].each do |component|
    if component.is_a?(Hash)
      component_name = component.fetch("name", component.inspect)
      component_text = "Combined people configuration #{key.inspect} component #{component_name}"
      Entitlements::Util::Util.validate_attr!(component_spec, component, component_text)
      clazz = Entitlements::Data::People.class_for_config(component)
      clazz.validate_config!("#{key}:#{component_name}", component.fetch("config"))
    elsif component.is_a?(String)
      if Entitlements.config.fetch("people", {}).fetch(component, nil)
        resolved_component = Entitlements.config["people"][component]
        clazz = Entitlements::Data::People.class_for_config(resolved_component)
        clazz.validate_config!(component, resolved_component.fetch("config"))
      else
        raise ArgumentError, "In #{key}, reference to invalid component #{component.inspect}!"
      end
    else
      raise ArgumentError, "In #{key}, expected array of hashes/strings but got #{component.inspect}!"
    end
  end

  nil
end

Instance Method Details

#read(uid = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/entitlements/data/people/combined.rb', line 122

def read(uid = nil)
  @people ||= read_entire_hash
  return @people unless uid

  @people_downcase ||= @people.map { |people_uid, _data| [people_uid.downcase, people_uid] }.to_h
  unless @people_downcase.key?(uid.downcase)
    raise Entitlements::Data::People::NoSuchPersonError, "read(#{uid.inspect}) matched no known person"
  end

  @people[@people_downcase[uid.downcase]]
end