Sha256: 568ebb2bdab53be97da703c179792266f07a41865d8db4d814d21b6497d28a7f

Contents?: true

Size: 823 Bytes

Versions: 12

Compression:

Stored size: 823 Bytes

Contents

require 'active_support'

class LHS::Record

  module FindBy
    extend ActiveSupport::Concern

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

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

      private

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
lhs-7.0.1 lib/lhs/concerns/record/find_by.rb
lhs-7.0.0 lib/lhs/concerns/record/find_by.rb
lhs-6.8.2 lib/lhs/concerns/record/find_by.rb
lhs-6.8.1 lib/lhs/concerns/record/find_by.rb
lhs-6.8.0 lib/lhs/concerns/record/find_by.rb
lhs-6.7.2 lib/lhs/concerns/record/find_by.rb
lhs-6.7.1 lib/lhs/concerns/record/find_by.rb
lhs-6.7.0 lib/lhs/concerns/record/find_by.rb
lhs-6.6.2 lib/lhs/concerns/record/find_by.rb
lhs-6.6.1 lib/lhs/concerns/record/find_by.rb
lhs-6.6.0 lib/lhs/concerns/record/find_by.rb
lhs-6.5.0 lib/lhs/concerns/record/find_by.rb