Class: ArelRest::Parser

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

Defined Under Namespace

Classes: OperatorNotFound

Constant Summary collapse

TEMPLATE_OPERATORS =
{
  "eq" => Predications::EqOperator,
  "not_eq" => Predications::NotEqOperator,
  "gt" => Predications::GtOperator,
  "gteq" => Predications::GteqOperator,
  "lt" => Predications::LtOperator,
  "lteq" => Predications::LteqOperator,
  "in" => Predications::InOperator,
  "not_in" => Predications::NotInOperator,
  "between" => Predications::BetweenOperator,
  "matches" => Predications::MatchesOperator,
  "does_not_match" => Predications::DoesNotMatchOperator,
}

Class Method Summary collapse

Class Method Details

.build_pair_query_string_and_values(q) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/parser.rb', line 48

def self.build_pair_query_string_and_values(q)
  operator = TEMPLATE_OPERATORS[q[:operator].to_s]

  raise(Parser::OperatorNotFound, q[:operator].to_s) unless operator

  operator.process(q)
end

.parse_filter_to_arel(query) ⇒ Object



79
80
81
# File 'lib/parser.rb', line 79

def self.parse_filter_to_arel(query)
  query_builder(query)
end

.query_builder(query) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/parser.rb', line 56

def self.query_builder(query)
  conector = query.keys.detect{|connector| [:or, :and].include?(connector.to_sym)}
  pair_query_string_and_values = query[conector].map do |query_obj|
    if query_obj.keys.map(&:to_sym).any?{|key| [:or,:and].include?(key)}
      nested_query = query_builder(query_obj) # Recursive
      builded = nested_query
    else
      builded = build_pair_query_string_and_values(query_obj)
    end

    builded
  end.inject do |query_composited, query_node|
    if query_composited.nil?
      query_composited = query_node
    else
      query_composited = query_composited.send(conector, query_node)
    end
    query_composited
  end

  pair_query_string_and_values
end