class Parelation::Criteria::Where::CriteriaBuilder

Attributes

chain[R]

@return [ActiveRecord::Relation]

value[R]

@return [Hash]

Public Class Methods

new(value, chain) click to toggle source

@param value [Hash] the user-provided criteria @param chain [ActiveRecord::Relation]

# File lib/parelation/criteria/where/criteria_builder.rb, line 14
def initialize(value, chain)
  @value = value
  @chain = chain
end

Public Instance Methods

build() click to toggle source

@return [Hash] criteria that can be passed into

the +where+ method of an ActiveRecord::Relation chain.
# File lib/parelation/criteria/where/criteria_builder.rb, line 22
def build
  value.inject(Hash.new) do |hash, (field, value)|
    values = [value].flatten

    if values.count > 1
      assign_array(hash, field, values)
    else
      assign_value(hash, field, values)
    end

    hash
  end
end

Private Instance Methods

assign_array(hash, field, values) click to toggle source

Assigns each of the provided values to the hash and casts the value to a database-readable value.

@param hash [Hash] @param field [Symbol] @param values [Array]

# File lib/parelation/criteria/where/criteria_builder.rb, line 45
def assign_array(hash, field, values)
  values.each { |val| (hash[field] ||= []) << cast(field, val) }
end
assign_value(hash, field, values) click to toggle source

Assigns the first value of the provided values array to the hash and casts it to a database-readable value.

@param hash [Hash] @param field [Symbol] @param values [Array]

# File lib/parelation/criteria/where/criteria_builder.rb, line 56
def assign_value(hash, field, values)
  hash[field] = cast(field, values[0])
end
cast(field, value) click to toggle source

@param field [Symbol] @param value [String]

# File lib/parelation/criteria/where/criteria_builder.rb, line 63
def cast(field, value)
  Parelation::Criteria::Where::Caster
    .new(field, value, chain.model).cast
end