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-14.0.1 lib/lhs/concerns/record/find_by.rb
lhs-14.0.0 lib/lhs/concerns/record/find_by.rb
lhs-13.2.3 lib/lhs/concerns/record/find_by.rb
lhs-13.2.2 lib/lhs/concerns/record/find_by.rb
lhs-13.2.1 lib/lhs/concerns/record/find_by.rb
lhs-13.2.0 lib/lhs/concerns/record/find_by.rb
lhs-13.1.0 lib/lhs/concerns/record/find_by.rb
lhs-13.0.1 lib/lhs/concerns/record/find_by.rb
lhs-13.0.0 lib/lhs/concerns/record/find_by.rb
lhs-12.3.0 lib/lhs/concerns/record/find_by.rb
lhs-12.2.1 lib/lhs/concerns/record/find_by.rb
lhs-12.2.0 lib/lhs/concerns/record/find_by.rb
lhs-12.1.0 lib/lhs/concerns/record/find_by.rb
lhs-12.0.3 lib/lhs/concerns/record/find_by.rb
lhs-12.0.2 lib/lhs/concerns/record/find_by.rb
lhs-12.0.1 lib/lhs/concerns/record/find_by.rb
lhs-12.0.0 lib/lhs/concerns/record/find_by.rb
lhs-11.3.3 lib/lhs/concerns/record/find_by.rb
lhs-11.3.2 lib/lhs/concerns/record/find_by.rb
lhs-11.3.1 lib/lhs/concerns/record/find_by.rb