Class: Aspire::UserLookup

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(filename: nil, store: nil) ⇒ void

Initialises a new UserLookup instance

Parameters:

  • filename (String) (defaults to: nil)

    the filename of the CSV file to populate the hash

See Also:

  • Aspire::UserLookup.(Hash(Hash#initialize)


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

Parameters:

  • method (Symbol)

    the method name

  • args (Array)

    the method arguments

  • block (Proc)

    the code block

Returns:

  • (Object)

    the store method result



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

#storeObject

Returns a hash-like object mapping user URIs to their JSON data.

Returns:

  • (Object)

    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

Parameters:

  • uri (String)

    the URI of the user

  • factory (Aspire::Object::Factory) (defaults to: nil)

    the data object factory

Returns:



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

Parameters:

  • filename (String) (defaults to: nil)

    the filename of the 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

Parameters:

  • method (Symbol)

    the method name

  • include_private (Boolean) (defaults to: false)

    if true, include private methods, otherwise include only public methods

Returns:

  • (Boolean)

    true if the store supports the method, false otherwise



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