Class: Entitlements::Data::People::YAML

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

Constant Summary collapse

C =
::Contracts
PARAMETERS =

Parameters

{
  "filename" => { required: true, type: String }
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Contracts::Core

common, extended, included

Constructor Details

#initialize(filename:, people: nil) ⇒ YAML

Returns a new instance of YAML.



65
66
67
68
69
# File 'lib/entitlements/data/people/yaml.rb', line 65

def initialize(filename:, people: nil)
  @filename = filename
  @people = people
  @people_downcase = nil
end

Class Method Details

.fingerprint(config) ⇒ Object



25
26
27
# File 'lib/entitlements/data/people/yaml.rb', line 25

def self.fingerprint(config)
  PARAMETERS.keys.map { |key| config[key].inspect }.join("||")
end

.new_from_config(config) ⇒ Object



38
39
40
# File 'lib/entitlements/data/people/yaml.rb', line 38

def self.new_from_config(config)
  new(filename: config.fetch("filename"))
end

.validate_config!(key, config) ⇒ Object



51
52
53
54
# File 'lib/entitlements/data/people/yaml.rb', line 51

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

Instance Method Details

#read(uid = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/entitlements/data/people/yaml.rb', line 77

def read(uid = nil)
  @people ||= begin
    Entitlements.logger.debug "Loading people from #{filename.inspect}"

    # :nocov:
    raw_person_data = if RubyVersionCheck.ruby_version2?
                        ::YAML.load(File.read(filename)).to_h
    else
      ::YAML.load(File.read(filename), permitted_classes: [Date]).to_h
    end
    # :nocov:

    raw_person_data.map do |id, data|
      [id, Entitlements::Models::Person.new(uid: id, attributes: data)]
    end.to_h
  end
  return @people if uid.nil?

  # Requested a specific user ID
  @people_downcase ||= @people.map { |person_uid, data| [person_uid.downcase, person_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