Class: HashConditions::Parser

Inherits:
Object
  • Object
show all
Extended by:
Core
Defined in:
lib/hash_conditions/parser.rb

Constant Summary

Constants included from Core

Core::ARITMETIC_OPERATORS, Core::DEBUG, Core::PRECISION

Class Method Summary collapse

Methods included from Core

_ext_get_module, _ext_match, _ext_parse, _ext_read_module, add_bundle, bundles, contains_bundle, eval_expression, eval_operand, extract_expression, get_condition_from_expression, get_key, get_op, iterator, log, module_match, modules, ops, re_type, reset, results_from_expression, rev_op

Class Method Details

._parse_key_value_condition(expression) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hash_conditions/parser.rb', line 24

def self._parse_key_value_condition expression
  # "#{key} #{_parse_value_condition condition}"
  comparisson = case expression[:operator]
    when :==
      "= #{_parse_value expression[:value]}"
    when :!=
      "!= #{_parse_value expression[:value]}"
    when :>, :<, :>=, :<=
      "#{ expression[:operator] } #{_parse_value expression[:value]}"
    when :in
      "IN ( #{ expression[:value].map{ |v| _parse_value v }.join ", " } )"
    when :contains
      "LIKE #{ _parse_value(expression[:value], '%', '%') }"
    when :between
      "BETWEEN #{ _parse_value expression[:value][0] } AND #{ _parse_value expression[:value][1] }"
  end

  "#{expression[:key]} #{comparisson}"
end

._parse_value(value, prepend = '', append = '') ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/hash_conditions/parser.rb', line 44

def self._parse_value value, prepend = '', append = ''
  case value
    when String then "'#{prepend}#{value}#{append}'"
    when DateTime, Date, Time then "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
    when Integer, Float then "#{value}"
    when Hash then _parse_value( eval_operand( {}, value ) , prepend, append )
  end
end

.get_conditions(conditions, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hash_conditions/parser.rb', line 5

def self.get_conditions conditions, options = {}
  options = options.merge \
    operation: :parse,
    result: lambda{ | expression, options |
      _parse_key_value_condition expression
    },
    finalize: lambda{ | array, options |
      "( #{ array.join( " #{ options[:glue].upcase } " ) } )"
    }

  result = iterator conditions, options

  result.
    gsub!(/^\( /, '').
    gsub!(/ \)$/, '')

  result
end