class PIM::Services::LoadRequestType

Constants

DEFAULT_KEYS_PARAM
DEFAULT_PARENT_PARAM

Attributes

convert_to[R]
name[R]
proxy_service_method_name[R]
return_type[R]

Public Class Methods

new(name, keys_param: DEFAULT_KEYS_PARAM, parent_param: DEFAULT_PARENT_PARAM, needs_parent: false, return_type: Hash, convert_to: nil, proxy_service_method_name: nil, proxy_service_args: nil, redirect: nil, redirect_params: nil @name = name.to_s) click to toggle source
# File services.rb, line 186
def initialize name,
    keys_param: DEFAULT_KEYS_PARAM,         # Name of the 'key' parameter
    parent_param: DEFAULT_PARENT_PARAM,     # Name of the 'parent' parameter
    needs_parent: false,                    # Enable, if request needs a 'parent'
    return_type: Hash,                      # Expected return type of the result - Hash for key-value and Array for simply list of values
    convert_to: nil,                        # Set if values should be converted to a specific class
    proxy_service_method_name: nil,         # Name of the method to call in the proxy service
    proxy_service_args: nil,                # Proc to create the list of arguments used for the proxy service call
    redirect: nil,                          # Proc to be executed if the request is a "redirect" request
    redirect_params: nil                    # List of parameters needed for a "redirect" request

  @name = name.to_s
  @keys_param = keys_param
  @needs_parent = needs_parent
  @parent_param = parent_param
  @return_type = return_type
  @convert_to = convert_to
  @proxy_service_method_name = proxy_service_method_name
  @proxy_service_args = proxy_service_args
  @redirect = redirect
  @redirect_params = redirect_params
end

Public Instance Methods

convert_result(result) click to toggle source

Convert the result values of a Hash result, if value conversion was requested

# File services.rb, line 264
def convert_result result
  return result if result.nil? or result.empty? or convert_to.nil? or !PIM.is_hash?(result)
  result.map { |k,v| [k, PIM::Services.__convert(v, to: convert_to)] }.to_h
end
redirect(data_module, *loaders, **redirect_args) click to toggle source

Execute the direct request, if the request is a redirect request. Return nil otherwise.

# File services.rb, line 257
def redirect data_module, *loaders, **redirect_args
  return nil unless redirect?
  return @redirect.call(data_module, *loaders, **redirect_args)
end
redirect?() click to toggle source

Return true, if request is a redirect request

# File services.rb, line 250
def redirect?
  return @redirect.is_a?(Proc)
end
request(**kwargs) click to toggle source

Create new request of this type

# File services.rb, line 211
def request **kwargs
  LoadRequest.new(self, **kwargs)
end
request_args(**kwargs) click to toggle source

Get request arguments from kwargs

# File services.rb, line 217
def request_args **kwargs

  # Get parent from kwargs, if parent is needed
  if @needs_parent
    parent = [*kwargs[@parent_param]].compact.map(&:to_s)
    raise "Missing mandatory 'parent' argument" if parent.empty?
  end

  # Get either redirect args, if request is a redirect request, or keys from kwargs
  if redirect?
    raise "Missing 'redirect_params' argument for redirect" if PIM.is_empty?(@redirect_params)
    redirect_args = kwargs.select { |k,v| @redirect_params.include?(k) }
    missing_args = @redirect_params - redirect_args.keys
    raise "Missing mandatory redirect argument(s) #{missing_args.join(', ')}" unless missing_args.empty?
  else
    keys = [*kwargs[@keys_param]].compact.map(&:to_s)
    raise "Missing mandatory '#{@keys_param}' argument" if keys.empty? and (parent.nil? or parent.empty?)
  end

  # Get proxy args
  proxy_args = []
  if @proxy_service_args.is_a?(Proc)
    proxy_args += @proxy_service_args.call(**kwargs)
  else
    proxy_args += parent unless parent.nil?
    proxy_args += [keys]
  end

  return keys, parent, redirect_args, proxy_args
end