hash_conditions 0.1.8 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -160,6 +160,7 @@ module HashConditions
160
160
  case parser_output
161
161
  when Hash then iterator(parser_output, options)
162
162
  when NilClass then nil
163
+ when String then parser_output # this should be removed, since the matcher will not support it
163
164
  else raise "return data of type #{ parser_output.class } not supported"
164
165
  end
165
166
  end
@@ -14,15 +14,18 @@ module HashConditions
14
14
  configurations[key]
15
15
  end
16
16
 
17
- def self.match hash, conditions
18
- iterator conditions,
17
+ def self.match hash, conditions, options = {}
18
+ options = {
19
19
  operation: :match,
20
20
  result: lambda{ | expression, options |
21
- match_single hash, expression
21
+ match_single hash, expression, options
22
22
  },
23
23
  finalize: lambda{ | array, options |
24
24
  finalize hash, array, options
25
25
  }
26
+ }.merge options
27
+
28
+ iterator conditions, options
26
29
  end
27
30
 
28
31
  def self.finalize hash, array, options
@@ -39,14 +42,14 @@ module HashConditions
39
42
  '$divide' => :/,
40
43
  }
41
44
 
42
- def self.get_key hash, key
45
+ def self.get_key hash, key, options = {}
43
46
  __get_values = lambda do | values |
44
- values.map{ |x| get_key hash, x }
47
+ values.map{ |x| get_key hash, x, options }
45
48
  end
46
49
  case key
47
50
  when String, Symbol
48
51
  if key.to_s == '$now'
49
- Time.now
52
+ options[:current_time] || Time.now
50
53
  else
51
54
  re_type hash[key]
52
55
  end
@@ -67,8 +70,8 @@ module HashConditions
67
70
  end
68
71
  end
69
72
 
70
- def self.match_single hash, expression
71
- hash_value = get_key hash, expression[:key]
73
+ def self.match_single hash, expression, options
74
+ hash_value = get_key hash, expression[:key], options
72
75
  comparisson_value = expression[ :value ]
73
76
 
74
77
  case expression[:operator]
@@ -86,12 +89,66 @@ module HashConditions
86
89
 
87
90
  comparisson_value.include? hash_value
88
91
  when :between
89
- hash_value > comparisson_value.shift and hash_value < comparisson_value.shift
92
+ hash_value > comparisson_value[0] and hash_value < comparisson_value[1]
90
93
  when :contains
91
94
  !! %r{#{comparisson_value}}.match( hash_value )
92
95
  else
93
96
  hash_value.send( expression[:operator], comparisson_value )
94
97
  end
95
98
  end
99
+
100
+ def self.when hash, query
101
+ now_result = match hash, query
102
+ test_times = critical_times( hash, time_expressions( query ) )
103
+ test_times.
104
+ sort.
105
+ drop_while{ |t| t < Time.now }.
106
+ find{ |t| now_result != match( hash, query, current_time: t ) }
107
+ end
108
+
109
+ def self.critical_times hash, expressions
110
+ expressions.
111
+ map{ | e |
112
+ case e[:operator]
113
+ when :<, :<=, :>, :>= then
114
+ diff = e[:value] - get_key(hash, e[:key]) + 1
115
+ when :==, :!= then Time.now + s[:diff]
116
+ diff = e[:value] - get_key(hash, e[:key])
117
+ when :between
118
+ diff = e[:value][0] - get_key(hash, e[:key])
119
+ diff = e[:value][1] - get_key(hash, e[:key]) if Time.now + diff < Time.now
120
+ end
121
+
122
+ Time.now + diff
123
+ }
124
+ end
125
+
126
+ def self.time_expressions conditions
127
+ expressions = []
128
+ iterator conditions,
129
+ operation: :match,
130
+ result: lambda{ | expression, options |
131
+ expressions << expression if uses_now? expression
132
+ },
133
+ finalize: lambda{ | array, options |
134
+ expressions
135
+ }
136
+
137
+ expressions
138
+ end
139
+
140
+ def self.uses_now? expression
141
+ key_uses_now? expression[:key]
142
+ end
143
+
144
+ def self.key_uses_now? key
145
+ case key
146
+ when String, Symbol
147
+ key.to_s == '$now'
148
+ when Hash
149
+ op, values = key.to_a.first
150
+ values.map{ |v| key_uses_now? v }.any?
151
+ end
152
+ end
96
153
  end
97
154
  end
@@ -1,3 +1,3 @@
1
1
  module HashConditions
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.10"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_conditions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-06-04 00:00:00.000000000 Z
12
+ date: 2015-06-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -102,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
102
102
  version: '0'
103
103
  segments:
104
104
  - 0
105
- hash: 1535850409122845806
105
+ hash: -314695273958895407
106
106
  required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  none: false
108
108
  requirements:
@@ -111,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  version: '0'
112
112
  segments:
113
113
  - 0
114
- hash: 1535850409122845806
114
+ hash: -314695273958895407
115
115
  requirements: []
116
116
  rubyforge_project:
117
117
  rubygems_version: 1.8.23