Sha256: 330122d21d05a7d30e7e2372290c4d3e8ff658709fb25d0c3808a0305f372f7f

Contents?: true

Size: 710 Bytes

Versions: 5

Compression:

Stored size: 710 Bytes

Contents

require 'active_support'

class LHS::Service

  module FindBy
    extend ActiveSupport::Concern

    module ClassMethods

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

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

      private

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lhs-2.2.2 lib/lhs/concerns/service/find_by.rb
lhs-2.2.1 lib/lhs/concerns/service/find_by.rb
lhs-2.2.0 lib/lhs/concerns/service/find_by.rb
lhs-2.1.1 lib/lhs/concerns/service/find_by.rb
lhs-2.1.0 lib/lhs/concerns/service/find_by.rb