class ArelSearch::Builder

Public Class Methods

new(table) click to toggle source
# File lib/arel_search/builder.rb, line 6
def initialize table
  @table = table
end

Public Instance Methods

build(expression) click to toggle source
# File lib/arel_search/builder.rb, line 10
def build expression
  ast = SQLSearch.parse(expression)
  search_statement(ast)
end
comparison_statement(comparison) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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