irjudson-dm-persevere-adapter 0.0.2 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +0 -1
- data/Rakefile +5 -5
- data/lib/persevere_adapter.rb +86 -67
- data/lib/persevere_adapter/version.rb +1 -1
- metadata +1 -3
- data/TODO +0 -0
data/README.txt
CHANGED
data/Rakefile
CHANGED
@@ -13,7 +13,7 @@ require ROOT + 'lib/persevere_adapter/version'
|
|
13
13
|
GEM_NAME = 'dm-persevere-adapter'
|
14
14
|
GEM_VERSION = DataMapper::PersevereAdapter::VERSION
|
15
15
|
|
16
|
-
Hoe.
|
16
|
+
Hoe.spec(GEM_NAME) do |p|
|
17
17
|
p.developer('Ivan R. Judson', 'irjudson [a] gmail [d] com')
|
18
18
|
|
19
19
|
p.description = 'A DataMapper Adapter for persevere'
|
@@ -21,14 +21,14 @@ Hoe.new(GEM_NAME, GEM_VERSION) do |p|
|
|
21
21
|
p.url = 'http://github.com/USERNAME/dm-persevere-adapter'
|
22
22
|
|
23
23
|
p.clean_globs |= %w[ log pkg coverage ]
|
24
|
-
p.spec_extras = {
|
25
|
-
:has_rdoc => true,
|
24
|
+
p.spec_extras = {
|
25
|
+
:has_rdoc => true,
|
26
26
|
:extra_rdoc_files => %w[ README.txt LICENSE.txt TODO History.txt ]
|
27
27
|
}
|
28
28
|
|
29
29
|
p.extra_deps = [
|
30
|
-
['dm-core', "~> 0.9.10"],
|
31
|
-
['extlib', "~> 0.9.10"],
|
30
|
+
['dm-core', "~> 0.9.10"],
|
31
|
+
['extlib', "~> 0.9.10"],
|
32
32
|
['persevere', "~> 1.0.0"]
|
33
33
|
]
|
34
34
|
|
data/lib/persevere_adapter.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
gem 'dm-core', '~> 0.9.10'
|
2
|
-
require 'dm-core'
|
3
1
|
require 'rubygems'
|
2
|
+
require 'dm-core'
|
4
3
|
require 'extlib'
|
5
4
|
require 'json'
|
6
5
|
require 'persevere'
|
@@ -52,7 +51,17 @@ module DataMapper
|
|
52
51
|
# exceptions when there's a problem
|
53
52
|
|
54
53
|
if response.code == "201"# good:
|
55
|
-
|
54
|
+
rsrc_hash = JSON.parse(response.body)
|
55
|
+
# Typecast attributes, DM expects them properly cast
|
56
|
+
resource.model.properties.each do |prop|
|
57
|
+
value = rsrc_hash[prop.field.to_s]
|
58
|
+
if !value.nil?
|
59
|
+
rsrc_hash[prop.field.to_s] = prop.typecast(value)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
resource.id = rsrc_hash["id"]
|
64
|
+
|
56
65
|
created += 1
|
57
66
|
else
|
58
67
|
return false
|
@@ -82,14 +91,14 @@ module DataMapper
|
|
82
91
|
# @api semipublic
|
83
92
|
def update(attributes, query)
|
84
93
|
updated = 0
|
85
|
-
puts "In Update A: #{attributes} Q: #{query.conditions.inspect}"
|
86
|
-
resources = read_many(query)
|
87
|
-
puts "Resources found: #{resources}"
|
88
|
-
resources.each do |resource|
|
89
|
-
key = resource.class.key(self.name).map do |property|
|
90
|
-
resource.instance_variable_get(property.instance_variable_name)
|
91
|
-
end
|
92
94
|
|
95
|
+
if ! query.is_a?(DataMapper::Query)
|
96
|
+
resources = [query].flatten
|
97
|
+
else
|
98
|
+
resources = read_many(query)
|
99
|
+
end
|
100
|
+
|
101
|
+
resources.each do |resource|
|
93
102
|
tblname = Extlib::Inflection.classify(resource.class).pluralize
|
94
103
|
path = "/#{tblname}/#{resource.id}"
|
95
104
|
|
@@ -120,7 +129,8 @@ module DataMapper
|
|
120
129
|
# @api semipublic
|
121
130
|
|
122
131
|
def read_one(query)
|
123
|
-
read_many(query)
|
132
|
+
results = read_many(query)
|
133
|
+
results[0,1]
|
124
134
|
end
|
125
135
|
|
126
136
|
##
|
@@ -137,8 +147,7 @@ module DataMapper
|
|
137
147
|
# @api semipublic
|
138
148
|
def read_many(query)
|
139
149
|
resources = Array.new
|
140
|
-
|
141
|
-
json_query = make_json_query(query.conditions)
|
150
|
+
json_query = make_json_query(query)
|
142
151
|
|
143
152
|
tblname = Extlib::Inflection.classify(query.model).pluralize
|
144
153
|
path = "/#{tblname}/#{json_query}"
|
@@ -147,18 +156,22 @@ module DataMapper
|
|
147
156
|
|
148
157
|
if response.code == "200"
|
149
158
|
results = JSON.parse(response.body)
|
150
|
-
results.each do |
|
151
|
-
|
152
|
-
|
159
|
+
results.each do |rsrc_hash|
|
160
|
+
# Typecast attributes, DM expects them properly cast
|
161
|
+
query.model.properties.each do |prop|
|
162
|
+
value = rsrc_hash[prop.field.to_s]
|
163
|
+
if !value.nil?
|
164
|
+
rsrc_hash[prop.field.to_s] = prop.typecast(value)
|
165
|
+
end
|
153
166
|
end
|
154
|
-
resources << query.model.load(values, query)
|
155
167
|
end
|
168
|
+
|
169
|
+
resources = query.model.load(results, query)
|
156
170
|
else
|
157
171
|
return false
|
158
172
|
end
|
159
173
|
|
160
|
-
|
161
|
-
resources
|
174
|
+
query.filter_records(resources)
|
162
175
|
end
|
163
176
|
|
164
177
|
alias :read :read_many
|
@@ -175,12 +188,14 @@ module DataMapper
|
|
175
188
|
# @api semipublic
|
176
189
|
def delete(query)
|
177
190
|
deleted = 0
|
178
|
-
resources = read_many(query)
|
179
|
-
resources.each do |resource|
|
180
|
-
key = resource.class.key(self.name).map do |property|
|
181
|
-
resource.instance_variable_get(property.instance_variable_name)
|
182
|
-
end
|
183
191
|
|
192
|
+
if ! query.is_a?(DataMapper::Query)
|
193
|
+
resources = [query].flatten
|
194
|
+
else
|
195
|
+
resources = read_many(query)
|
196
|
+
end
|
197
|
+
|
198
|
+
resources.each do |resource|
|
184
199
|
tblname = Extlib::Inflection.classify(resource.class).pluralize
|
185
200
|
path = "/#{tblname}/#{resource.id}"
|
186
201
|
|
@@ -213,7 +228,23 @@ module DataMapper
|
|
213
228
|
|
214
229
|
def initialize(name, uri_or_options)
|
215
230
|
super
|
216
|
-
|
231
|
+
|
232
|
+
if uri_or_options.class
|
233
|
+
@identity_maps = {}
|
234
|
+
end
|
235
|
+
|
236
|
+
@options = Hash.new
|
237
|
+
|
238
|
+
uri_or_options.each do |k,v|
|
239
|
+
@options[k.to_sym] = v
|
240
|
+
end
|
241
|
+
|
242
|
+
@options[:scheme] = @options[:adapter]
|
243
|
+
@options.delete(:scheme)
|
244
|
+
|
245
|
+
uri = URI::HTTP.build(@options).to_s
|
246
|
+
|
247
|
+
@persevere = Persevere.new(uri)
|
217
248
|
@resource_naming_convention = NamingConventions::Resource::Underscored
|
218
249
|
@identity_maps = {}
|
219
250
|
@classes = []
|
@@ -235,22 +266,6 @@ module DataMapper
|
|
235
266
|
end
|
236
267
|
end
|
237
268
|
|
238
|
-
def make_uri(uri_or_options)
|
239
|
-
if uri_or_options.is_a?(String)
|
240
|
-
begin
|
241
|
-
URI.parse(uri_or_options)
|
242
|
-
return uri_or_options.to_s
|
243
|
-
rescue URI::InvalidURIError => e
|
244
|
-
puts "Error parsing persevere URI: ", e
|
245
|
-
end
|
246
|
-
elsif uri_or_options.is_a?(Hash)
|
247
|
-
nh = uri_or_options.dup
|
248
|
-
nh[:scheme] = nh[:adapter]
|
249
|
-
nh.delete(:scheme)
|
250
|
-
return URI::HTTP.build(nh).to_s
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
269
|
##
|
255
270
|
# Convert a DataMapper Resource to a JSON.
|
256
271
|
#
|
@@ -273,37 +288,41 @@ module DataMapper
|
|
273
288
|
#
|
274
289
|
# @api semipublic
|
275
290
|
|
276
|
-
def make_json_query(
|
291
|
+
def make_json_query(query)
|
277
292
|
query_terms = Array.new
|
293
|
+
|
294
|
+
conditions = query.conditions
|
295
|
+
|
278
296
|
conditions.each do |condition|
|
279
|
-
|
280
|
-
if
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
when :eql
|
287
|
-
query_terms << "#{condition[1].field()}=#{value}"
|
288
|
-
when :lt
|
289
|
-
query_terms << "#{condition[1].field()}<#{value}"
|
290
|
-
when :gt
|
291
|
-
query_terms << "#{condition[1].field()}>#{value}"
|
292
|
-
when :lte
|
293
|
-
query_terms << "#{condition[1].field()}<=#{value}"
|
294
|
-
when :gte
|
295
|
-
query_terms << "#{condition[1].field()}=>#{value}"
|
296
|
-
when :not
|
297
|
-
query_terms << "#{condition[1].field()}!=#{value}"
|
298
|
-
when :like
|
299
|
-
if condition[2].is_a?(String)
|
300
|
-
query_terms << "#{condition[1].field()}~'*#{condition[2].to_s}*'"
|
297
|
+
operator, property, bind_value = condition
|
298
|
+
if ! property.nil? && !bind_value.nil?
|
299
|
+
v = property.typecast(bind_value)
|
300
|
+
if v.is_a?(String)
|
301
|
+
value = "'#{bind_value}'"
|
302
|
+
else
|
303
|
+
value = "#{bind_value}"
|
301
304
|
end
|
302
|
-
|
303
|
-
|
305
|
+
|
306
|
+
query_terms << case operator
|
307
|
+
when :eql then "#{property.field()}=#{value}"
|
308
|
+
when :lt then "#{property.field()}<#{value}"
|
309
|
+
when :gt then "#{property.field()}>#{value}"
|
310
|
+
when :lte then "#{property.field()}<=#{value}"
|
311
|
+
when :gte then "#{property.field()}=>#{value}"
|
312
|
+
when :not then "#{property.field()}!=#{value}"
|
313
|
+
when :like then "#{property.field()}~'*#{value}*'"
|
314
|
+
else puts "Unknown condition: #{operator}"
|
315
|
+
end
|
304
316
|
end
|
305
317
|
end
|
306
|
-
|
318
|
+
|
319
|
+
if query_terms.length != 0
|
320
|
+
query = "?#{query_terms.join("&")}"
|
321
|
+
else
|
322
|
+
query = ""
|
323
|
+
end
|
324
|
+
|
325
|
+
query
|
307
326
|
end
|
308
327
|
end
|
309
328
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: irjudson-dm-persevere-adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ivan R. Judson
|
@@ -42,7 +42,6 @@ extensions: []
|
|
42
42
|
extra_rdoc_files:
|
43
43
|
- README.txt
|
44
44
|
- LICENSE.txt
|
45
|
-
- TODO
|
46
45
|
- History.txt
|
47
46
|
files:
|
48
47
|
- History.txt
|
@@ -50,7 +49,6 @@ files:
|
|
50
49
|
- Manifest.txt
|
51
50
|
- README.txt
|
52
51
|
- Rakefile
|
53
|
-
- TODO
|
54
52
|
- lib/persevere_adapter.rb
|
55
53
|
- lib/persevere_adapter/version.rb
|
56
54
|
- spec/integration/persevere_adapter_spec.rb
|
data/TODO
DELETED
File without changes
|