class LUSI::API::Core::Lookup::LUSILookupTable
A caching lookup table which retrieves values from the LUSI
API
Attributes
@!attribute [rw] api
@return [LUSI::API::Core::API, nil] the LUSI API instance to use for lookups
Public Class Methods
Initialises a new LUSILookupTable
instance @param api [LUSI::API::Core::API] the LUSI
API
instance to use @param result_class [Class] the class to instantiate from the LUSI
API
XML
response @param key_attribute [Proc, String, Symbol, nil] an extractor to return the key attribute from the instance
If a Proc is supplied, it will be called with the instance as the sole parameter. If a String or Symbol are supplied, the named attribute will be returned from the instance If nil or no extractor is supplied, the attribute 'identity' will be returned if it exists. @raise [NameError] if the specified attribute cannot be found
@param loadable [Boolean, nil] if true, the lookup table is populated from a single API
call; otherwise
the lookup table is populated by individual API calls each time a key lookup fails
@param param [String, nil] the LUSI
API
parameter to use for the key lookup @param load_params [Hash<String, any>, nil] default LUSI
API
parameters passed to the load call Other positional and keyword parameters are passed to the result class' get_instance method @return [void]
LUSI::API::Core::Lookup::LookupTable::new
# File lib/lusi_api/core/lookup.rb, line 95 def initialize(api = nil, result_class = nil, *args, key_attribute: nil, loadable: nil, param: nil, load_params: nil, **kwargs, &block) super(block) key_attribute = :identity if key_attribute.nil? if key_attribute.is_a? Proc @key_attribute = key_attribute else @key_attribute = Proc.new { |obj| obj.nil? ? nil : obj.instance_variable_get("@#{key_attribute}") } end @api = api @args = args || [] @kwargs = kwargs || {} @load_params = load_params @loadable = loadable ? true : false @param = param @result_class = result_class end
Public Instance Methods
Retrieves the object corresponding to the key from the LUSI
API
and instantiates a result from the XML
@param key [any] the key to search @return [any] an instance of the result_class for the matching object
# File lib/lusi_api/core/lookup.rb, line 118 def get_value(key) # A loadable lookup table is populated with a single API call, so do regular key lookup raise LookupError.new('lookup table is loadable') if @loadable # For non-loadable lookup tables, call the LUSI API to get the key value and create a corresponding instance xml = @api.call(@path, @endpoint, @method, { @param => key }) result_class.new(xml, xml_root) end
Retrieves all objects from the LUSI
API
and adds them to the lookup table @param clear [Boolean, nil] if true, clear the lookup table before loading @param lookup [LUSI::API::Core::Lookup::LookupService] the lookup service for object resolution @return [Boolean] true if the lookup table was loaded, false if it is not loadable
# File lib/lusi_api/core/lookup.rb, line 133 def load(clear = false, lookup = nil, **params) # Bail out if this lookup table isn't loadable return false unless @loadable # Set up the LUSI API call parameters params ||= @load_params || {} # Clear the lookup table before loading if required self.clear if clear # Call the LUSI API # - do not use the lookup service to resolve result_class instances #xml = @api.call(@path, @endpoint, @method, **params) #xml.xpath(@xml_root) do |x| params.merge(@kwargs) @result_class.get_instance(@api, lookup, *@args, use_lookup: false, **params) do |obj| # Get the lookup key from the instance and add the instance to the hash begin key = @key_attribute.call(obj) self[key] = obj rescue NameError => e # ? end end # Return true to indicate successful loading true end