Sha256: 3f2d9e9910ce7920bc30a679fec5d3813c463fcd68320c703923ed062960e75b

Contents?: true

Size: 857 Bytes

Versions: 52

Compression:

Stored size: 857 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).merge(options.fetch(:params, {}))
        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

52 entries across 52 versions & 1 rubygems

Version Path
lhs-7.4.1 lib/lhs/concerns/record/find_by.rb
lhs-7.4.0 lib/lhs/concerns/record/find_by.rb
lhs-7.3.0 lib/lhs/concerns/record/find_by.rb
lhs-7.2.5 lib/lhs/concerns/record/find_by.rb
lhs-7.2.4 lib/lhs/concerns/record/find_by.rb
lhs-7.2.3 lib/lhs/concerns/record/find_by.rb
lhs-7.2.2 lib/lhs/concerns/record/find_by.rb
lhs-7.2.1 lib/lhs/concerns/record/find_by.rb
lhs-7.2.0 lib/lhs/concerns/record/find_by.rb
lhs-7.1.0 lib/lhs/concerns/record/find_by.rb
lhs-7.0.3 lib/lhs/concerns/record/find_by.rb
lhs-7.0.2 lib/lhs/concerns/record/find_by.rb