class LUSI::API::Core::Lookup::LookupTable

Implements a caching lookup table

Public Class Methods

new(*args, &block) click to toggle source

Initialises a new LookupTable instance @param (see Hash) @return [void]

Calls superclass method
# 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

Public Instance Methods

get_value(key) click to toggle source

Retrieves the value for a key from some data source @param key [any] the key to search for @return [any] the corresponding value @raise [LookupError] if the data source lookup fails

# File lib/lusi_api/core/lookup.rb, line 67
def get_value(key)
  raise LookupError.new(key)
end