Class: LUSI::API::Core::Lookup::LookupTable
- Inherits:
-
Hash
- Object
- Hash
- LUSI::API::Core::Lookup::LookupTable
- Defined in:
- lib/lusi_api/core/lookup.rb
Overview
Implements a caching lookup table
Direct Known Subclasses
Instance Method Summary collapse
-
#get_value(key) ⇒ any
Retrieves the value for a key from some data source.
-
#initialize(*args, &block) ⇒ void
constructor
Initialises a new LookupTable instance.
Constructor Details
#initialize(*args, &block) ⇒ void
Initialises a new LookupTable instance
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/lusi_api/core/lookup.rb', line 36 def initialize(*args, &block) # Initialise the superclass super(*args) # Define a default proc to look up and cache missing keys # Note that if a hash has a default value set, this is lost (reset to nil) when a default_proc is set, # so we preserve the default value here to use in the proc. default_value = default self.default_proc = Proc.new do |hash, key| begin # If the lookup succeeds, add the value to the hash and return the value. self[key] = get_value(key) rescue LookupError => e # Lookup failed if block # Call the block passed by the caller block.call(hash, key) else # Return the default value for the hash default_value end end end end |
Instance Method Details
#get_value(key) ⇒ any
Retrieves the value for a key from some data source
67 68 69 |
# File 'lib/lusi_api/core/lookup.rb', line 67 def get_value(key) raise LookupError.new(key) end |