Sha256: 2fd33abe17e6f587b4a11cb0df31642c17227b7deae6805d32a7f50abbc0befe

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

class Parelation::Applier

  # @return [Array] the list of active criteria classes
  #   that are used to narrow down database results.
  #
  CRITERIA = [
    Parelation::Criteria::Select,
    Parelation::Criteria::Limit,
    Parelation::Criteria::Offset,
    Parelation::Criteria::Order,
    Parelation::Criteria::Query,
    Parelation::Criteria::Where,
  ]

  # @return [ActiveRecord::Relation]
  #
  attr_reader :chain

  # @return [ActionController::Parameters, Hash]
  #
  attr_reader :params

  # @param chain [ActionController::Relation] the base chain to build on.
  # @param params [ActionController::Parameters, Hash] user input via params.
  #
  def initialize(chain, params)
    @chain = chain
    @params = params
  end

  # @return [ActiveRecord::Relation] the criteria-applied {#chain}.
  #
  def apply
    @apply ||= apply_to_chain
  end

  private

  # Iterates over each user-provided parameter and incrementally
  # updates the {#chain} to incorporate the user-requested criteria.
  #
  # @return [ActiveRecord::Relation]
  #
  def apply_to_chain
    params.each do |param, value|
      CRITERIA.each do |criteria|
        if criteria.match?(param)
          begin
            @chain = criteria.new(chain, param, value).call
          rescue
            raise Parelation::Errors::Parameter,
              "the #{param} parameter is invalid"
          end
        end
      end
    end

    chain
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
parelation-1.0.0 lib/parelation/applier.rb
parelation-0.2.0 lib/parelation/applier.rb