rocking_chair 0.0.3 → 0.0.4

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/LICENSE.txt ADDED
@@ -0,0 +1,15 @@
1
+ /*
2
+ * Copyright (c) 2010 Jonathan Weiss <jw@innerewut.de>
3
+ *
4
+ * Permission to use, copy, modify, and distribute this software for any
5
+ * purpose with or without fee is hereby granted, provided that the above
6
+ * copyright notice and this permission notice appear in all copies.
7
+ *
8
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+ */
data/README.md CHANGED
@@ -45,4 +45,15 @@ Caveats
45
45
  =============
46
46
 
47
47
  At the moment the performance is not as good as it could be as there is a lot of serialization to and from JSON going on.
48
- This will be improved by storing not only the JSON tree but also the Ruby representation of the stored objects.
48
+ This will be improved by storing not only the JSON tree but also the Ruby representation of the stored objects.
49
+
50
+ License
51
+ =============
52
+
53
+ RockingChair is licensed under the OpenBSD / two-clause BSD license, modeled after the ISC license. See LICENSE.txt
54
+
55
+ About
56
+ =============
57
+
58
+ RockingChair was written by [Jonathan Weiss](http://twitter.com/jweiss) to make [Mathias Meyer](http://twitter.com/roidrage) happy.
59
+
@@ -2,10 +2,10 @@ module RockingChair
2
2
  module CouchRestHttpAdapter
3
3
  URL_PARAMETER = /[a-zA-Z0-9\-\_\%]+/
4
4
 
5
- @_RockingChair_debug = true
5
+ @_rocking_chair_debug = true
6
6
 
7
7
  def get(uri, headers={})
8
- puts "GET: #{uri.inspect}: #{headers.inspect}" if @_RockingChair_debug
8
+ puts "GET: #{uri.inspect}: #{headers.inspect}" if @_rocking_chair_debug
9
9
  url, parameters = RockingChair::Server.normalize_url(uri)
10
10
  if url == ''
11
11
  RockingChair::Server.info
@@ -29,7 +29,7 @@ module RockingChair
29
29
  end
30
30
 
31
31
  def post(uri, payload, headers={})
32
- puts "POST: #{uri.inspect}: #{payload.inspect} #{headers.inspect}" if @_RockingChair_debug
32
+ puts "POST: #{uri.inspect}: #{payload.inspect} #{headers.inspect}" if @_rocking_chair_debug
33
33
  url, parameters = RockingChair::Server.normalize_url(uri)
34
34
  if url.match(/\A(#{URL_PARAMETER})\/?\Z/)
35
35
  RockingChair::Server.store($1, nil, payload, parameters)
@@ -41,7 +41,7 @@ module RockingChair
41
41
  end
42
42
 
43
43
  def put(uri, payload, headers={})
44
- puts "PUT: #{uri.inspect}: #{payload.inspect} #{headers.inspect}" if @_RockingChair_debug
44
+ puts "PUT: #{uri.inspect}: #{payload.inspect} #{headers.inspect}" if @_rocking_chair_debug
45
45
  url, parameters = RockingChair::Server.normalize_url(uri)
46
46
  if url.match(/\A(#{URL_PARAMETER})\Z/)
47
47
  RockingChair::Server.create_db(url)
@@ -55,19 +55,21 @@ module RockingChair
55
55
  end
56
56
 
57
57
  def delete(uri, headers={})
58
- puts "DELETE: #{uri.inspect}: #{headers.inspect}" if @_RockingChair_debug
58
+ puts "DELETE: #{uri.inspect}: #{headers.inspect}" if @_rocking_chair_debug
59
59
  url, parameters = RockingChair::Server.normalize_url(uri)
60
60
  if url.match(/\A(#{URL_PARAMETER})\Z/)
61
61
  RockingChair::Server.delete_db(url)
62
62
  elsif url.match(/\A(#{URL_PARAMETER})\/(#{URL_PARAMETER})\Z/)
63
63
  RockingChair::Server.delete($1, $2, parameters)
64
+ elsif url.match(/\A(#{URL_PARAMETER})\/_design\/(#{URL_PARAMETER})\Z/)
65
+ RockingChair::Server.delete($1, "_design/#{$2}", parameters)
64
66
  else
65
67
  raise "DELETE: Unknown url: #{uri.inspect}: #{headers.inspect}"
66
68
  end
67
69
  end
68
70
 
69
71
  def copy(uri, headers)
70
- puts "COPY: #{uri.inspect}: #{headers.inspect}" if @_RockingChair_debug
72
+ puts "COPY: #{uri.inspect}: #{headers.inspect}" if @_rocking_chair_debug
71
73
  url, parameters = RockingChair::Server.normalize_url(uri)
72
74
  if url.match(/\A(#{URL_PARAMETER})\/(#{URL_PARAMETER})\Z/)
73
75
  RockingChair::Server.copy($1, $2, headers.merge(parameters))
@@ -41,6 +41,7 @@ module RockingChair
41
41
  RockingChair::Helper.jsonfy_options(@options, 'key', 'startkey', 'endkey', 'startkey_docid', 'endkey_docid')
42
42
 
43
43
  normalize_view_name
44
+ normalize_descending_options
44
45
  end
45
46
 
46
47
  def find
@@ -80,8 +81,12 @@ module RockingChair
80
81
  end
81
82
 
82
83
  def render_for_all
83
- offset = filter_by_startkey
84
- filter_by_endkey
84
+ #key_size_before_startkey_filter = keys.size
85
+ #filter_items_not_in_range('_id', options['startkey'], nil) if options['startkey']
86
+ #filter_items_not_in_range('_id', options['startkey'], options['endkey']) if options['endkey']
87
+ offset = 0 #key_size_before_startkey_filter - keys.size
88
+ #offset = filter_by_startkey
89
+ #filter_by_endkey
85
90
  filter_by_limit
86
91
 
87
92
  rows = keys.map do |key|
@@ -99,18 +104,21 @@ module RockingChair
99
104
  protected
100
105
 
101
106
  def find_all
107
+ filter_items_by_key('_id')
102
108
  sort_by_attribute('_id')
103
109
  end
104
110
 
105
111
  def find_all_by_class
106
112
  filter_items_without_correct_ruby_class
107
113
  filter_deleted_items if options['without_deleted'].to_s == 'true'
114
+ sort_by_attribute('_id')
108
115
  end
109
116
 
110
- def find_belongs_to(belongs_to)
117
+ def find_belongs_to(belongs_to)
111
118
  filter_items_by_key([foreign_key_id(belongs_to)])
112
119
  filter_items_without_correct_ruby_class
113
120
  filter_deleted_items if options['without_deleted'].to_s == 'true'
121
+ sort_by_attribute('_id')
114
122
  end
115
123
 
116
124
  def find_by_attribute(attribute_string)
@@ -143,7 +151,7 @@ module RockingChair
143
151
  def filter_items_by_key(attributes)
144
152
  if options['startkey']
145
153
  filter_items_by_range(attributes)
146
- else
154
+ elsif options['key']
147
155
  filter_items_by_exact_key(attributes)
148
156
  end
149
157
  end
@@ -158,7 +166,6 @@ module RockingChair
158
166
  def filter_items_by_range(attributes)
159
167
  start_keys = options['startkey'].is_a?(Array) ? options['startkey'] : [options['startkey']]
160
168
  end_keys = options['endkey'].is_a?(Array) ? options['endkey'] : [options['endkey']]
161
-
162
169
  attributes.each_with_index do |attribute, index|
163
170
  filter_items_not_in_range(attribute, start_keys[index], end_keys[index])
164
171
  end
@@ -234,40 +241,7 @@ module RockingChair
234
241
  end
235
242
  end
236
243
  end
237
-
238
- def filter_by_startkey
239
- offset = 0
240
- if options['startkey']
241
- startkey_found = false
242
- @keys = keys.map do |key|
243
- if startkey_found || key == options['startkey']
244
- startkey_found = true
245
- key
246
- else
247
- offset += 1
248
- nil
249
- end
250
- end.compact
251
- end
252
- return offset
253
- end
254
-
255
- def filter_by_endkey
256
- if options['endkey']
257
- endkey_found = false
258
- @keys = keys.map do |key|
259
- if key == options['endkey']
260
- endkey_found = true
261
- key
262
- elsif endkey_found
263
- nil
264
- else
265
- key
266
- end
267
- end.compact
268
- end
269
- end
270
-
244
+
271
245
  def foreign_key_id(name)
272
246
  name.underscore.gsub('/','__').gsub('::','__') + "_id"
273
247
  end
@@ -280,5 +254,11 @@ module RockingChair
280
254
  description
281
255
  end
282
256
 
257
+ def normalize_descending_options
258
+ if options['descending'].to_s == 'true' && (options['startkey'] || options['endkey'])
259
+ options['startkey'], options['endkey'] = options['endkey'], options['startkey']
260
+ end
261
+ end
262
+
283
263
  end
284
264
  end
@@ -105,7 +105,7 @@ class CouchRestTest < Test::Unit::TestCase
105
105
  @db.save_doc({'_id' => "A", 'a' => 'b'})
106
106
 
107
107
  assert_equal({
108
- "total_rows" => 3, "offset" => 1, "rows" => [
108
+ "total_rows" => 3, "offset" => 0, "rows" => [
109
109
  {"id" => "B", "key" => "B", "value" => {"rev" => "the-revision"}},
110
110
  {"id" => "C", "key" => "C", "value" => {"rev" => "the-revision"}}
111
111
  ]
@@ -119,11 +119,11 @@ class CouchRestTest < Test::Unit::TestCase
119
119
  @db.save_doc({'_id' => "D", 'a' => 'b'})
120
120
 
121
121
  assert_equal({
122
- "total_rows" => 4, "offset" => 1, "rows" => [
122
+ "total_rows" => 4, "offset" => 0, "rows" => [
123
123
  {"id" => "C", "key" => "C", "value" => {"rev" => "the-revision", '_rev' => 'the-revision', '_id' => 'C', 'a' => 'b'}},
124
124
  {"id" => "B", "key" => "B", "value" => {"rev" => "the-revision", '_rev' => 'the-revision', '_id' => 'B', 'a' => 'b'}}
125
125
  ]
126
- }, @db.documents(:startkey => 'C', :limit => 2, :include_docs => true, :descending => true))
126
+ }, @db.documents(:startkey => "C\u999", :endkey => "B", :limit => 2, :include_docs => true, :descending => true))
127
127
  end
128
128
  end
129
129
 
@@ -233,7 +233,7 @@ class DatabaseTest < Test::Unit::TestCase
233
233
  @db["B"] = {"data" => "Z"}.to_json
234
234
 
235
235
  assert_equal({
236
- "total_rows" => 3, "offset" => 1, "rows" => [
236
+ "total_rows" => 3, "offset" => 0, "rows" => [
237
237
  {"id" => "B", "key" => "B", "value" => {"rev" => "rev"}},
238
238
  {"id" => "C", "key" => "C", "value" => {"rev" => "rev"}}
239
239
  ]
@@ -247,7 +247,7 @@ class DatabaseTest < Test::Unit::TestCase
247
247
  @db["B"] = {"data" => "Z"}.to_json
248
248
 
249
249
  assert_equal({
250
- "total_rows" => 3, "offset" => 1, "rows" => [
250
+ "total_rows" => 3, "offset" => 0, "rows" => [
251
251
  {"id" => "B", "key" => "B", "value" => {"rev" => "rev"}},
252
252
  {"id" => "C", "key" => "C", "value" => {"rev" => "rev"}}
253
253
  ]
@@ -262,7 +262,7 @@ class DatabaseTest < Test::Unit::TestCase
262
262
  @db["D"] = {"data" => "Z"}.to_json
263
263
 
264
264
  assert_equal({
265
- "total_rows" => 4, "offset" => 1, "rows" => [
265
+ "total_rows" => 4, "offset" => 0, "rows" => [
266
266
  {"id" => "B", "key" => "B", "value" => {"rev" => "rev"}}
267
267
  ]
268
268
  }.to_json, @db.all_documents('startkey' => 'B', 'limit' => '1'))
@@ -276,11 +276,11 @@ class DatabaseTest < Test::Unit::TestCase
276
276
  @db["D"] = {"data" => "Z"}.to_json
277
277
 
278
278
  assert_equal({
279
- "total_rows" => 4, "offset" => 2, "rows" => [
279
+ "total_rows" => 4, "offset" => 0, "rows" => [
280
280
  {"id" => "B", "key" => "B", "value" => {"rev" => "rev"}},
281
281
  {"id" => "A", "key" => "A", "value" => {"rev" => "rev"}}
282
282
  ]
283
- }.to_json, @db.all_documents('startkey' => 'B', 'descending' => 'true'))
283
+ }.to_json, @db.all_documents('startkey' => "B\u999", 'endkey' => "A", 'descending' => 'true'))
284
284
  end
285
285
 
286
286
  should "combine start, limit, and descending" do
@@ -291,10 +291,10 @@ class DatabaseTest < Test::Unit::TestCase
291
291
  @db["D"] = {"data" => "Z"}.to_json
292
292
 
293
293
  assert_equal({
294
- "total_rows" => 4, "offset" => 2, "rows" => [
294
+ "total_rows" => 4, "offset" => 0, "rows" => [
295
295
  {"id" => "B", "key" => "B", "value" => {"rev" => "rev"}}
296
296
  ]
297
- }.to_json, @db.all_documents('startkey' => 'B', 'descending' => 'true', 'limit' => '1'))
297
+ }.to_json, @db.all_documents('startkey' => "B\u999", 'endkey' => "B", 'descending' => 'true', 'limit' => '1'))
298
298
  end
299
299
 
300
300
  should "end by the given key" do
@@ -308,7 +308,7 @@ class DatabaseTest < Test::Unit::TestCase
308
308
  {"id" => "A", "key" => "A", "value" => {"rev" => "rev"}},
309
309
  {"id" => "B", "key" => "B", "value" => {"rev" => "rev"}}
310
310
  ]
311
- }.to_json, @db.all_documents('endkey' => 'B'))
311
+ }.to_json, @db.all_documents('endkey' => 'B', 'startkey' => 'A'))
312
312
  end
313
313
 
314
314
  should "combine start and end key" do
@@ -319,7 +319,7 @@ class DatabaseTest < Test::Unit::TestCase
319
319
  @db["D"] = {"data" => "Z"}.to_json
320
320
 
321
321
  assert_equal({
322
- "total_rows" => 4, "offset" => 1, "rows" => [
322
+ "total_rows" => 4, "offset" => 0, "rows" => [
323
323
  {"id" => "B", "key" => "B", "value" => {"rev" => "rev"}},
324
324
  {"id" => "C", "key" => "C", "value" => {"rev" => "rev"}}
325
325
  ]
@@ -334,7 +334,7 @@ class DatabaseTest < Test::Unit::TestCase
334
334
  @db["D"] = {"data" => "Z"}.to_json
335
335
 
336
336
  assert_equal({
337
- "total_rows" => 4, "offset" => 1, "rows" => [
337
+ "total_rows" => 4, "offset" => 0, "rows" => [
338
338
  {"id" => "B", "key" => "B", "value" => {"rev" => "rev", '_rev' => 'rev', 'data' => 'Z', '_id' => 'B'}},
339
339
  {"id" => "C", "key" => "C", "value" => {"rev" => "rev", '_rev' => 'rev', 'data' => 'Z', '_id' => 'C'}}
340
340
  ]
@@ -106,6 +106,24 @@ class SimplyStoredTest < Test::Unit::TestCase
106
106
  assert user.save
107
107
  assert_equal [user.id], @project.users.map(&:id)
108
108
  end
109
+
110
+ should "support counting associated" do
111
+ assert_equal 0, @project.user_count
112
+ user = User.create(:firstname => 'Michael', :project => @project)
113
+ assert_equal 1, @project.user_count(:force_reload => true)
114
+ end
115
+
116
+ should "support limiting" do
117
+ 3.times{ User.create!(:firstname => 'Michael', :project => @project) }
118
+ assert_equal 3, @project.users.size
119
+ assert_equal 2, @project.users(:limit => 2).size
120
+ end
121
+
122
+ should "support order" do
123
+ 3.times{|i| User.create!(:firstname => "user #{i}", :project => @project) }
124
+ assert_not_equal @project.users(:order => :asc).map(&:id), @project.users(:order => :desc).map(&:id)
125
+ assert_equal @project.users(:order => :asc).reverse, @project.users(:order => :desc)
126
+ end
109
127
  end
110
128
 
111
129
  context "when querying the all_documents view" do
@@ -124,6 +142,12 @@ class SimplyStoredTest < Test::Unit::TestCase
124
142
  assert_equal ['Peter', 'Michael Mann', 'Hulk'].sort, User.all.map(&:firstname).sort
125
143
  end
126
144
 
145
+ should "support order" do
146
+ 3.times{|i| User.create!(:firstname => "user #{i}") }
147
+ assert_not_equal User.all(:order => :asc).map(&:id), User.all(:order => :desc).map(&:id)
148
+ assert_equal User.all(:order => :asc).reverse, User.all(:order => :desc)
149
+ end
150
+
127
151
  should "load first" do
128
152
  User.create(:firstname => 'Michael Mann', :project => @project)
129
153
  User.create(:firstname => 'Peter', :project => @project)
@@ -209,6 +233,16 @@ class SimplyStoredTest < Test::Unit::TestCase
209
233
  end
210
234
 
211
235
  end
236
+
237
+ context "when deleting all design docs" do
238
+ should "reset all design docs" do
239
+ User.find_all_by_firstname('a')
240
+ db = "http://127.0.0.1:5984/#{CouchPotato::Config.database_name}"
241
+ assert_nothing_raised do
242
+ assert_equal 1, SimplyStored::Couch.delete_all_design_documents(db)
243
+ end
244
+ end
245
+ end
212
246
 
213
247
  end
214
248
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rocking_chair
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Weiss
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-10 00:00:00 +01:00
12
+ date: 2010-02-18 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -29,8 +29,10 @@ executables: []
29
29
  extensions: []
30
30
 
31
31
  extra_rdoc_files:
32
+ - LICENSE.txt
32
33
  - README.md
33
34
  files:
35
+ - LICENSE.txt
34
36
  - README.md
35
37
  - lib/rocking_chair.rb
36
38
  - lib/rocking_chair/couch_rest_http_adapter.rb