mongo-parser-rb 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.yardopts +1 -0
- data/lib/mongo-parser-rb/query.rb +15 -0
- data/lib/mongo-parser-rb/query/expression.rb +4 -6
- data/lib/mongo-parser-rb/version.rb +1 -1
- data/test/mongo-parser-rb/query_test.rb +14 -1
- metadata +3 -2
data/.gitignore
CHANGED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
-m markdown lib/**/*.rb - README.md
|
@@ -1,19 +1,34 @@
|
|
1
1
|
module MongoParserRB
|
2
2
|
class Query
|
3
3
|
|
4
|
+
# Parse a query, returning an initialized MongoParserRB::Query object.
|
5
|
+
#
|
6
|
+
# @param raw_query [Hash] the hash to parse
|
7
|
+
# @return [MongoParserRB::Query] initialized query object
|
4
8
|
def self.parse(raw_query)
|
5
9
|
new(raw_query).parse!
|
6
10
|
end
|
7
11
|
|
12
|
+
# Use {MongoParserRB::Query.parse}.
|
13
|
+
# @visibility private
|
8
14
|
def initialize(raw_query)
|
9
15
|
@raw_query = raw_query
|
10
16
|
end
|
11
17
|
|
18
|
+
# @visibility private
|
12
19
|
def parse!
|
13
20
|
@expression_tree = parse_root_expression(@raw_query)
|
14
21
|
self
|
15
22
|
end
|
16
23
|
|
24
|
+
# Check if a document matches a query.
|
25
|
+
#
|
26
|
+
# q = MongoParserRB::Query.parse(:x => {:$gt => 3})
|
27
|
+
# q.matches_document?(:x => 4) => true
|
28
|
+
# q.matches_document?(:x => 3) => false
|
29
|
+
#
|
30
|
+
# @param document [Hash] The document to check
|
31
|
+
# @return [Boolean]
|
17
32
|
def matches_document?(document)
|
18
33
|
raise NotParsedError, "Query not parsed (run parse!)" if @expression_tree.nil?
|
19
34
|
@expression_tree.evaluate(document)
|
@@ -6,13 +6,13 @@ module MongoParserRB
|
|
6
6
|
|
7
7
|
def conjunction_operators
|
8
8
|
@conjunction_operators ||= [
|
9
|
-
:$and,
|
9
|
+
:$and,
|
10
10
|
:$or
|
11
11
|
]
|
12
12
|
end
|
13
13
|
|
14
14
|
def conjunction_operator?(operator)
|
15
|
-
conjunction_operators.include?(operator)
|
15
|
+
conjunction_operators.include?(operator)
|
16
16
|
end
|
17
17
|
|
18
18
|
def negative_equality_operators
|
@@ -47,7 +47,7 @@ module MongoParserRB
|
|
47
47
|
@operator = operator
|
48
48
|
|
49
49
|
if Expression.conjunction_operator? @operator
|
50
|
-
@arguments = args[0]
|
50
|
+
@arguments = args[0]
|
51
51
|
else
|
52
52
|
@field = Field.new(args[0])
|
53
53
|
@arguments = args[1]
|
@@ -103,7 +103,6 @@ module MongoParserRB
|
|
103
103
|
|
104
104
|
def evaluate_equality(document)
|
105
105
|
value_for_field = @field.value_in_document(document)
|
106
|
-
return false if !value_for_field && !@field.in_document?(document)
|
107
106
|
|
108
107
|
case @operator
|
109
108
|
when :$eq
|
@@ -128,7 +127,6 @@ module MongoParserRB
|
|
128
127
|
end
|
129
128
|
end
|
130
129
|
end
|
131
|
-
|
132
130
|
end
|
133
131
|
end
|
134
|
-
end
|
132
|
+
end
|
@@ -85,7 +85,7 @@ class QueryTest < MiniTest::Unit::TestCase
|
|
85
85
|
|
86
86
|
|
87
87
|
def test_or
|
88
|
-
query = MongoParserRB::Query.parse(:$or => [
|
88
|
+
query = MongoParserRB::Query.parse(:$or => [
|
89
89
|
{:string_key => "abc"},
|
90
90
|
{:integer_key => {:$gt => 5}}
|
91
91
|
])
|
@@ -133,6 +133,19 @@ class QueryTest < MiniTest::Unit::TestCase
|
|
133
133
|
query = MongoParserRB::Query.parse(:string_key => nil)
|
134
134
|
assert query.matches_document?(:string_key => nil)
|
135
135
|
refute query.matches_document?(:string_key => 'hey')
|
136
|
+
assert query.matches_document?(:something_else => "hello")
|
137
|
+
assert query.matches_document?({})
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_ne_with_nil
|
141
|
+
query = MongoParserRB::Query.parse(:string_key => {:$ne => 'cheese'})
|
142
|
+
assert query.matches_document?(:string_key => nil)
|
143
|
+
assert query.matches_document?({})
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_no_key_in_unknown
|
147
|
+
query = MongoParserRB::Query.parse(:string_key => {:$in=>[nil, "", "null", "nil"]})
|
148
|
+
assert query.matches_document?(:something_else => "hello")
|
136
149
|
end
|
137
150
|
|
138
151
|
def test_regex_eq
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo-parser-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
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-05-04 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Parse and evaluate mongo queries in Ruby
|
15
15
|
email:
|
@@ -19,6 +19,7 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- .gitignore
|
22
|
+
- .yardopts
|
22
23
|
- Gemfile
|
23
24
|
- LICENSE.txt
|
24
25
|
- README.md
|