Sha256: 47bee2e10091a7d3d432d1f1e306a63fd1fcb8803e5b706a9b905910ea50f79c

Contents?: true

Size: 904 Bytes

Versions: 8

Compression:

Stored size: 904 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_class
        data._record_class.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

8 entries across 8 versions & 1 rubygems

Version Path
lhs-3.3.6 lib/lhs/concerns/record/find.rb
lhs-3.3.5 lib/lhs/concerns/record/find.rb
lhs-3.3.3 lib/lhs/concerns/record/find.rb
lhs-3.3.2 lib/lhs/concerns/record/find.rb
lhs-3.3.1 lib/lhs/concerns/record/find.rb
lhs-3.3.0 lib/lhs/concerns/record/find.rb
lhs-3.2.0 lib/lhs/concerns/record/find.rb
lhs-3.1.3 lib/lhs/concerns/record/find.rb