class Dusen::Syntax

Constants

DEFAULT_UNKNOWN_SCOPER

Public Class Methods

new() click to toggle source
# File lib/dusen/syntax.rb, line 6
def initialize
  @scopers = {}
end

Public Instance Methods

fields() click to toggle source
# File lib/dusen/syntax.rb, line 31
def fields
  @scopers
end
learn_field(field, &scoper) click to toggle source
# File lib/dusen/syntax.rb, line 10
def learn_field(field, &scoper)
  field = field.to_s
  @scopers[field] = scoper
end
learn_unknown_field(&unknown_scoper) click to toggle source
# File lib/dusen/syntax.rb, line 15
def learn_unknown_field(&unknown_scoper)
  @unknown_scoper = unknown_scoper
end
parse(query) click to toggle source
# File lib/dusen/syntax.rb, line 35
def parse(query)
  Parser.parse(query)
end

Private Instance Methods

build_exclude_scope(root_scope, exclude_query) click to toggle source
# File lib/dusen/syntax.rb, line 64
def build_exclude_scope(root_scope, exclude_query)
  root_scope_without_conditions = root_scope.except(:where)
  exclude_scope = find_parsed_query(root_scope_without_conditions, exclude_query)
  exclude_scope_conditions = concatenate_where_values(exclude_scope.where_values)
  if exclude_scope_conditions.present?
    inverted_sql = "NOT COALESCE (" + exclude_scope_conditions + ",0)"
    exclude_scope.except(:where).where(inverted_sql)
  else
    # we cannot build an inverted scope without where-conditions

    root_scope
  end
end
concatenate_where_values(where_values) click to toggle source
# File lib/dusen/syntax.rb, line 78
def concatenate_where_values(where_values)
  if where_values.any?
    if where_values[0].is_a?(String)
      first = where_values.shift
      where = where_values.reduce(first) do |result, value|
        result << " AND " << value
      end
      where
    else
      # where_values are AREL-Nodes
      where = where_values.reduce(:and)
      where.to_sql
    end
  end
end
find_parsed_query(root_scope, query) click to toggle source
# File lib/dusen/syntax.rb, line 55
def find_parsed_query(root_scope, query)
  scope = root_scope
  query.each do |token|
    scoper = @scopers[token.field] || unknown_scoper
    scope = scoper.call(scope, token.value)
  end
  scope
end
unknown_scoper() click to toggle source
# File lib/dusen/syntax.rb, line 51
def unknown_scoper
  @unknown_scoper || DEFAULT_UNKNOWN_SCOPER
end