Sha256: 5166c51d05a7016dee46cb12e379452d230417b941ca3d9b79bd13aec4c55a5f

Contents?: true

Size: 892 Bytes

Versions: 3

Compression:

Stored size: 892 Bytes

Contents

require 'active_support'

class LHS::Record

  module Find
    extend ActiveSupport::Concern

    module ClassMethods
      # Find a single uniqe record
      def find(args)
        data =
          if args.is_a? Hash
            find_with_parameters(args)
          else
            find_by_id(args)
          end
        return data unless data._record
        data._record.new(data)
      end

      private

      def find_with_parameters(params)
        data = request(params: params)
        if data._proxy.is_a?(LHS::Collection)
          fail LHC::NotFound.new('Requested unique item. Multiple were found.', data._request.response) if data.count > 1
          data.first || fail(LHC::NotFound.new('No item was found.', data._request.response))
        else
          data
        end
      end

      def find_by_id(id)
        request(params: { id: id })
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lhs-3.4.2 lib/lhs/concerns/record/find.rb
lhs-3.4.1 lib/lhs/concerns/record/find.rb
lhs-3.4.0 lib/lhs/concerns/record/find.rb