plucky 0.6.1 → 0.6.2
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/plucky/criteria_hash.rb
CHANGED
@@ -92,23 +92,25 @@ module Plucky
|
|
92
92
|
if old_value.is_a?(Hash) && new_value.is_a?(Hash)
|
93
93
|
self.class.new(old_value).merge(self.class.new(new_value)).to_hash
|
94
94
|
else
|
95
|
-
|
95
|
+
merge_values_into_array(old_value, new_value)
|
96
96
|
end
|
97
97
|
end
|
98
98
|
elsif value_is_hash && !other_is_hash
|
99
99
|
if modifier_key = value.keys.detect { |k| Plucky.modifier?(k) }
|
100
|
-
value[modifier_key]
|
100
|
+
current_value = value[modifier_key]
|
101
|
+
value[modifier_key] = current_value.concat(array(other_value)).uniq
|
101
102
|
else
|
102
103
|
# kaboom! Array(value).concat(Array(other_value)).uniq
|
103
104
|
end
|
104
105
|
elsif other_is_hash && !value_is_hash
|
105
106
|
if modifier_key = other_value.keys.detect { |k| Plucky.modifier?(k) }
|
106
|
-
other_value[modifier_key]
|
107
|
+
current_value = other_value[modifier_key]
|
108
|
+
other_value[modifier_key] = current_value.concat(array(value)).uniq
|
107
109
|
else
|
108
110
|
# kaboom! Array(value).concat(Array(other_value)).uniq
|
109
111
|
end
|
110
112
|
else
|
111
|
-
|
113
|
+
merge_values_into_array(value, other_value)
|
112
114
|
end
|
113
115
|
else
|
114
116
|
other_value
|
@@ -117,6 +119,18 @@ module Plucky
|
|
117
119
|
self.class.new(target)
|
118
120
|
end
|
119
121
|
|
122
|
+
# Private
|
123
|
+
def merge_values_into_array(value, other_value)
|
124
|
+
array(value).concat(array(other_value)).uniq
|
125
|
+
end
|
126
|
+
|
127
|
+
# Private: Array(BSON::ObjectId) returns the byte array or what not instead
|
128
|
+
# of the object id. This makes sure it is an array of object ids, not the
|
129
|
+
# guts of the object id.
|
130
|
+
def array(value)
|
131
|
+
value.is_a?(BSON::ObjectId) ? [value] : Array(value)
|
132
|
+
end
|
133
|
+
|
120
134
|
# Public
|
121
135
|
def merge!(other)
|
122
136
|
merge(other).to_hash.each do |key, value|
|
@@ -26,7 +26,7 @@ module Plucky
|
|
26
26
|
|
27
27
|
if nesting_operator?(key)
|
28
28
|
value.map { |v| criteria_hash_class.new(v, options).to_hash }
|
29
|
-
elsif parent_key == key && !modifier?(key)
|
29
|
+
elsif parent_key == key && !modifier?(key) && !value.empty?
|
30
30
|
# we're not nested and not the value for a symbol operator
|
31
31
|
{:$in => value.to_a}
|
32
32
|
else
|
data/lib/plucky/version.rb
CHANGED
@@ -100,6 +100,23 @@ describe Plucky::CriteriaHash do
|
|
100
100
|
c2.merge(c1).source.should eq(:foo => {:$in => %w[bar baz]})
|
101
101
|
end
|
102
102
|
|
103
|
+
it "correctly merges two bson object ids" do
|
104
|
+
id1 = BSON::ObjectId.new
|
105
|
+
id2 = BSON::ObjectId.new
|
106
|
+
c1 = described_class.new(:foo => id1)
|
107
|
+
c2 = described_class.new(:foo => id2)
|
108
|
+
c1.merge(c2).source.should eq(:foo => {:$in => [id1, id2]})
|
109
|
+
end
|
110
|
+
|
111
|
+
it "correctly merges array and an object id" do
|
112
|
+
id1 = BSON::ObjectId.new
|
113
|
+
id2 = BSON::ObjectId.new
|
114
|
+
c1 = described_class.new(:foo => [id1])
|
115
|
+
c2 = described_class.new(:foo => id2)
|
116
|
+
c1.merge(c2).source.should eq(:foo => {:$in => [id1, id2]})
|
117
|
+
c2.merge(c1).source.should eq(:foo => {:$in => [id1, id2]})
|
118
|
+
end
|
119
|
+
|
103
120
|
it "is able to merge two modifier hashes" do
|
104
121
|
c1 = described_class.new(:$in => [1, 2])
|
105
122
|
c2 = described_class.new(:$in => [2, 3])
|
@@ -64,6 +64,12 @@ describe Plucky::Normalizers::CriteriaHashValue do
|
|
64
64
|
subject.call(:foo, :foo, actual).should eq(expected)
|
65
65
|
end
|
66
66
|
|
67
|
+
it "does not turn value to $in with an empty array value" do
|
68
|
+
actual = []
|
69
|
+
expected = []
|
70
|
+
subject.call(:foo, :foo, actual).should eq(expected)
|
71
|
+
end
|
72
|
+
|
67
73
|
it "does not turn value to $in with $or key" do
|
68
74
|
actual = [{:numbers => 1}, {:numbers => 2}]
|
69
75
|
expected = [{:numbers => 1}, {:numbers => 2}]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: plucky
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-12-31 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongo
|
16
|
-
requirement: &
|
16
|
+
requirement: &70139265557120 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '1.5'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70139265557120
|
25
25
|
description:
|
26
26
|
email:
|
27
27
|
- nunemaker@gmail.com
|
@@ -91,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
91
|
version: '0'
|
92
92
|
segments:
|
93
93
|
- 0
|
94
|
-
hash:
|
94
|
+
hash: -4423147418805869857
|
95
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
96
|
none: false
|
97
97
|
requirements:
|
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
version: '0'
|
101
101
|
segments:
|
102
102
|
- 0
|
103
|
-
hash:
|
103
|
+
hash: -4423147418805869857
|
104
104
|
requirements: []
|
105
105
|
rubyforge_project:
|
106
106
|
rubygems_version: 1.8.10
|