mongo-query 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,168 @@
1
+ grammar MongoQueryGrammar
2
+
3
+ rule query
4
+ space and_expression space {
5
+ def to_mongo
6
+ and_expression.to_mongo
7
+ end
8
+ }
9
+ end
10
+
11
+ rule and_expression
12
+ binary_expression space and_op space and_expression {
13
+ def to_mongo
14
+ res = binary_expression.to_mongo
15
+ new_res = and_expression.to_mongo
16
+ res.each do |key,value|
17
+ if new_res.has_key?(key)
18
+ value.merge!(new_res.delete(key))
19
+ end
20
+ end
21
+ res.merge(new_res)
22
+ end
23
+ }
24
+ / binary_expression
25
+ end
26
+
27
+ rule and_op
28
+ 'and' / 'AND'
29
+ end
30
+
31
+ rule binary_expression
32
+ field_name space '=' space literal {
33
+ def to_mongo
34
+ {field_name.to_mongo=>literal.to_mongo}
35
+ end
36
+ }
37
+ /
38
+ (
39
+ field_name space operator:comp_op space value:literal
40
+ / field_name space operator:list_op space value:list
41
+ / field_name space operator:size_op space value:integer
42
+ / field_name space operator:exists_op space value:boolean
43
+ ) {
44
+ def to_mongo
45
+ {field_name.to_mongo=>{ operator.to_mongo => value.to_mongo }}
46
+ end
47
+ }
48
+ end
49
+
50
+ rule list
51
+ '(' space list_items space ')' {
52
+ def to_mongo
53
+ list_items.to_mongo
54
+ end
55
+ }
56
+ end
57
+
58
+ rule list_items
59
+ literal space ',' space list_items {
60
+ def to_mongo
61
+ [literal.to_mongo] + [list_items.to_mongo].flatten
62
+ end
63
+ }
64
+ / literal
65
+ end
66
+
67
+ rule comp_op
68
+ '<=' { def to_mongo; '$lte'; end}
69
+ / '<' { def to_mongo; '$lt'; end}
70
+ / '>=' { def to_mongo; '$gte'; end}
71
+ / '>' { def to_mongo; '$gt'; end}
72
+ / '!=' { def to_mongo; '$ne'; end}
73
+ end
74
+
75
+ rule list_op
76
+ in_op / not_in_op / all_op
77
+ end
78
+
79
+ rule in_op
80
+ 'in' / 'IN' {def to_mongo; "$in"; end }
81
+ end
82
+
83
+ rule not_in_op
84
+ 'not in' / 'NOT IN' {def to_mongo; "$nin"; end }
85
+ end
86
+
87
+ rule all_op
88
+ 'all' / 'ALL' {def to_mongo; "$all"; end }
89
+ end
90
+
91
+ rule size_op
92
+ 'size' / 'SIZE' {def to_mongo; "$size"; end }
93
+ end
94
+
95
+ rule exists_op
96
+ 'exists' / 'EXISTS' {def to_mongo; "$exists"; end }
97
+ end
98
+
99
+ rule literal
100
+ string / number / boolean / regex
101
+ end
102
+
103
+ rule number
104
+ float / integer
105
+ end
106
+
107
+ rule float
108
+ [-+]? [0-9]+ '.' [0-9]+ {
109
+ def to_mongo
110
+ text_value.to_f
111
+ end
112
+ }
113
+ end
114
+
115
+ rule integer
116
+ [-+]? [0-9]+ {
117
+ def to_mongo
118
+ text_value.to_i
119
+ end
120
+ }
121
+ end
122
+
123
+ rule string
124
+ (double_quote_string / single_quote_string) {
125
+ def to_mongo
126
+ text_value[1...-1]
127
+ end
128
+ }
129
+ end
130
+
131
+ rule double_quote_string
132
+ '"' ('\"' / '\\' / [^"])* '"'
133
+ end
134
+
135
+ rule single_quote_string
136
+ "'" ("\\'" / '\\' / [^'])* "'"
137
+ end
138
+
139
+ rule boolean
140
+ 'true' { def to_mongo; true; end }
141
+ / 'false' { def to_mongo; false; end }
142
+ end
143
+
144
+ rule space
145
+ [ \n\t]*
146
+ end
147
+
148
+ rule regex
149
+ '/' [^/]+ '/' {
150
+ def to_mongo
151
+ Regexp.new(text_value[1...-1])
152
+ end
153
+ }
154
+ end
155
+
156
+ rule field_name
157
+ (single_field_name '.' field_name / single_field_name) {
158
+ def to_mongo
159
+ text_value
160
+ end
161
+ }
162
+ end
163
+
164
+ rule single_field_name
165
+ [a-zA-z0-9_-]+
166
+ end
167
+
168
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo-query
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hayes Davis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-23 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: treetop
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Mongo-query compiles SQL-like where clauses into Mongo query 'documents'
26
+ email: hayes@appozite.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ files:
34
+ - README.rdoc
35
+ - lib/mongo-query.rb
36
+ - lib/mongo-query/mongo_query.rb
37
+ - lib/mongo-query/mongo_query_grammar.rb
38
+ - lib/mongo-query/mongo_query_grammar.treetop
39
+ has_rdoc: true
40
+ homepage: http://github.com/hayesdavis/mongo-query
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --inline-source
46
+ - --charset=UTF-8
47
+ - --main=README.rdoc
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project: mongo-query
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: Mongo-query compiles SQL-like where clauses into Mongo query 'documents'
69
+ test_files: []
70
+