Sha256: 1b2ceaa02d66c9176c68b4c46558a4ebc2d631b6ca0a5eeab7e749a8666af494
Contents?: true
Size: 1.06 KB
Versions: 62
Compression:
Stored size: 1.06 KB
Contents
# frozen_string_literal: true 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, trace!(options)) rescue LHC::NotFound nil end # Raise if no record was found def find_by!(params = {}, options = nil) _find_by(params, trace!(options)) end private def _find_by(params, options = {}) raise(LHS::Unprocessable.new, 'Cannot find Record without an ID') if params.any? && params.all? { |_, value| value.blank? } options ||= {} params = params.dup.merge(limit_key(:parameter) => 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.unwrap_nested_item) end end end end end
Version data entries
62 entries across 62 versions & 1 rubygems