Class: Aspire::UserLookup
- Inherits:
-
Object
- Object
- Aspire::UserLookup
- Defined in:
- lib/aspire/user_lookup.rb
Overview
Implements a hash of User instances indexed by URI The hash can be populated from an Aspire All User Profiles report CSV file
Instance Attribute Summary collapse
-
#store ⇒ Object
A hash-like object mapping user URIs to their JSON data.
Instance Method Summary collapse
-
#[](uri, factory = nil) ⇒ Aspire::Object::User
Returns an Aspire::Object::User instance for a URI.
-
#initialize(filename: nil, store: nil) ⇒ void
constructor
Initialises a new UserLookup instance.
-
#load(filename = nil) ⇒ void
Populates the store from an All User Profiles report CSV file.
-
#method_missing(method, *args, &block) ⇒ Object
Proxies missing methods to the store.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Proxies missing method respond_to? to the store.
Constructor Details
#initialize(filename: nil, store: nil) ⇒ void
Initialises a new UserLookup instance
18 19 20 21 |
# File 'lib/aspire/user_lookup.rb', line 18 def initialize(filename: nil, store: nil) self.store = store || {} load(filename) if filename end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Proxies missing methods to the store
53 54 55 56 |
# File 'lib/aspire/user_lookup.rb', line 53 def method_missing(method, *args, &block) super unless store.respond_to?(method) store.public_send(method, *args, &block) end |
Instance Attribute Details
#store ⇒ Object
Returns a hash-like object mapping user URIs to their JSON data.
12 13 14 |
# File 'lib/aspire/user_lookup.rb', line 12 def store @store end |
Instance Method Details
#[](uri, factory = nil) ⇒ Aspire::Object::User
Returns an Aspire::Object::User instance for a URI
27 28 29 30 |
# File 'lib/aspire/user_lookup.rb', line 27 def [](uri, factory = nil) data = store[uri] data.nil? ? nil : Aspire::Object::User.new(uri, factory, json: data) end |
#load(filename = nil) ⇒ void
This method returns an undefined value.
Populates the store from an All User Profiles report CSV file
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/aspire/user_lookup.rb', line 35 def load(filename = nil) delim = /\s*;\s*/ # The delimiter for email and role lists enum = Aspire::Enumerator::ReportEnumerator.new(filename).enumerator enum.each do |row| # Construct a JSON data structure for the user uri = row[3] data = csv_to_json_api(row, email_delim: delim, role_delim: delim) csv_to_json_other(row, data) # Store the JSON data in the lookup table store[uri] = data end end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Proxies missing method respond_to? to the store
63 64 65 |
# File 'lib/aspire/user_lookup.rb', line 63 def respond_to_missing?(method, include_private = false) store.respond_to?(method, include_private) end |