Sha256: 2ee27192c83f3d7aef50841341eedb80e86013c45f8e69b44608db1652e5454e

Contents?: true

Size: 871 Bytes

Versions: 2

Compression:

Stored size: 871 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 && data._proxy.is_a?(LHS::Collection)
          data.first || raise(LHC::NotFound.new('No item was found.', data._request.response))
        elsif data
          data._record.new(data)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lhs-14.0.3 lib/lhs/concerns/record/find_by.rb
lhs-14.0.2 lib/lhs/concerns/record/find_by.rb