Class: LUSI::API::Core::Lookup::LUSILookupTable

Inherits:
LookupTable
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • api (LUSI::API::Core::API) (defaults to: nil)

    the LUSI API instance to use

  • result_class (Class) (defaults to: nil)

    the class to instantiate from the LUSI API XML response

  • key_attribute (Proc, String, Symbol, nil) (defaults to: 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

  • loadable (Boolean, nil) (defaults to: 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 (String, nil) (defaults to: nil)

    the LUSI API parameter to use for the key lookup

  • load_params (Hash<String, any>, nil) (defaults to: nil)

    default LUSI API parameters passed to the load call



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

#apiLUSI::API::Core::API?

Returns the LUSI API instance to use for lookups.

Returns:



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

Parameters:

  • key (any)

    the key to search

Returns:

  • (any)

    an instance of the result_class for the matching object

Raises:



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

Parameters:

  • clear (Boolean, nil) (defaults to: false)

    if true, clear the lookup table before loading

  • lookup (LUSI::API::Core::Lookup::LookupService) (defaults to: nil)

    the lookup service for object resolution

Returns:

  • (Boolean)

    true if the lookup table was loaded, false if it is not loadable



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