Class: LUSI::API::Enrolment::EnrolmentLookup
- Inherits:
-
Object
- Object
- LUSI::API::Enrolment::EnrolmentLookup
- Defined in:
- lib/lusi_api/enrolment.rb
Instance Method Summary collapse
- #[](key, *indices) ⇒ Object
-
#add(enrolment = nil, *indices) ⇒ void
Adds an enrolment to specified indices (default is all indices if unspecified) Reminaing positional parameters specify the indices to update.
-
#fetch(key = nil, *indices) ⇒ Array<LUSI::API::Enrolment::EnrolmentBase>?
Searches the specified indices for the lookup key and returns the first match Remaining positional parameters specify the indices to search.
-
#initialize(enrolments = nil, *indices, &block) ⇒ EnrolmentLookup
constructor
Initialises a new EnrolmentLookup instance.
Constructor Details
#initialize(enrolments = nil, *indices, &block) ⇒ EnrolmentLookup
Initialises a new EnrolmentLookup instance
166 167 168 169 170 |
# 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 |
Instance Method Details
#[](key, *indices) ⇒ Object
173 174 175 |
# File 'lib/lusi_api/enrolment.rb', line 173 def [](key, *indices) fetch(key, *indices) end |
#add(enrolment = nil, *indices) ⇒ void
This method returns an undefined value.
Adds an enrolment to specified indices (default is all indices if unspecified) Reminaing positional parameters specify the indices to update
181 182 183 184 185 |
# 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) ⇒ Array<LUSI::API::Enrolment::EnrolmentBase>?
Searches the specified indices for the lookup key and returns the first match 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.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# 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 |