hash_conditions 0.1.16 → 0.1.17

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
- OTY2ZDFkZWU4YWE1ZGRjMzU3M2EzODhkMzQ1ZWE4MGVkMTI2OWY4Ng==
4
+ MjYwN2RmYmExMWMzYzEyOTEyMTkyMmU2NDhiMWI1MWM0ZTVjZGI4Mw==
5
5
  data.tar.gz: !binary |-
6
- NDhhNzVkZDE2ZTEzZjQyZDRmMTQyZGMwYWUxYmQwOTM0MmFkMjUwZQ==
6
+ NzcwYTRhM2Q5ZTkzMmI5OWE4Y2JjMWJiNTViYmJjN2E4MDUwMDM1ZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTQ0NmMzMTc4NzMyNzQwODVkNDQ3ODQxNTY3ZjcwMGQyODk0NGU0NWVjMmQ2
10
- MmRlODA1YmVlMjk2YzQ2OGFjMzgwZDk0ZjQ5ODVjYWZhYTNjNjZkOTI2MjQ3
11
- N2Y1YWFhYzMzMDcwYzU1YWE3ZDE4YjQwMTlhZTkzNzUxNGMzNzk=
9
+ NTA3Y2I1Njg3MmQ1NmIzYmYzM2ViZWEzMmNlNmQzMDY2YzE2OTY2YWZmNTk5
10
+ MzI0N2VmZWUzZTI3MDc4ODRkOWU2ZjI2MjJlZjk4ZDhkZDc3NjgzY2FhOTgx
11
+ Njc3MzllNDJlMTQwNGRkNWRmMjZkODU0N2VkMDg1NThmZmUxMDI=
12
12
  data.tar.gz: !binary |-
13
- NjZmN2ZjOTBjMGRmZjNlZDk0NjdhODg4OGJkZWYxM2JhZmM4ODI1YmQxN2E5
14
- ZDc2N2JkMGI4ZDViMGQ1NTRkOWNkNDllNjEzZmRhNjgzOWZlOGM0M2RjYjgy
15
- MjI2OGQxZGE4MjY5Y2NmNGRkMTdiNDMwZTc5YWVmNzc2OTNiNmM=
13
+ MTJhZGE4NWFhYjJkNmNiNTUzYjkzNjJhMGFmOGNkN2JlMzdlMjYzYTVkOGVm
14
+ NGM5YWM0ZDcwZjA1MDBhZDdiZjE1NTQ5MTFlM2FmYmFlMTBjYjk1NGRkMmYz
15
+ OGY1YWQwNTIyZTU0MmYwNTNkNmI0YjliMDZjZDcwMDVhNWY0NjI=
@@ -1,6 +1,13 @@
1
1
  module HashConditions
2
2
  module Core
3
3
  # Modular Matchers
4
+ ARITMETIC_OPERATORS = {
5
+ '$add' => :+,
6
+ '$substract' => :-,
7
+ '$multiply' => :*,
8
+ '$divide' => :/,
9
+ }
10
+
4
11
  def modules
5
12
  @@modules ||= []
6
13
  end
@@ -134,6 +141,38 @@ module HashConditions
134
141
  end
135
142
  end
136
143
 
144
+ def eval_operand hash, key, options = {}
145
+ __get_values = lambda do | values |
146
+ values.map{ |x| eval_operand hash, x, options }
147
+ end
148
+ case key
149
+ when String, Symbol
150
+ if key.to_s == '$now'
151
+ options[:current_time] || Time.now
152
+ else
153
+ val = options[:is_key] ? hash[key] : key
154
+ re_type val
155
+ end
156
+ when Hash
157
+ op, values = key.to_a.first
158
+
159
+ case op.to_s
160
+ when *ARITMETIC_OPERATORS.keys
161
+ #TODO: Test feature: when applying aritmetics it forces that the values are floats
162
+ __get_values.call( values ).each{ |v| v.to_f }.inject( ARITMETIC_OPERATORS[ op ] )
163
+ when '$ifNull'
164
+ __get_values.call( values ).drop_while{ |n| n.nil? }.shift
165
+ when '$concat'
166
+ __get_values.call( values ).join('')
167
+ when '$concatWithSeparator'
168
+ separator = values.shift
169
+ __get_values.call( values ).join( separator )
170
+ end
171
+ else
172
+ key
173
+ end
174
+ end
175
+
137
176
  def _ext_match condition, options
138
177
  ->(key) { _ext_get_module( key, condition, options ) != nil }
139
178
  end
@@ -1,12 +1,6 @@
1
1
  module HashConditions
2
2
  class Matcher
3
3
  extend Core
4
- ARITMETIC_OPERATORS = {
5
- '$add' => :+,
6
- '$substract' => :-,
7
- '$multiply' => :*,
8
- '$divide' => :/,
9
- }
10
4
 
11
5
  def self.fix_for_aritmetics *values
12
6
  class_precedence = [ Float, Integer, String ]
@@ -54,38 +48,6 @@ module HashConditions
54
48
  end
55
49
  end
56
50
 
57
- def self.eval_operand hash, key, options = {}
58
- __get_values = lambda do | values |
59
- values.map{ |x| eval_operand hash, x, options }
60
- end
61
- case key
62
- when String, Symbol
63
- if key.to_s == '$now'
64
- options[:current_time] || Time.now
65
- else
66
- val = options[:is_key] ? hash[key] : key
67
- re_type val
68
- end
69
- when Hash
70
- op, values = key.to_a.first
71
-
72
- case op.to_s
73
- when *ARITMETIC_OPERATORS.keys
74
- #TODO: Test feature: when applying aritmetics it forces that the values are floats
75
- __get_values.call( values ).each{ |v| v.to_f }.inject( ARITMETIC_OPERATORS[ op ] )
76
- when '$ifNull'
77
- __get_values.call( values ).drop_while{ |n| n.nil? }.shift
78
- when '$concat'
79
- __get_values.call( values ).join('')
80
- when '$concatWithSeparator'
81
- separator = values.shift
82
- __get_values.call( values ).join( separator )
83
- end
84
- else
85
- key
86
- end
87
- end
88
-
89
51
  def self.match_single hash, expression, options
90
52
  hash_value = eval_operand hash, expression[:key], options.merge(is_key: true)
91
53
  comparisson_value = eval_operand hash, expression[ :value ], options
@@ -112,8 +74,8 @@ module HashConditions
112
74
  values = fix_for_aritmetics hash_value, comparisson_value
113
75
  values[0].send( expression[:operator], values[1] )
114
76
  end
115
- rescue
116
- raise "The expression: #{ expression } has an error"
77
+ # rescue
78
+ # raise "The expression: #{ expression } has an error"
117
79
  end
118
80
 
119
81
  def self.when hash, query
@@ -35,7 +35,7 @@ module HashConditions
35
35
  when :contains
36
36
  "LIKE #{ _parse_value(expression[:value], '%', '%') }"
37
37
  when :between
38
- "BETWEEN #{ _parse_value expression[:value].shift } AND #{ _parse_value expression[:value].shift }"
38
+ "BETWEEN #{ _parse_value expression[:value][0] } AND #{ _parse_value expression[:value][1] }"
39
39
  end
40
40
 
41
41
  "#{expression[:key]} #{comparisson}"
@@ -46,6 +46,7 @@ module HashConditions
46
46
  when String then "'#{prepend}#{value}#{append}'"
47
47
  when DateTime, Date, Time then "'#{value.strftime("%Y-%m-%d %H:%M")}'"
48
48
  when Integer, Float then "#{value}"
49
+ when Hash then _parse_value( eval_operand( {}, value ) , prepend, append )
49
50
  end
50
51
  end
51
52
  end
@@ -1,3 +1,3 @@
1
1
  module HashConditions
2
- VERSION = "0.1.16"
2
+ VERSION = "0.1.17"
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: 0.1.16
4
+ version: 0.1.17
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-09 00:00:00.000000000 Z
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler