module DHS::Record::Find::ClassMethods

Public Instance Methods

find(*args) click to toggle source

Find a single uniqe record

# File lib/dhs/concerns/record/find.rb, line 12
def find(*args)
  args, options = process_args(args)
  options = trace!(options)
  raise(DHS::Unprocessable.new, 'Cannot find Record without an ID') if args.blank? && !args.is_a?(Array)
  data =
    if args.present? && args.is_a?(Array)
      find_in_parallel(args, options)
    elsif args.is_a? Hash
      find_with_parameters(args, options)
    else
      find_by_id(args, options)
    end
  return nil if data.nil?
  return data unless data._record
  if data.collection?
    data.map { |record| data._record.new(record.unwrap_nested_item) }
  else
    data._record.new(data.unwrap_nested_item)
  end
end

Private Instance Methods

find_by_id(args, options = {}) click to toggle source
# File lib/dhs/concerns/record/find.rb, line 63
def find_by_id(args, options = {})
  request(request_options(args, options))
end
find_in_parallel(args, options) click to toggle source
# File lib/dhs/concerns/record/find.rb, line 67
def find_in_parallel(args, options)
  options = args.map { |argument| request_options(argument, options) }
  request(options)
end
find_with_parameters(args, options = {}) click to toggle source
# File lib/dhs/concerns/record/find.rb, line 58
def find_with_parameters(args, options = {})
  data = request(request_options(args, options))
  get_unique_item!(data)
end
get_unique_item!(data) click to toggle source
# File lib/dhs/concerns/record/find.rb, line 48
def get_unique_item!(data)
  return if data.nil?
  if data._proxy.is_a?(DHS::Collection)
    raise DHC::NotFound.new('Requested unique item. Multiple were found.', data._request.response) if data.length > 1
    data.first || raise(DHC::NotFound.new('No item was found.', data._request.response))
  else
    data
  end
end
process_args(args) click to toggle source
# File lib/dhs/concerns/record/find.rb, line 35
def process_args(args)
  if args.length == 1
    args = args.first
  elsif args.length == 2 && args.last.is_a?(Hash)
    options = args.pop if args.last.is_a?(Hash)
    args = args.first
  elsif args.last.is_a?(Hash)
    options = args.pop
  end
  options ||= nil
  [args, options]
end
request_options(args, options) click to toggle source
# File lib/dhs/concerns/record/find.rb, line 72
def request_options(args, options)
  options ||= {}
  if args.is_a? Hash
    options.merge(params: args)
  elsif href?(args)
    options.merge(url: args)
  elsif args.present?
    options.merge(params: { id: args })
  else
    options
  end
end