Sha256: 337a66d8bf45c40f3c29519bd340778452740345210656799c8401e93582daaf

Contents?: true

Size: 1.01 KB

Versions: 21

Compression:

Stored size: 1.01 KB

Contents

require 'active_support'

class LHS::Record

  module Find
    extend ActiveSupport::Concern

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

      private

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

      def find_by_id(id, options = {})
        options ||= {}
        request(options.merge(params: { id: id }))
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
lhs-6.3.1 lib/lhs/concerns/record/find.rb
lhs-6.3.0 lib/lhs/concerns/record/find.rb
lhs-6.2.0 lib/lhs/concerns/record/find.rb
lhs-6.1.0 lib/lhs/concerns/record/find.rb
lhs-6.0.0 lib/lhs/concerns/record/find.rb
lhs-5.7.1 lib/lhs/concerns/record/find.rb
lhs-5.7.0 lib/lhs/concerns/record/find.rb
lhs-5.6.6 lib/lhs/concerns/record/find.rb
lhs-5.6.5 lib/lhs/concerns/record/find.rb
lhs-5.6.4 lib/lhs/concerns/record/find.rb
lhs-5.6.3 lib/lhs/concerns/record/find.rb
lhs-5.6.2 lib/lhs/concerns/record/find.rb
lhs-5.6.1 lib/lhs/concerns/record/find.rb
lhs-5.6.0 lib/lhs/concerns/record/find.rb
lhs-5.5.0 lib/lhs/concerns/record/find.rb
lhs-5.4.2 lib/lhs/concerns/record/find.rb
lhs-5.4.1 lib/lhs/concerns/record/find.rb
lhs-5.4.0 lib/lhs/concerns/record/find.rb
lhs-5.3.0 lib/lhs/concerns/record/find.rb
lhs-5.2.0 lib/lhs/concerns/record/find.rb