mongo-parser-rb 0.0.8 → 0.0.9

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2ca72a97d873ee39341d3940cd420c4356d3f36d
4
+ data.tar.gz: 8cd517a154c3e5f8f0777c8160565cc39b413ad9
5
+ SHA512:
6
+ metadata.gz: 248798735c41f7905a3d24cf26986161715fd97d60e1b15b7b84c53535efa2a57a6aa0201680fee05329ab3bbd93fb5a00e578b3d9fdae8e8a0e643aaf8905db
7
+ data.tar.gz: 86e95e493e18b9e524f5ff7a1da9cfc66a5c6b502bea4ca6ea68ce92aa37f719264b2d66365a0655493bd19175322b871eca9561439bd745c396d0de87db2bad
data/README.md CHANGED
@@ -23,7 +23,7 @@ q.matches_document?({:comment_count => 11}) => false
23
23
  q.matches_document?({:comment_count => 6}) => true
24
24
  ```
25
25
 
26
- The following operators are currently supported: `$and`, `$or`, `$in`, `$nin`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`.
26
+ The following operators are currently supported: `$and`, `$or`, `$in`, `$nin`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`, `$not`.
27
27
 
28
28
  Regexps are also supported:
29
29
 
@@ -41,12 +41,18 @@ module MongoParserRB
41
41
  parse_sub_expression(key, value, field)
42
42
  end)
43
43
  end
44
-
44
+
45
45
  def parse_sub_expression(key, value, field = nil)
46
46
  if Expression.operator?(key)
47
47
  case key
48
48
  when *Expression.conjunction_operators
49
49
  Expression.new(key, value.map { |v| parse_root_expression(v) })
50
+ when *Expression.inversion_operators
51
+ if value.kind_of?(Hash)
52
+ Expression.new(:$not, field, parse_root_expression(value, field))
53
+ else
54
+ Expression.new(:$not, field, Expression.new(:$eq, field, value))
55
+ end
50
56
  else
51
57
  Expression.new(key, field, value)
52
58
  end
@@ -4,6 +4,16 @@ module MongoParserRB
4
4
 
5
5
  class << self
6
6
 
7
+ def inversion_operators
8
+ @inversion_operators ||= [
9
+ :$not
10
+ ]
11
+ end
12
+
13
+ def inversion_operator?(operator)
14
+ inversion_operators.include?(operator)
15
+ end
16
+
7
17
  def conjunction_operators
8
18
  @conjunction_operators ||= [
9
19
  :$and,
@@ -38,7 +48,7 @@ module MongoParserRB
38
48
  end
39
49
 
40
50
  def operator?(operator)
41
- equality_operator?(operator) || conjunction_operator?(operator)
51
+ equality_operator?(operator) || conjunction_operator?(operator) || inversion_operator?(operator)
42
52
  end
43
53
 
44
54
  end
@@ -46,7 +56,7 @@ module MongoParserRB
46
56
  def initialize(operator, *args)
47
57
  @operator = operator
48
58
 
49
- if Expression.conjunction_operator? @operator
59
+ if Expression.conjunction_operator?(@operator)
50
60
  @arguments = args[0]
51
61
  else
52
62
  @field = Field.new(args[0])
@@ -62,6 +72,8 @@ module MongoParserRB
62
72
  evaluate_negative_equality(document)
63
73
  when *Expression.equality_operators
64
74
  evaluate_equality(document)
75
+ when *Expression.inversion_operators
76
+ evaluate_inversion(document)
65
77
  end
66
78
  rescue NoMethodError, TypeError
67
79
  false
@@ -69,6 +81,13 @@ module MongoParserRB
69
81
 
70
82
  private
71
83
 
84
+ def evaluate_inversion(document)
85
+ # Mongo negative equality operators return true when
86
+ # the specified field does not exist on a document.
87
+ return true if !@field.value_in_document(document) && !@field.in_document?(document)
88
+ !@arguments.evaluate(document)
89
+ end
90
+
72
91
  def evaluate_conjunction(document)
73
92
  case @operator
74
93
  when :$and
@@ -1,3 +1,3 @@
1
1
  module MongoParserRB
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -1,7 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class QueryTest < MiniTest::Unit::TestCase
4
-
5
4
  def test_raises_if_not_parsed
6
5
  assert_raises(MongoParserRB::NotParsedError) do
7
6
  query = MongoParserRB::Query.new(:integer_key => 10)
@@ -28,6 +27,22 @@ class QueryTest < MiniTest::Unit::TestCase
28
27
  refute query.matches_document?(:integer_key => 9)
29
28
  end
30
29
 
30
+ def test_integer_not_gt
31
+ query = MongoParserRB::Query.parse(:integer_key => {:$not => {:$gt => 10}})
32
+ refute query.matches_document?(:integer_key => 11)
33
+ assert query.matches_document?(:integer_key => 10)
34
+ assert query.matches_document?(:integer_key => 9)
35
+ assert query.matches_document?({})
36
+ end
37
+
38
+ def test_integer_not_gt_or_lt
39
+ query = MongoParserRB::Query.parse(:integer_key => {:$not => {:$gt => 8, :$lt => 10}})
40
+ assert query.matches_document?(:integer_key => 6)
41
+ assert query.matches_document?(:integer_key => 11)
42
+ refute query.matches_document?(:integer_key => 9)
43
+ assert query.matches_document?({})
44
+ end
45
+
31
46
  def test_integer_lt
32
47
  query = MongoParserRB::Query.parse(:integer_key => {:$lt => 10})
33
48
  query.matches_document?(:integer_key => 9)
@@ -135,6 +150,14 @@ class QueryTest < MiniTest::Unit::TestCase
135
150
  refute query.matches_document?(:integer_key => 3)
136
151
  end
137
152
 
153
+ def test_integer_in_and_ne
154
+ query = MongoParserRB::Query.parse(:integer_key => {:$in => [1,2,3], :$ne => 3})
155
+ assert query.matches_document?(:integer_key => 1)
156
+ assert query.matches_document?(:integer_key => 2)
157
+ refute query.matches_document?(:integer_key => 3)
158
+ refute query.matches_document?(:integer_key => 4)
159
+ end
160
+
138
161
  def test_eq_nil
139
162
  query = MongoParserRB::Query.parse(:string_key => nil)
140
163
  assert query.matches_document?(:string_key => nil)
@@ -160,6 +183,12 @@ class QueryTest < MiniTest::Unit::TestCase
160
183
  refute query.matches_document?(:string_key => 'world')
161
184
  end
162
185
 
186
+ def test_regex_not_eq
187
+ query = MongoParserRB::Query.parse(:string_key => {:$not => /hello/})
188
+ assert query.matches_document?(:string_key => 'world')
189
+ refute query.matches_document?(:string_key => 'hello world')
190
+ end
191
+
163
192
  def test_operator_data_type_mismatch
164
193
  query = MongoParserRB::Query.parse(:array_key => {:$in => [1]})
165
194
  refute query.matches_document?(:array_key => "hey")
@@ -195,5 +224,4 @@ class QueryTest < MiniTest::Unit::TestCase
195
224
  query = MongoParserRB::Query.parse(:integer_key => {:$gt => 5})
196
225
  refute query.matches_document?(:integer_key => "hello")
197
226
  end
198
-
199
227
  end
metadata CHANGED
@@ -1,15 +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.8
5
- prerelease:
4
+ version: 0.0.9
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ben McRedmond
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-17 00:00:00.000000000 Z
11
+ date: 2014-01-31 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Parse and evaluate mongo queries in Ruby
15
14
  email:
@@ -36,27 +35,26 @@ files:
36
35
  - test/test_helper.rb
37
36
  homepage: ''
38
37
  licenses: []
38
+ metadata: {}
39
39
  post_install_message:
40
40
  rdoc_options: []
41
41
  require_paths:
42
42
  - lib
43
43
  required_ruby_version: !ruby/object:Gem::Requirement
44
- none: false
45
44
  requirements:
46
- - - ! '>='
45
+ - - '>='
47
46
  - !ruby/object:Gem::Version
48
47
  version: '0'
49
48
  required_rubygems_version: !ruby/object:Gem::Requirement
50
- none: false
51
49
  requirements:
52
- - - ! '>='
50
+ - - '>='
53
51
  - !ruby/object:Gem::Version
54
52
  version: '0'
55
53
  requirements: []
56
54
  rubyforge_project:
57
- rubygems_version: 1.8.23
55
+ rubygems_version: 2.0.14
58
56
  signing_key:
59
- specification_version: 3
57
+ specification_version: 4
60
58
  summary: Parse and evaluate mongo queries in Ruby
61
59
  test_files:
62
60
  - test/mongo-parser-rb/field_test.rb