class LUSI::API::Enrolment::EnrolmentLookup

Public Class Methods

new(enrolments = nil, *indices, &block) click to toggle source

Initialises a new EnrolmentLookup instance

# File lib/lusi_api/enrolment.rb, line 166
def initialize(enrolments = nil, *indices, &block)
  @default_proc = block
  @indices = {}
  enrolments.each { |e| add(e, *indices) }
end

Public Instance Methods

[](key, *indices) click to toggle source

@see (LUSI::API::Enrolment::EnrolmentLookup#fetch)

# File lib/lusi_api/enrolment.rb, line 173
def [](key, *indices)
  fetch(key, *indices)
end
add(enrolment = nil, *indices) click to toggle source

Adds an enrolment to specified indices (default is all indices if unspecified) @param enrolment [LUSI::API::Enrolment::EnrolmentBase] the enrolment to add Reminaing positional parameters specify the indices to update @return [void]

# File lib/lusi_api/enrolment.rb, line 181
def add(enrolment = nil, *indices)
  indices = enrolment.lookup_indices if indices.nil? || indices.empty?
  indices.each { |index| add_enrolment(enrolment, index) }
  nil
end
fetch(key = nil, *indices) click to toggle source

Searches the specified indices for the lookup key and returns the first match @param key [Object] the lookup key. If the key defines method enrolment_lookup_keys, this method determines

which keys are searched for.

Remaining positional parameters specify the indices to search. If no indices are specified, but the key instance defines method enrolment_lookup_indices, this method determines which indices are searched. @return [Array<LUSI::API::Enrolment::EnrolmentBase>, nil] the enrolments corresponding to key, or nil

if match was found
# File lib/lusi_api/enrolment.rb, line 194
def fetch(key = nil, *indices)

  # If no index is specified, infer it from the key type if possible
  if indices.nil? || indices.empty?
    indices = key.respond_to?(:enrolment_lookup_indices) ? key.enrolment_lookup_indices : nil
  end
  return nil if indices.nil? || indices.empty?

  # Use the lookup keys specified by the key instance if possible, otherwise use the literal key value
  keys = key.respond_to?(:enrolment_lookup_keys) ? key.enrolment_lookup_keys : [key]

  # Search the specified indices until a match is found, then return matches for all key values from this index
  unless keys.nil? || keys.empty?
    result = []
    indices.each do |index|
      i = @indices[index]
      if i
        keys.each { |key| result += i[key] || [] }
      end
      return result unless result.empty?
    end
  end

  # If we get here, the search failed in all indices - call the default_proc if available, otherwise return nil
  @default_proc ? @default_proc.call(key) : nil

end

Protected Instance Methods

add_enrolment(enrolment, index) click to toggle source

Adds an enrolment to the specified index @param enrolment [LUSI::API::Enrolment::EnrolmentBase] the enrolment @param index [Symbol] the index to be updated

# File lib/lusi_api/enrolment.rb, line 227
def add_enrolment(enrolment, index)
  # Get the specified index hash
  @indices[index] = {} unless @indices.include?(index)
  hash = @indices[index]
  # Add the enrolment to the list
  key = enrolment.lookup_key(index)
  if hash.include?(key)
    hash[key].push(enrolment)
  else
    hash[key] = [enrolment]
  end
  # Return the list containing the enrolment
  hash[key]
end