Class: LUSI::API::Core::Lookup::LUSILookupTable
- Inherits:
-
LookupTable
- Object
- Hash
- LookupTable
- LUSI::API::Core::Lookup::LUSILookupTable
- Defined in:
- lib/lusi_api/core/lookup.rb,
lib/lusi_api/core/lookup.rb
Overview
A caching lookup table which retrieves values from the LUSI API
Instance Attribute Summary collapse
-
#api ⇒ LUSI::API::Core::API?
The LUSI API instance to use for lookups.
Instance Method Summary collapse
-
#get_value(key) ⇒ any
Retrieves the object corresponding to the key from the LUSI API and instantiates a result from the XML.
-
#initialize(api = nil, result_class = nil, *args, key_attribute: nil, loadable: nil, param: nil, load_params: nil, **kwargs, &block) ⇒ void
constructor
Initialises a new LUSILookupTable instance Other positional and keyword parameters are passed to the result class’ #get_instance method.
-
#load(clear = false, lookup = nil, **params) ⇒ Boolean
Retrieves all objects from the LUSI API and adds them to the lookup table.
Constructor Details
#initialize(api = nil, result_class = nil, *args, key_attribute: nil, loadable: nil, param: nil, load_params: nil, **kwargs, &block) ⇒ void
Initialises a new LUSILookupTable instance Other positional and keyword parameters are passed to the result class’ #get_instance method
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# 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 |
Instance Attribute Details
#api ⇒ LUSI::API::Core::API?
Returns the LUSI API instance to use for lookups.
79 80 81 |
# File 'lib/lusi_api/core/lookup.rb', line 79 def api @api end |
Instance Method Details
#get_value(key) ⇒ any
Retrieves the object corresponding to the key from the LUSI API and instantiates a result from the XML
118 119 120 121 122 123 124 125 126 127 |
# 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 |
#load(clear = false, lookup = nil, **params) ⇒ Boolean
Retrieves all objects from the LUSI API and adds them to the lookup table
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# 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 |