module NoSE::StatementConditions::ClassMethods

Add methods to the class for populating conditions

Private Instance Methods

build_condition(condition, tree, params) click to toggle source

Construct a condition object from the parse tree @return [void]

# File lib/nose/statements.rb, line 77
def build_condition(condition, tree, params)
  field = add_field_with_prefix tree[:path], condition[:field], params
  Condition.new field, condition[:op].to_sym,
                condition_value(condition, field)
end
condition_value(condition, field) click to toggle source

Get the value of a condition from the parse tree @return [Object]

# File lib/nose/statements.rb, line 85
def condition_value(condition, field)
  value = condition[:value]

  # Convert the value to the correct type
  type = field.class.const_get 'TYPE'
  value = field.class.value_from_string(value.to_s) \
    unless type.nil? || value.nil?

  # Don't allow predicates on foreign keys
  fail InvalidStatementException, 'Predicates cannot use foreign keys' \
    if field.is_a? Fields::ForeignKeyField

  condition.delete :value

  value
end
conditions_from_tree(tree, params) click to toggle source

Extract conditions from a parse tree @return [Hash]

# File lib/nose/statements.rb, line 66
def conditions_from_tree(tree, params)
  conditions = tree[:where].nil? ? [] : tree[:where][:expression]
  conditions = conditions.map { |c| build_condition c, tree, params }

  params[:conditions] = Hash[conditions.map do |condition|
    [condition.field.id, condition]
  end]
end