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 CHANGED
@@ -1 +1 @@
1
- 0.3.6
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.6"
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-03"
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 = [
@@ -7,7 +7,7 @@ module DataMapper
7
7
  @value = value
8
8
  end
9
9
 
10
- def build
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 build
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 build
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
- case conditions
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
- parse_query = Parse::Conditions::And.new
169
- feed_reversely(parse_query, conditions)
188
+ condition.each { |c| translate_reversely c, result }
170
189
  when AndOperation
171
- parse_query = Parse::Conditions::And.new
172
- feed_directly(parse_query, conditions)
190
+ condition.each { |c| translate c, result }
173
191
  when OrOperation
174
- parse_query = Parse::Conditions::Or.new
175
- feed_or(parse_query, conditions)
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
- parse_query.build
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 feed_for(parse_query, condition, comparison_class)
226
+ def translate_for(result, condition, comparison_class)
182
227
  subject = condition.subject
228
+
183
229
  case subject
184
230
  when DataMapper::Property
185
- comparison = comparison_class.new condition.value
186
- parse_query.add subject.field, comparison
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
- parse_query.add "objectId", comparison_class.new(condition.value.map { |resource| resource.send child_key.name })
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
- parse_query.add child_key.field, comparison_class.new(condition.foreign_key_mapping.value)
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 feed_reversely(parse_query, conditions)
199
- conditions.each do |condition|
200
- case condition
201
- when EqualToComparison then feed_for(parse_query, condition, Ne)
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
- raise NotImplementedError
253
+ result.merge! field => combine(comparisons)
211
254
  end
212
255
  end
213
256
  end
214
257
 
215
- def feed_directly(parse_query, conditions)
216
- conditions.each do |condition|
217
- feed_with_condition parse_query, condition
218
- end
219
- end
220
-
221
- def feed_or(queries, conditions)
222
- conditions.each do |condition|
223
- parse_query = Parse::Conditions::And.new
224
- feed_with_condition parse_query, condition
225
- queries.add parse_query
226
- end
227
- end
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
 
@@ -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.6
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-03 00:00:00.000000000 Z
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: 4240417428092590610
245
+ hash: -1860376599775651579
246
246
  required_rubygems_version: !ruby/object:Gem::Requirement
247
247
  none: false
248
248
  requirements: