Class: ArelSearch::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/arel_search/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ Builder

Returns a new instance of Builder.



6
7
8
# File 'lib/arel_search/builder.rb', line 6

def initialize table
  @table = table
end

Instance Method Details

#build(expression) ⇒ Object



10
11
12
13
# File 'lib/arel_search/builder.rb', line 10

def build expression
  ast = SQLSearch.parse(expression)
  search_statement(ast)
end

#comparison_statement(comparison) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/arel_search/builder.rb', line 35

def comparison_statement comparison

  unless comparison.left.is_a?(SQLSearch::Atoms::Column)
    raise MalformedQueryError, 'left operand must always be a column'
  end

  case comparison.operator
  when :IN
    @table[comparison.left.name.to_sym].in(comparison.right.values.map(&:value))
  when :">"
    @table[comparison.left.name.to_sym].gt(comparison.right.value)
  when :">="
    @table[comparison.left.name.to_sym].gte(comparison.right.value)
  when :"<"
    @table[comparison.left.name.to_sym].lt(comparison.right.value)
  when :"<="
    @table[comparison.left.name.to_sym].lte(comparison.right.value)
  when :"="
    @table[comparison.left.name.to_sym].eq(comparison.right.value)
  when :"<>"
    @table[comparison.left.name.to_sym].neq(comparison.right.value)
  when :LIKE
    @table[comparison.left.name.to_sym].matches(comparison.right.value)
  else raise
  end
end

#condition_statement(condition) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/arel_search/builder.rb', line 24

def condition_statement condition
  case condition
  when SQLSearch::Conditions::Or
    search_statement(condition.left).or(search_statement(condition.right))
  when SQLSearch::Conditions::And
    search_statement(condition.left).and(search_statement(condition.right))
  when SQLSearch::Conditions::Not
    search_statement(condition.value).not
  end
end

#search_statement(search) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/arel_search/builder.rb', line 15

def search_statement search
  case search
  when SQLSearch::Conditions::Base
    condition_statement(search)
  when SQLSearch::Comparisons::Base
    comparison_statement(search)
  end
end