hash_conditions 1.1.23 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Mjc3OTg4MjlkZDkzZmI4MTc1YjI2OGRjMzUzZmUzOWYwZmI3OGM2Mg==
4
+ MGY2YzgxMmQ0ZTIzOTIyOGM1NGI0MDlkOGI4ZjIyNDQ2NmY5ZTFlZQ==
5
5
  data.tar.gz: !binary |-
6
- ZWUwMmVlMTJjMGNlNGI1NDhjYTMyYjI4ZWVmYjg2MGJkMTYwYTZjNg==
6
+ OTE5ZTk3M2VjMjhmODIwNjJlNGQ1ZDZkYWU0M2IzMWYwMTMwYzUyMg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZGFiYjQ1NTFiOTk3ZjU1NzQ3OTc0MjIwNTA0ZDBlNWQyNWI2NzE3YmRhOTQ2
10
- YTIyY2NjZjUyYWZiMDcyMjJjY2Q5NDA2NGNkMTFjMzZmY2ExYTI1YTY2ZGU5
11
- ZTJjNzZmMDkyYjg2ZGFlZmYwNTQwYTgxMmJjNDY1NjA0ODM3OGM=
9
+ OWU3YWQxNDZiMzI5Yjk1ODg4NWM5N2EzODQwYjM1OTRkZTBkY2YzMzY2ZTc3
10
+ OTAxMzFiZTRhMzU4ZmNhYzUzZmUxYzJiODhmZDg0NzYxMTQ5Y2MzZTk5NTUx
11
+ MTdlOGFmZjRjNjkwNWUwOGVhODVlODAyODA5YTdmMTMxNWJlZWQ=
12
12
  data.tar.gz: !binary |-
13
- ZTFmYzZhODQ3ODMyYjI1YzQ0OTcwMGE3ZjQ3MGVkNGQ3NzBmZmNjZGVhYTJk
14
- Zjk0ZGUzZjg2YTgxNzRmNjQyMGNkY2ExZTFjMzlkNzE2M2Y1ZDllZTZmOGYz
15
- MWE3MGFiYjIzNWJjYTFiMDQwNTY3ZWJjZjdlOWJlNmU0M2FmZTk=
13
+ MjUxMTY0NzcxNjkzNTFkOGIwZDA0OGMyMzA4MmM2NmE4ZTJkZDRmNThhOTBk
14
+ ODRjNWU1NmMxODQwYWM2NTI3ODI1MjkxMTdhNzJjMDYxMzcyNTg5NWZmMDJi
15
+ ZTcxNTM2MjI3ZGExY2MwZTUxNDA4MDJkNGJmYzM3OWY4NTI4YjE=
@@ -2,6 +2,7 @@ module HashConditions
2
2
  module Core
3
3
  # Modular Matchers
4
4
  DEBUG=false
5
+ PRECISION=5
5
6
  ARITMETIC_OPERATORS = {
6
7
  '$add' => :+,
7
8
  '$substract' => :-,
@@ -210,7 +211,7 @@ module HashConditions
210
211
  end
211
212
 
212
213
  def log *args
213
- puts *args if(DEBUG)
214
+ puts args.map(&:to_s).join(" ") if(DEBUG)
214
215
  end
215
216
  end
216
217
  end
@@ -9,7 +9,7 @@ module HashConditions
9
9
 
10
10
  values = case klass.name
11
11
  when "Integer" then values.map(&:to_i)
12
- when "Float" then values.map(&:to_f)
12
+ when "Float" then values.map(&:to_f).map{|x|x.round(3)}
13
13
  when "String" then values.map(&:to_s)
14
14
  else values
15
15
  end
@@ -71,7 +71,8 @@ module HashConditions
71
71
 
72
72
  comparisson_value.include? hash_value
73
73
  when :between
74
- hash_value > comparisson_value[0] and hash_value < comparisson_value[1]
74
+ values = fix_for_aritmetics hash_value, *comparisson_value
75
+ values[0] >= values[1] and values[0] < values[2]
75
76
  when :contains
76
77
  !! %r{#{comparisson_value}}.match( hash_value )
77
78
  else
@@ -82,31 +83,39 @@ module HashConditions
82
83
  # raise "The expression: #{ expression } has an error"
83
84
  end
84
85
 
85
- def self.when hash, query
86
- now_result = match hash, query
87
- test_times = critical_times( hash, time_expressions( query ) )
86
+ def self.when hash, query, options = {}
87
+ current_time = options[:current_time] ||= Time.now
88
+ now_result = match hash, query, options
89
+ test_times = critical_times( hash, time_expressions( query ), options )
88
90
  test_times.
89
91
  sort.
90
- drop_while{ |t| t < Time.now }.
91
- find{ |t| now_result != match( hash, query, current_time: t ) }
92
+ drop_while{ |t| t < current_time }.
93
+ find{ |t| now_result != match( hash, query, options.merge(current_time: t) ) }.
94
+ tap{ |t| log 'Critical Times:', t }
92
95
  end
93
96
 
94
- def self.critical_times hash, expressions
97
+ def self.critical_times hash, expressions, options = {}
98
+ current_time = options[:current_time] ||= Time.now
95
99
  expressions.
96
- map{ | e |
100
+ map do | e |
97
101
  inverter = operand_uses_now?(e[:value])? -1: 1
98
102
  case e[:operator]
99
103
  when :<, :<=, :>, :>= then
100
- diff = inverter * get_diff( eval_operand( hash, e[:value] ), eval_operand( hash, e[:key], is_key: true )) + 1
101
- when :==, :!= then Time.now + s[:diff]
102
- diff = inverter * get_diff( eval_operand( hash, e[:value] ), eval_operand(hash, e[:key]) )
104
+ diff = inverter * get_diff( eval_operand( hash, e[:value], options ),
105
+ eval_operand( hash, e[:key], options.merge(is_key: true) )) + 0.001
106
+ when :==, :!= then
107
+ # TODO: test this functionality
108
+ diff = inverter * get_diff( eval_operand( hash, e[:value], options ),
109
+ eval_operand(hash, e[:key], options.merge(is_key: true) ) )
103
110
  when :between
104
- diff = inverter * get_diff( eval_operand( hash, e[:value][0] ), eval_operand(hash, e[:key], is_key: true ) )
105
- diff = inverter * get_diff( eval_operand( hash, e[:value][1] ), eval_operand(hash, e[:key], is_key: true ) ) if Time.now + diff < Time.now
111
+ diff = inverter * get_diff( eval_operand( hash, e[:value][0], options ),
112
+ eval_operand(hash, e[:key], options.merge(is_key: true) ) )
113
+ diff = inverter * get_diff( eval_operand( hash, e[:value][1], options ),
114
+ eval_operand(hash, e[:key], options.merge(is_key: true) ) ) if current_time + diff < current_time
106
115
  end
107
116
 
108
- Time.now + diff
109
- }
117
+ current_time + diff
118
+ end
110
119
  end
111
120
 
112
121
  def self.get_diff *values
@@ -1,3 +1,3 @@
1
1
  module HashConditions
2
- VERSION = "1.1.23"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_conditions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.23
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giancarlo Palavicini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-19 00:00:00.000000000 Z
11
+ date: 2015-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler