hash-to-conditions 0.3.3 → 0.3.4
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.
- data/lib/helpers/array_helper.rb +24 -16
- data/lib/helpers/hash_helper.rb +3 -3
- metadata +5 -4
data/lib/helpers/array_helper.rb
CHANGED
@@ -21,7 +21,7 @@ class ArrayHelper
|
|
21
21
|
parts = @array.first.to_s.split('.')
|
22
22
|
raise "field_cannot_be_empty" if parts.empty?
|
23
23
|
|
24
|
-
field = parts[0].strip
|
24
|
+
field = parts[0].strip.to_s
|
25
25
|
operator = parts[1]
|
26
26
|
value = @array.last
|
27
27
|
|
@@ -34,35 +34,43 @@ class ArrayHelper
|
|
34
34
|
# handle implicit .eq ({'name' => 'value'}) or .like ({'name' => 'value%'})
|
35
35
|
operator = value.to_s.index('%') ? 'like' : 'eq'
|
36
36
|
end
|
37
|
-
operator = operator.downcase
|
37
|
+
operator = operator.to_s.downcase
|
38
38
|
mapped = operator.to_operator
|
39
39
|
raise "unknown_operator" unless mapped
|
40
40
|
|
41
41
|
# handle .null or .nnull, suppress value
|
42
42
|
return [field + mapped] if operator.index('null')
|
43
43
|
|
44
|
-
|
45
|
-
if ['in', 'between'].index(operator)
|
46
|
-
values = value.to_s.split(',').collect { | ea | ea.strip }
|
47
|
-
if 'in' == operator
|
48
|
-
result = [field + mapped, values]
|
49
|
-
else
|
50
|
-
# between
|
51
|
-
result = [field + mapped, values[0], values[1]]
|
52
|
-
end
|
53
|
-
return result
|
54
|
-
end
|
44
|
+
return [field + mapped, value] unless ['in', 'between'].index(operator)
|
55
45
|
|
56
|
-
|
46
|
+
handle_in_between(operator, field, mapped, value)
|
57
47
|
end
|
58
48
|
|
59
49
|
|
60
50
|
protected
|
61
51
|
|
62
|
-
|
63
|
-
|
52
|
+
# Creates a new instance
|
53
|
+
def initialize(array)
|
64
54
|
@array = array
|
65
55
|
end
|
56
|
+
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# Handle .in (?) or .between ? and ?
|
61
|
+
# Return an expanded Array condition
|
62
|
+
#
|
63
|
+
def handle_in_between(operator, field, mapped_operator, csv)
|
64
|
+
values = csv.to_s.split(',').collect { | ea | ea.strip }
|
65
|
+
if 'in' == operator
|
66
|
+
result = [field + mapped_operator, values]
|
67
|
+
else
|
68
|
+
# between
|
69
|
+
result = [field + mapped_operator, values[0], values[1]]
|
70
|
+
end
|
71
|
+
result
|
72
|
+
end
|
73
|
+
|
66
74
|
end
|
67
75
|
|
68
76
|
end
|
data/lib/helpers/hash_helper.rb
CHANGED
@@ -35,7 +35,7 @@ class HashHelper
|
|
35
35
|
raise "empty_condition" if @hash.empty?
|
36
36
|
result_s = ''
|
37
37
|
result_a = []
|
38
|
-
join_s = @hash.first.first
|
38
|
+
join_s = @hash.first.first.to_s
|
39
39
|
if ['AND', 'OR'].index(join_s.upcase)
|
40
40
|
parse(@hash.first.last, join_s.upcase, result_s, result_a)
|
41
41
|
else
|
@@ -56,8 +56,8 @@ class HashHelper
|
|
56
56
|
|
57
57
|
result_s << '('
|
58
58
|
count = hash.length
|
59
|
-
hash.each_pair { |
|
60
|
-
arr =
|
59
|
+
hash.each_pair { | k, v |
|
60
|
+
arr = [k, v].to_condition
|
61
61
|
if arr.kind_of?(Hash)
|
62
62
|
# handle nested condition
|
63
63
|
parse(arr.first.last, arr.first.first, result_s, result_a, 1+nest_lev)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash-to-conditions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
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: 2013-
|
12
|
+
date: 2013-07-03 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: The HashToConditions gem provides an easy way to build ActiveRecord Array
|
15
15
|
conditions directly from a Hash.
|
@@ -26,7 +26,8 @@ files:
|
|
26
26
|
- lib/ext/string.rb
|
27
27
|
- lib/ext/hash.rb
|
28
28
|
homepage: https://rubygems.org/gems/hash-to-conditions
|
29
|
-
licenses:
|
29
|
+
licenses:
|
30
|
+
- MIT
|
30
31
|
post_install_message:
|
31
32
|
rdoc_options: []
|
32
33
|
require_paths:
|
@@ -45,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
46
|
version: '0'
|
46
47
|
requirements: []
|
47
48
|
rubyforge_project:
|
48
|
-
rubygems_version: 1.8.
|
49
|
+
rubygems_version: 1.8.25
|
49
50
|
signing_key:
|
50
51
|
specification_version: 3
|
51
52
|
summary: Converts a Hash to ActiveRecord Array conditions
|