Sha256: 7b48e5ac91df257f23fab0a315281cf799389eccfb72fc5234003ea0818db2a4

Contents?: true

Size: 766 Bytes

Versions: 7

Compression:

Stored size: 766 Bytes

Contents

require 'active_support'

class LHS::Record

  module FindBy
    extend ActiveSupport::Concern

    module ClassMethods
      # Fetch some record by parameters
      def find_by(params = {})
        _find_by(params)
      rescue LHC::NotFound
        nil
      end

      # Raise if no record was found
      def find_by!(params = {})
        _find_by(params)
      end

      private

      def _find_by(params)
        params = params.dup.merge(limit: 1)
        data = request(params: params)
        data =
          if data._proxy.is_a?(LHS::Collection)
            data.first || fail(LHC::NotFound.new('No item was found.', data._request.response))
          else
            data
          end
        data._record_class.new(data)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
lhs-3.1.1 lib/lhs/concerns/record/find_by.rb
lhs-3.1.0 lib/lhs/concerns/record/find_by.rb
lhs-3.0.5 lib/lhs/concerns/record/find_by.rb
lhs-3.0.4 lib/lhs/concerns/record/find_by.rb
lhs-3.0.3 lib/lhs/concerns/record/find_by.rb
lhs-3.0.2 lib/lhs/concerns/record/find_by.rb
lhs-3.0.1 lib/lhs/concerns/record/find_by.rb