Class: HashConditions::Matcher

Inherits:
Object
  • Object
show all
Extended by:
Core
Defined in:
lib/hash_conditions/matcher.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

.configuration(key) ⇒ Object



26
27
28
# File 'lib/hash_conditions/matcher.rb', line 26

def self.configuration key
  configurations[key]
end

.configurationsObject



18
19
20
# File 'lib/hash_conditions/matcher.rb', line 18

def self.configurations
  @@configurations ||= {}
end

.configure(config) ⇒ Object



22
23
24
# File 'lib/hash_conditions/matcher.rb', line 22

def self.configure config
  configurations.merge! config
end

.critical_times(hash, expressions, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/hash_conditions/matcher.rb', line 97

def self.critical_times hash, expressions, options = {}
  current_time = options[:current_time] ||= Time.now
  expressions.
    map do | e |
      inverter = operand_uses_now?(e[:value])? -1: 1
      case e[:operator]
        when :<, :<=, :>, :>= then
          diff = inverter * get_diff( eval_operand( hash, e[:value], options ),
                                      eval_operand( hash, e[:key], options.merge(is_key: true) )) + 0.001
        when :==, :!= then
          # TODO: test this functionality
          diff = inverter * get_diff( eval_operand( hash, e[:value], options ),
                                      eval_operand(hash, e[:key], options.merge(is_key: true) ) )
        when :between
          diff = inverter * get_diff( eval_operand( hash, e[:value][0], options ),
                                      eval_operand(hash, e[:key], options.merge(is_key: true) ) )
          diff = inverter * get_diff( eval_operand( hash, e[:value][1], options ),
                                      eval_operand(hash, e[:key], options.merge(is_key: true) ) ) if current_time + diff < current_time
      end

      current_time + diff
    end
end

.finalize(hash, array, options) ⇒ Object



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

def self.finalize hash, array, options
  case options[:glue]
    when :or then array.any?
    when :and then array.all?
  end
end

.fix_for_aritmetics(*values) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/hash_conditions/matcher.rb', line 5

def self.fix_for_aritmetics *values
  class_precedence = [ Float, Integer, String ]

  klass = class_precedence.find{ |k| values.any?{ |v| v.is_a? k } } || NilClass

  values = case klass.name
    when "Integer" then values.map(&:to_i)
    when "Float"   then values.map(&:to_f).map{|x|x.round(3)}
    when "String"  then values.map(&:to_s)
    else values
  end
end

.get_diff(*values) ⇒ Object



121
122
123
# File 'lib/hash_conditions/matcher.rb', line 121

def self.get_diff *values
  fix_for_aritmetics(*values).inject(&:-)
end

.match(hash, conditions, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hash_conditions/matcher.rb', line 30

def self.match hash, conditions, options = {}
  options = {
    operation: :match,
    result: lambda{ | expression, options |
      match_single hash, expression, options
    },
    finalize: lambda{ | array, options |
      finalize hash, array, options
    }
  }.merge options

  iterator conditions, options
end

.match_single(hash, expression, options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hash_conditions/matcher.rb', line 51

def self.match_single hash, expression, options
  log "Matching:", expression
  hash_value = eval_operand hash, expression[:key], options.merge(is_key: true)
  comparisson_value = eval_operand hash, expression[ :value ], options

  log "Left:", hash_value
  log "Right:", comparisson_value

  case expression[:operator]
    when :==
      if configuration( :force_string_comparations )
        hash_value = hash_value.to_s
        comparisson_value = comparisson_value.to_s
      end
      hash_value == comparisson_value
    when :in
      if configuration( :force_string_comparations )
        hash_value = hash_value.to_s
        comparisson_value = comparisson_value.map(&:to_s)
      end

      comparisson_value.include? hash_value
    when :between
      values = fix_for_aritmetics hash_value, *comparisson_value
      values[0] >= values[1] and values[0] < values[2]
    when :contains
      !! %r{#{comparisson_value}}.match( hash_value )
    else
      values = fix_for_aritmetics hash_value, comparisson_value
      values[0].send( expression[:operator], values[1] )
  end
  # rescue
    # raise "The expression: #{ expression } has an error"
end

.operand_uses_now?(key) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/hash_conditions/matcher.rb', line 147

def self.operand_uses_now? key
  case key
    when String, Symbol
      key.to_s == '$now'
    when Hash
      op, values = key.to_a.first
      values.map{ |v| operand_uses_now? v }.any?
    when Array
      key.map{ |v| operand_uses_now? v }.any?
    else
      false
  end
end

.time_expressions(conditions) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/hash_conditions/matcher.rb', line 125

def self.time_expressions conditions
  expressions = []
  iterator conditions,
    operation: :match,
    result: lambda{ | expression, options |
      expressions << expression if uses_now? expression
    },
    finalize: lambda{ | array, options |
      expressions
    }

  expressions
end

.time_sensible?(conditions) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/hash_conditions/matcher.rb', line 139

def self.time_sensible? conditions
  ! time_expressions(conditions).empty?
end

.uses_now?(expression) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/hash_conditions/matcher.rb', line 143

def self.uses_now? expression
  operand_uses_now? expression[:key]  or operand_uses_now? expression[:value]
end

.when(hash, query, options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/hash_conditions/matcher.rb', line 86

def self.when hash, query, options = {}
  current_time = options[:current_time] ||= Time.now
  now_result = match hash, query, options
  test_times = critical_times( hash, time_expressions( query ), options )
  test_times.
   sort.
   drop_while{ |t| t < current_time }.
   find{ |t| now_result != match( hash, query, options.merge(current_time: t) ) }.
   tap{ |t| log 'Critical Times:', t }
end