dm-parse 0.3.6 → 0.3.7
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/VERSION +1 -1
- data/dm-parse.gemspec +2 -2
- data/lib/adapters/parse/query.rb +4 -53
- data/lib/adapters/parse_adapter.rb +82 -52
- data/spec/parse_adapter_spec.rb +5 -0
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.7
|
data/dm-parse.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "dm-parse"
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.7"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Zhi-Qiang Lei"]
|
12
|
-
s.date = "2012-07-
|
12
|
+
s.date = "2012-07-04"
|
13
13
|
s.description = "An extension to make DataMapper working on Parse.com"
|
14
14
|
s.email = "zhiqiang.lei@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/adapters/parse/query.rb
CHANGED
@@ -7,7 +7,7 @@ module DataMapper
|
|
7
7
|
@value = value
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def as_json
|
11
11
|
{ key_name => @value }
|
12
12
|
end
|
13
13
|
|
@@ -17,7 +17,7 @@ module DataMapper
|
|
17
17
|
end
|
18
18
|
|
19
19
|
class Eql < Comparison
|
20
|
-
def
|
20
|
+
def as_json
|
21
21
|
@value
|
22
22
|
end
|
23
23
|
end
|
@@ -58,57 +58,8 @@ module DataMapper
|
|
58
58
|
result.join
|
59
59
|
end
|
60
60
|
|
61
|
-
def
|
62
|
-
{key_name => @value.source}.tap { |value| value["$options"] = options if options.present? }
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
class And
|
67
|
-
def initialize
|
68
|
-
@conditions = Mash.new
|
69
|
-
end
|
70
|
-
|
71
|
-
def add(field, comparison)
|
72
|
-
@conditions[field] ||= []
|
73
|
-
@conditions[field] << comparison
|
74
|
-
self
|
75
|
-
end
|
76
|
-
|
77
|
-
def combine(comparisons)
|
78
|
-
groups = comparisons.group_by { |comparison| comparison.is_a? Eql }
|
79
|
-
equals = groups[true]
|
80
|
-
others = groups[false]
|
81
|
-
|
82
|
-
if equals.present? && others.present?
|
83
|
-
raise "Parse Query: cannot combine Eql with others"
|
84
|
-
elsif equals.present?
|
85
|
-
raise "can only use one EqualToComparison for a field" unless equals.size == 1
|
86
|
-
equals.first.build
|
87
|
-
elsif others.present?
|
88
|
-
others.inject({}) do |result, comparison|
|
89
|
-
result.merge! comparison.build
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
def build
|
95
|
-
@conditions.inject({}) do |result, (field, comparisons)|
|
96
|
-
result.merge! field => combine(comparisons)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
class Or
|
102
|
-
def initialize
|
103
|
-
@queries = []
|
104
|
-
end
|
105
|
-
|
106
|
-
def add(query)
|
107
|
-
@queries << query
|
108
|
-
end
|
109
|
-
|
110
|
-
def build
|
111
|
-
{"$or" => @queries.map { |query| query.build } }
|
61
|
+
def as_json
|
62
|
+
{ key_name => @value.source }.tap { |value| value["$options"] = options if options.present? }
|
112
63
|
end
|
113
64
|
end
|
114
65
|
|
@@ -163,82 +163,112 @@ module DataMapper
|
|
163
163
|
conditions = query.conditions
|
164
164
|
return nil if conditions.blank?
|
165
165
|
|
166
|
-
|
166
|
+
result = {}
|
167
|
+
translate(conditions, result)
|
168
|
+
finalize result
|
169
|
+
end
|
170
|
+
|
171
|
+
def translate(condition, result)
|
172
|
+
case condition
|
173
|
+
when RegexpComparison
|
174
|
+
translate_for(result, condition, Regex)
|
175
|
+
when EqualToComparison
|
176
|
+
translate_for(result, condition, Eql)
|
177
|
+
when GreaterThanComparison
|
178
|
+
translate_for(result, condition, Gt)
|
179
|
+
when GreaterThanOrEqualToComparison
|
180
|
+
translate_for(result, condition, Gte)
|
181
|
+
when LessThanComparison
|
182
|
+
translate_for(result, condition, Lt)
|
183
|
+
when LessThanOrEqualToComparison
|
184
|
+
translate_for(result, condition, Lte)
|
185
|
+
when InclusionComparison
|
186
|
+
translate_for(result, condition, In)
|
167
187
|
when NotOperation
|
168
|
-
|
169
|
-
feed_reversely(parse_query, conditions)
|
188
|
+
condition.each { |c| translate_reversely c, result }
|
170
189
|
when AndOperation
|
171
|
-
|
172
|
-
feed_directly(parse_query, conditions)
|
190
|
+
condition.each { |c| translate c, result }
|
173
191
|
when OrOperation
|
174
|
-
|
175
|
-
|
192
|
+
result["$or"] ||= []
|
193
|
+
result["$or"] = result["$or"] + condition.map do |c|
|
194
|
+
r = {}
|
195
|
+
translate c, r
|
196
|
+
r
|
197
|
+
end
|
198
|
+
else
|
199
|
+
raise NotImplementedError
|
176
200
|
end
|
201
|
+
end
|
177
202
|
|
178
|
-
|
203
|
+
def translate_reversely(condition, result)
|
204
|
+
case condition
|
205
|
+
when EqualToComparison
|
206
|
+
translate_for(result, condition, Ne)
|
207
|
+
when GreaterThanComparison
|
208
|
+
translate_for(result, condition, Lte)
|
209
|
+
when GreaterThanOrEqualToComparison
|
210
|
+
translate_for(result, condition, Lt)
|
211
|
+
when LessThanComparison
|
212
|
+
translate_for(result, condition, Gte)
|
213
|
+
when LessThanOrEqualToComparison
|
214
|
+
translate_for(result, condition, Gt)
|
215
|
+
when InclusionComparison
|
216
|
+
translate_for(result, condition, Nin)
|
217
|
+
when NotOperation
|
218
|
+
condition.each { |c| translate c, result }
|
219
|
+
when AndOperation
|
220
|
+
condition.each { |c| translate_reversely c, result }
|
221
|
+
else
|
222
|
+
raise NotImplementedError
|
223
|
+
end
|
179
224
|
end
|
180
225
|
|
181
|
-
def
|
226
|
+
def translate_for(result, condition, comparison_class)
|
182
227
|
subject = condition.subject
|
228
|
+
|
183
229
|
case subject
|
184
230
|
when DataMapper::Property
|
185
|
-
|
186
|
-
|
231
|
+
field = subject.field
|
232
|
+
result[field] ||= []
|
233
|
+
result[field] << comparison_class.new(condition.value)
|
187
234
|
when DataMapper::Associations::OneToMany::Relationship
|
188
235
|
child_key = condition.subject.child_key.first
|
189
|
-
|
236
|
+
result["objectId"] ||= []
|
237
|
+
result["objectId"] << comparison_class.new(condition.value.map { |resource| resource.send child_key.name })
|
190
238
|
when DataMapper::Associations::ManyToOne::Relationship
|
191
239
|
child_key = subject.child_key.first
|
192
|
-
|
240
|
+
field = child_key.field
|
241
|
+
result[field] ||= []
|
242
|
+
result[field] << comparison_class.new(condition.foreign_key_mapping.value)
|
193
243
|
else
|
194
244
|
raise NotImplementedError, "Condition: #{condition}"
|
195
245
|
end
|
196
246
|
end
|
197
247
|
|
198
|
-
def
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
when GreaterThanComparison then feed_for(parse_query, condition, Lte)
|
203
|
-
when GreaterThanOrEqualToComparison then feed_for(parse_query, condition, Lt)
|
204
|
-
when LessThanComparison then feed_for(parse_query, condition, Gte)
|
205
|
-
when LessThanOrEqualToComparison then feed_for(parse_query, condition, Gt)
|
206
|
-
when NotOperation then feed_directly(parse_query, condition)
|
207
|
-
when AndOperation then feed_reversely(parse_query, condition)
|
208
|
-
when InclusionComparison then feed_for(parse_query, condition, Nin)
|
248
|
+
def finalize(queries)
|
249
|
+
queries.inject({}) do |result, (field, comparisons)|
|
250
|
+
if field == "$or"
|
251
|
+
result.merge! field => comparisons.map { |c| finalize c }
|
209
252
|
else
|
210
|
-
|
253
|
+
result.merge! field => combine(comparisons)
|
211
254
|
end
|
212
255
|
end
|
213
256
|
end
|
214
257
|
|
215
|
-
def
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
def feed_with_condition(parse_query, condition)
|
230
|
-
case condition
|
231
|
-
when RegexpComparison then feed_for(parse_query, condition, Regex)
|
232
|
-
when EqualToComparison then feed_for(parse_query, condition, Eql)
|
233
|
-
when GreaterThanComparison then feed_for(parse_query, condition, Gt)
|
234
|
-
when GreaterThanOrEqualToComparison then feed_for(parse_query, condition, Gte)
|
235
|
-
when LessThanComparison then feed_for(parse_query, condition, Lt)
|
236
|
-
when LessThanOrEqualToComparison then feed_for(parse_query, condition, Lte)
|
237
|
-
when InclusionComparison then feed_for(parse_query, condition, In)
|
238
|
-
when NotOperation then feed_reversely(parse_query, condition)
|
239
|
-
when AndOperation then feed_directly(parse_query, condition)
|
240
|
-
else
|
241
|
-
raise NotImplementedError
|
258
|
+
def combine(comparisons)
|
259
|
+
groups = comparisons.group_by { |comparison| comparison.is_a? Eql }
|
260
|
+
equals = groups[true]
|
261
|
+
others = groups[false]
|
262
|
+
|
263
|
+
if equals.present? && others.present?
|
264
|
+
raise "Parse Query: cannot combine Eql with others"
|
265
|
+
elsif equals.present?
|
266
|
+
raise "can only use one EqualToComparison for a field" unless equals.size == 1
|
267
|
+
equals.first.as_json
|
268
|
+
elsif others.present?
|
269
|
+
others.inject({}) do |result, comparison|
|
270
|
+
result.merge! comparison.as_json
|
271
|
+
end
|
242
272
|
end
|
243
273
|
end
|
244
274
|
|
data/spec/parse_adapter_spec.rb
CHANGED
@@ -61,6 +61,11 @@ describe DataMapper::Adapters::ParseAdapter do
|
|
61
61
|
context "when query has :or operation" do
|
62
62
|
let(:query) { (model.all(:rank => 3) + model.all(:rank => 4) + model.all(:rank => 5)).query }
|
63
63
|
it { should eq("$or" => [{"rank" => 3}, {"rank" => 4}, {"rank" => 5}]) }
|
64
|
+
|
65
|
+
context "in :and operation" do
|
66
|
+
let(:query) { (model.all(:title => "x") & (model.all(:rank => 3) + model.all(:rank => 4))).query }
|
67
|
+
it { should eq("title" => "x", "$or" => [{"rank" => 3}, {"rank" => 4}]) }
|
68
|
+
end
|
64
69
|
end
|
65
70
|
|
66
71
|
context "when query has union operator" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-parse
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.7
|
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: 2012-07-
|
12
|
+
date: 2012-07-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dm-core
|
@@ -242,7 +242,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
242
242
|
version: '0'
|
243
243
|
segments:
|
244
244
|
- 0
|
245
|
-
hash:
|
245
|
+
hash: -1860376599775651579
|
246
246
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
247
247
|
none: false
|
248
248
|
requirements:
|