Class: FilterLexer::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/queryfy/filter_lexer/formatter.rb

Instance Method Summary collapse

Instance Method Details

#to_arel(arel_table) ⇒ Object

Converts a FilterLexer::Filter to an arel node



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/queryfy/filter_lexer/formatter.rb', line 5

def to_arel(arel_table)
  # Get the elements we want to operate on
  field = elements[0].text_value
  operator_method = elements[1].to_arel
  val = elements[2].text_value

  # Check if the field we want to filter on exists
  field_index = arel_table.engine.column_names.index(field)

  # Field does not exist, fail
  if field_index.nil?
    raise NoSuchFieldError.new("Unknown field #{ field }", field)
  else
    # Get the arel field name from our input, just to make sure
    # there is nothing weird is in the input
    field = arel_table.engine.column_names[field_index]
  end
  ast_node = arel_table[field.to_sym]

  # Build an arel node from the resolved operator, value and field
  return ast_node.send(operator_method, val)
end