Sha256: cfdeb6f22b0992446e915d42399c2963445274421b8f80d63525578b8833ba0e

Contents?: true

Size: 822 Bytes

Versions: 22

Compression:

Stored size: 822 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 || fail(LHC::NotFound.new('No item was found.', data._request.response))
        else
          data._record.new(data)
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
lhs-6.4.0 lib/lhs/concerns/record/find_by.rb
lhs-6.3.1 lib/lhs/concerns/record/find_by.rb
lhs-6.3.0 lib/lhs/concerns/record/find_by.rb
lhs-6.2.0 lib/lhs/concerns/record/find_by.rb
lhs-6.1.0 lib/lhs/concerns/record/find_by.rb
lhs-6.0.0 lib/lhs/concerns/record/find_by.rb
lhs-5.7.1 lib/lhs/concerns/record/find_by.rb
lhs-5.7.0 lib/lhs/concerns/record/find_by.rb
lhs-5.6.6 lib/lhs/concerns/record/find_by.rb
lhs-5.6.5 lib/lhs/concerns/record/find_by.rb
lhs-5.6.4 lib/lhs/concerns/record/find_by.rb
lhs-5.6.3 lib/lhs/concerns/record/find_by.rb
lhs-5.6.2 lib/lhs/concerns/record/find_by.rb
lhs-5.6.1 lib/lhs/concerns/record/find_by.rb
lhs-5.6.0 lib/lhs/concerns/record/find_by.rb
lhs-5.5.0 lib/lhs/concerns/record/find_by.rb
lhs-5.4.2 lib/lhs/concerns/record/find_by.rb
lhs-5.4.1 lib/lhs/concerns/record/find_by.rb
lhs-5.4.0 lib/lhs/concerns/record/find_by.rb
lhs-5.3.0 lib/lhs/concerns/record/find_by.rb