mongo-parser-rb 0.0.9 → 0.0.10

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ca72a97d873ee39341d3940cd420c4356d3f36d
4
- data.tar.gz: 8cd517a154c3e5f8f0777c8160565cc39b413ad9
3
+ metadata.gz: 3e5c3c9944b9b2262bf312b33c3de09ab007635c
4
+ data.tar.gz: da70df6109d44a394390c39bcd917e78430a6797
5
5
  SHA512:
6
- metadata.gz: 248798735c41f7905a3d24cf26986161715fd97d60e1b15b7b84c53535efa2a57a6aa0201680fee05329ab3bbd93fb5a00e578b3d9fdae8e8a0e643aaf8905db
7
- data.tar.gz: 86e95e493e18b9e524f5ff7a1da9cfc66a5c6b502bea4ca6ea68ce92aa37f719264b2d66365a0655493bd19175322b871eca9561439bd745c396d0de87db2bad
6
+ metadata.gz: 8530e477fa457e89422e3804231d746b82dece1f0b7d0730273d3481aca7b216c9533bc7a2b317f1bf9e8a6655fa544c669344a98913db80354ce799426097ad
7
+ data.tar.gz: c640747cd0855739e8d422f79105ff70f01f9f61d1346f7ced2a833389553d23261abea5654f6dcd122d2447314346a3bf80bba952fdea64ffde542ac8a8b0cd
@@ -46,9 +46,19 @@ module MongoParserRB
46
46
  def equality_operator?(operator)
47
47
  equality_operators.include?(operator)
48
48
  end
49
+
50
+ def elemMatch_operators
51
+ @elemMatch_operators ||= [
52
+ :$elemMatch
53
+ ]
54
+ end
55
+
56
+ def elemMatch_operator?(operator)
57
+ elemMatch_operators.include?(operator)
58
+ end
49
59
 
50
60
  def operator?(operator)
51
- equality_operator?(operator) || conjunction_operator?(operator) || inversion_operator?(operator)
61
+ equality_operator?(operator) || conjunction_operator?(operator) || inversion_operator?(operator) || elemMatch_operator?(operator)
52
62
  end
53
63
 
54
64
  end
@@ -74,6 +84,8 @@ module MongoParserRB
74
84
  evaluate_equality(document)
75
85
  when *Expression.inversion_operators
76
86
  evaluate_inversion(document)
87
+ when *Expression.elemMatch_operators
88
+ evaluate_elemMatch(document)
77
89
  end
78
90
  rescue NoMethodError, TypeError
79
91
  false
@@ -81,6 +93,14 @@ module MongoParserRB
81
93
 
82
94
  private
83
95
 
96
+ def evaluate_elemMatch(document)
97
+ @field.value_in_document(document).any? do |subdocument|
98
+ @arguments.all? do |arg|
99
+ arg.evaluate(subdocument)
100
+ end
101
+ end
102
+ end
103
+
84
104
  def evaluate_inversion(document)
85
105
  # Mongo negative equality operators return true when
86
106
  # the specified field does not exist on a document.
@@ -53,6 +53,10 @@ module MongoParserRB
53
53
  else
54
54
  Expression.new(:$not, field, Expression.new(:$eq, field, value))
55
55
  end
56
+ when *Expression.elemMatch_operators
57
+ Expression.new(:$elemMatch, field, value.to_a.map do |(key, value)|
58
+ parse_sub_expression(key, value)
59
+ end)
56
60
  else
57
61
  Expression.new(key, field, value)
58
62
  end
@@ -1,3 +1,3 @@
1
1
  module MongoParserRB
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -219,7 +219,19 @@ class QueryTest < MiniTest::Unit::TestCase
219
219
  query = MongoParserRB::Query.parse(:array_key => {:$ne => [1]})
220
220
  assert query.matches_document?(:array_key => [1,2])
221
221
  end
222
-
222
+
223
+ def test_elemMatch
224
+ query = MongoParserRB::Query.parse({:user_event_summaries => {:$elemMatch=> {:name => "second-support", :count => 3}}})
225
+ refute query.matches_document?(:user_event_summaries => [{:name => "first-support", :count => 3}, {:name => "second-support", :count => 4}])
226
+ assert query.matches_document?(:user_event_summaries => [{:name => "first-support", :count => 4}, {:name => "second-support", :count => 3}])
227
+ end
228
+
229
+ def test_elemMatch_inequality
230
+ query = MongoParserRB::Query.parse(:user_event_summaries => {:$elemMatch=> {:count => {:$ne => 2}, :name => "second-support"}})
231
+ refute query.matches_document?(:user_event_summaries => [{:name => "first-support", :count => 3}, {:name => "second-support", :count => 2}])
232
+ assert query.matches_document?(:user_event_summaries => [{:name => "first-support", :count => 3}, {:name => "second-support", :count => 3}])
233
+ end
234
+
223
235
  def test_datatype_mismatch
224
236
  query = MongoParserRB::Query.parse(:integer_key => {:$gt => 5})
225
237
  refute query.matches_document?(:integer_key => "hello")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo-parser-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben McRedmond
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-31 00:00:00.000000000 Z
11
+ date: 2014-02-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Parse and evaluate mongo queries in Ruby
14
14
  email: