chill 4 → 5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/library/chill.rb +70 -23
  2. metadata +10 -10
@@ -37,8 +37,12 @@ module ChillDB
37
37
 
38
38
  # get or make a document with a particular id/name, or just a blank new one
39
39
  def document id = false
40
- if id
41
- ChillDB::Document.load(@@database, id)
40
+ if id.respond_to? :to_ary
41
+ list = id.to_ary.map { |i| i.to_s }
42
+ response = @@database.http('_all_docs?include_docs=true').post({ keys: list }.to_json)
43
+ ChillDB::List.load(JSON.parse(response), database: @@database)
44
+ elsif id.respond_to? :to_str
45
+ ChillDB::Document.load(@@database, id.to_str)
42
46
  else
43
47
  ChillDB::Document.new(@@database)
44
48
  end
@@ -52,6 +56,12 @@ module ChillDB
52
56
  return hash.commit! if hash.is_a? ChillDB::Document
53
57
  return ChillDB::Document.new(@@database, hash).commit!
54
58
  end
59
+
60
+ def open *args
61
+ headers = { accept: '*/*' }
62
+ headers.merge! args.pop if args.last.respond_to? :to_hash
63
+ @@database.http(args.map { |i| URI.escape(i, /[^a-z0-9_.-]/i) }.join('/'), headers)
64
+ end
55
65
  end
56
66
 
57
67
  class ChillDB::Database
@@ -80,8 +90,8 @@ class ChillDB::Database
80
90
  #def revs_limit=(v); http('_revs_limit').put(v.to_s); end
81
91
 
82
92
  # grab a RestClient http resource for this database
83
- def http resource
84
- RestClient::Resource.new((@url + resource).to_s, :headers => {accept: 'application/json', content_type: 'application/json'}) { |r| r }
93
+ def http resource, headers = {}
94
+ RestClient::Resource.new((@url + resource).to_s, headers: {accept: 'application/json', content_type: 'application/json'}.merge(headers)) { |r| r }
85
95
  end
86
96
 
87
97
  private
@@ -110,7 +120,7 @@ class ChillDB::IndifferentHash < Hash
110
120
 
111
121
  # getters and setters for hash items
112
122
  def method_missing name, *args
113
- return self[name.to_s] if self[name.to_s]
123
+ return self[name.to_s] if self.key? name.to_s
114
124
  return self[name.to_s[0...-1]] = args.first if name.to_s.end_with? '=' and args.length == 1
115
125
  super
116
126
  end
@@ -153,7 +163,7 @@ class ChillDB::IndifferentHash < Hash
153
163
  end
154
164
 
155
165
  def normalize thing
156
- return ChillDB::IndifferentHash.new.replace(thing) if thing.is_a? Hash
166
+ return ChillDB::IndifferentHash.new.replace(thing) if thing.respond_to? :to_hash unless thing.is_a? self.class
157
167
  return thing.map { |i| normalize(i) } if thing.is_a? Array
158
168
  return thing;
159
169
  end
@@ -234,6 +244,11 @@ class ChillDB::Document < ChillDB::IndifferentHash
234
244
  json = JSON.parse(request.body)
235
245
  json['_revisions']['ids']
236
246
  end
247
+
248
+ # for integration with url routers in webapps, to_param defaults to _id - which you can use straight up in urls
249
+ def to_param
250
+ self['_id']
251
+ end
237
252
  end
238
253
 
239
254
 
@@ -244,9 +259,36 @@ end
244
259
  class ChillDB::List < Array
245
260
  attr_accessor :total_rows, :offset, :database
246
261
 
262
+ # creates a new List from a couchdb response
263
+ def self.load list, extras = {}
264
+ new_list = self.new
265
+
266
+ raise "#{list['error']} - #{list['reason']}" if list['error']
267
+ [extras, list].each do |properties|
268
+ properties.each do |key, value|
269
+ new_list.send("#{key}=", value)
270
+ end
271
+ end
272
+
273
+ return new_list
274
+ end
275
+
247
276
  # store rows nicely in mah belleh
248
277
  def rows=(arr)
249
- self.replace arr.map { |item| ChillDB::IndifferentHash.new.replace(item) }
278
+ self.replace(arr.map { |item|
279
+ if item['value'].is_a? Hash
280
+ if item['value'].respond_to?(:[]) && item['value']['_id']
281
+ item['value']['_id'] ||= item['id']
282
+ item['value'] = ChillDB::Document.new(@database, item['value'])
283
+ else
284
+ item['value'] = ChillDB::IndifferentHash.new.replace(item['value'])
285
+ end
286
+ end
287
+
288
+ item['doc'] = ChillDB::Document.new(@database, item['doc']) if item['doc']
289
+
290
+ item
291
+ })
250
292
  end
251
293
 
252
294
  # we are the rows!
@@ -267,13 +309,27 @@ class ChillDB::List < Array
267
309
  end
268
310
 
269
311
  def docs
270
- self.map { |i| i['doc'] }
312
+ self.map { |i| i['doc'] }
271
313
  end
272
314
 
273
315
  def each_pair &proc
274
316
  self.each { |item| proc.call(item['key'], item['value']) }
275
317
  end
276
318
 
319
+ # gets an item by id value
320
+ def id value
321
+ self.find { |i| i['id'] == value }['doc']
322
+ end
323
+
324
+ def key value
325
+ self.find { |i| i['key'] == value }
326
+ end
327
+
328
+ def [] key
329
+ return key(key)['doc'] unless key.respond_to? :to_int
330
+ super
331
+ end
332
+
277
333
  # make a regular ruby hash version
278
334
  # if you want docs as values instead of emitted values, use to_h(:doc)
279
335
  def to_h value = :value
@@ -285,16 +341,15 @@ class ChillDB::List < Array
285
341
 
286
342
  return hash
287
343
  end
288
- alias_method :to_hash, :to_h
289
344
 
290
345
  # remove all the documents in this list from the database
291
346
  def delete_all!
292
- each { |item|
293
- raise "Not all documents listed are emitted as values in this view" if item.id != item.value._id
294
- }
347
+ each do |item|
348
+ raise "Some (all?) items in list do not contain _rev properties in their values" unless item['value']['_rev']
349
+ end
295
350
 
296
351
  request = { docs: map { |item|
297
- { _id: item.value._id, _rev: item.value._rev, _deleted: true }
352
+ { _id: item['value']['_id'] || item['id'], _rev: item['value']['_rev'], _deleted: true }
298
353
  } }
299
354
 
300
355
  response = JSON.parse @database.http("_bulk_docs").post(request.to_json)
@@ -364,17 +419,9 @@ class ChillDB::Design
364
419
  opts = options.map { |key, value| "#{URI.escape(key.to_s)}=#{URI.escape(value.to_s)}" }.join('&')
365
420
  url = "_design/#{URI.escape @name}/_view/#{URI.escape view.to_s}?#{opts}"
366
421
  response = @database.http(url).get()
367
- json = JSON.parse response.body
368
- raise "#{json['error']} - #{json['reason']} @ #{url}" if json['error']
369
-
370
- # put the results in to a QueryResults object - a glorified array
371
- results = ChillDB::List.new
372
- results.database = @database
373
- json.each do |key, value|
374
- results.send("#{key}=", value)
375
- end
376
422
 
377
- return results
423
+ # put the results in to a QueryResults object - a glorified array
424
+ return ChillDB::List.load(JSON.parse(response.body), database: @database)
378
425
  end
379
426
 
380
427
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chill
3
3
  version: !ruby/object:Gem::Version
4
- version: '4'
4
+ version: '5'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-03 00:00:00.000000000Z
12
+ date: 2011-10-31 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &70262269192360 !ruby/object:Gem::Requirement
16
+ requirement: &70177568025240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 1.6.0
21
+ version: 1.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70262269192360
24
+ version_requirements: *70177568025240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rest-client
27
- requirement: &70262269191800 !ruby/object:Gem::Requirement
27
+ requirement: &70177568024660 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.6.7
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70262269191800
35
+ version_requirements: *70177568024660
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: uuid
38
- requirement: &70262269191340 !ruby/object:Gem::Requirement
38
+ requirement: &70177568024180 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 2.3.4
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70262269191340
46
+ version_requirements: *70177568024180
47
47
  description: A little library to talk to a couchdb. I made it skinny, because couchdb
48
48
  is very simple. I think that's a good thing.
49
49
  email: a@creativepony.com
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  version: '0'
73
73
  requirements: []
74
74
  rubyforge_project:
75
- rubygems_version: 1.8.5
75
+ rubygems_version: 1.8.10
76
76
  signing_key:
77
77
  specification_version: 3
78
78
  summary: A tiny plug to hook ruby in to couchdb