jrun-couchrest 0.2.1.1 → 0.12.6

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.
Files changed (59) hide show
  1. data/README.md +6 -31
  2. data/Rakefile +1 -4
  3. data/examples/model/example.rb +13 -19
  4. data/lib/couchrest/core/database.rb +6 -8
  5. data/lib/couchrest/core/document.rb +45 -40
  6. data/lib/couchrest/core/model.rb +615 -0
  7. data/lib/couchrest/core/server.rb +1 -1
  8. data/lib/couchrest/monkeypatches.rb +48 -68
  9. data/lib/couchrest.rb +8 -54
  10. data/spec/couchrest/core/database_spec.rb +26 -31
  11. data/spec/couchrest/core/document_spec.rb +1 -1
  12. data/spec/couchrest/core/model_spec.rb +855 -0
  13. data/spec/spec_helper.rb +1 -6
  14. metadata +3 -56
  15. data/lib/couchrest/core/response.rb +0 -16
  16. data/lib/couchrest/mixins/attachments.rb +0 -31
  17. data/lib/couchrest/mixins/callbacks.rb +0 -483
  18. data/lib/couchrest/mixins/design_doc.rb +0 -64
  19. data/lib/couchrest/mixins/document_queries.rb +0 -48
  20. data/lib/couchrest/mixins/extended_attachments.rb +0 -68
  21. data/lib/couchrest/mixins/extended_document_mixins.rb +0 -6
  22. data/lib/couchrest/mixins/properties.rb +0 -125
  23. data/lib/couchrest/mixins/validation.rb +0 -234
  24. data/lib/couchrest/mixins/views.rb +0 -168
  25. data/lib/couchrest/mixins.rb +0 -4
  26. data/lib/couchrest/more/casted_model.rb +0 -28
  27. data/lib/couchrest/more/extended_document.rb +0 -217
  28. data/lib/couchrest/more/property.rb +0 -40
  29. data/lib/couchrest/support/blank.rb +0 -42
  30. data/lib/couchrest/support/class.rb +0 -191
  31. data/lib/couchrest/validation/auto_validate.rb +0 -163
  32. data/lib/couchrest/validation/contextual_validators.rb +0 -78
  33. data/lib/couchrest/validation/validation_errors.rb +0 -118
  34. data/lib/couchrest/validation/validators/absent_field_validator.rb +0 -74
  35. data/lib/couchrest/validation/validators/confirmation_validator.rb +0 -99
  36. data/lib/couchrest/validation/validators/format_validator.rb +0 -117
  37. data/lib/couchrest/validation/validators/formats/email.rb +0 -66
  38. data/lib/couchrest/validation/validators/formats/url.rb +0 -43
  39. data/lib/couchrest/validation/validators/generic_validator.rb +0 -120
  40. data/lib/couchrest/validation/validators/length_validator.rb +0 -134
  41. data/lib/couchrest/validation/validators/method_validator.rb +0 -89
  42. data/lib/couchrest/validation/validators/numeric_validator.rb +0 -104
  43. data/lib/couchrest/validation/validators/required_field_validator.rb +0 -109
  44. data/spec/couchrest/core/server_spec.rb +0 -35
  45. data/spec/couchrest/more/casted_extended_doc_spec.rb +0 -40
  46. data/spec/couchrest/more/casted_model_spec.rb +0 -98
  47. data/spec/couchrest/more/extended_doc_attachment_spec.rb +0 -130
  48. data/spec/couchrest/more/extended_doc_spec.rb +0 -509
  49. data/spec/couchrest/more/extended_doc_view_spec.rb +0 -207
  50. data/spec/couchrest/more/property_spec.rb +0 -130
  51. data/spec/couchrest/support/class_spec.rb +0 -59
  52. data/spec/fixtures/more/article.rb +0 -34
  53. data/spec/fixtures/more/card.rb +0 -20
  54. data/spec/fixtures/more/course.rb +0 -14
  55. data/spec/fixtures/more/event.rb +0 -6
  56. data/spec/fixtures/more/invoice.rb +0 -17
  57. data/spec/fixtures/more/person.rb +0 -8
  58. data/spec/fixtures/more/question.rb +0 -6
  59. data/spec/fixtures/more/service.rb +0 -12
@@ -1,6 +1,3 @@
1
- require File.join(File.dirname(__FILE__), 'support', 'class')
2
- require File.join(File.dirname(__FILE__), 'support', 'blank')
3
-
4
1
  # This file must be loaded after the JSON gem and any other library that beats up the Time class.
5
2
  class Time
6
3
  # This date format sorts lexicographically
@@ -31,19 +28,13 @@ if RUBY_VERSION.to_f < 1.9
31
28
  class Net::BufferedIO #:nodoc:
32
29
  alias :old_rbuf_fill :rbuf_fill
33
30
  def rbuf_fill
34
- if @io.respond_to?(:read_nonblock)
35
- begin
31
+ begin
32
+ @rbuf << @io.read_nonblock(65536)
33
+ rescue Errno::EWOULDBLOCK
34
+ if IO.select([@io], nil, nil, @read_timeout)
36
35
  @rbuf << @io.read_nonblock(65536)
37
- rescue Errno::EWOULDBLOCK
38
- if IO.select([@io], nil, nil, @read_timeout)
39
- retry
40
- else
41
- raise Timeout::TimeoutError
42
- end
43
- end
44
- else
45
- timeout(@read_timeout) do
46
- @rbuf << @io.sysread(65536)
36
+ else
37
+ raise Timeout::TimeoutError
47
38
  end
48
39
  end
49
40
  end
@@ -62,58 +53,47 @@ module RestClient
62
53
  :url => url,
63
54
  :headers => headers)
64
55
  end
65
-
66
- # class Request
67
- #
68
- # def establish_connection(uri)
69
- # Thread.current[:connection].finish if (Thread.current[:connection] && Thread.current[:connection].started?)
70
- # p net_http_class
71
- # net = net_http_class.new(uri.host, uri.port)
72
- # net.use_ssl = uri.is_a?(URI::HTTPS)
73
- # net.verify_mode = OpenSSL::SSL::VERIFY_NONE
74
- # Thread.current[:connection] = net
75
- # Thread.current[:connection].start
76
- # Thread.current[:connection]
77
- # end
78
- #
79
- # def transmit(uri, req, payload)
80
- # setup_credentials(req)
81
- #
82
- # Thread.current[:host] ||= uri.host
83
- # Thread.current[:port] ||= uri.port
84
- #
85
- # if (Thread.current[:connection].nil? || (Thread.current[:host] != uri.host))
86
- # p "establishing a connection"
87
- # establish_connection(uri)
88
- # end
89
- #
90
- # display_log request_log
91
- # http = Thread.current[:connection]
92
- # http.read_timeout = @timeout if @timeout
93
- #
94
- # begin
95
- # res = http.request(req, payload)
96
- # rescue
97
- # p "Net::HTTP connection failed, reconnecting"
98
- # establish_connection(uri)
99
- # http = Thread.current[:connection]
100
- # require 'ruby-debug'
101
- # debugger
102
- # req.body_stream = nil
103
- #
104
- # res = http.request(req, payload)
105
- # display_log response_log(res)
106
- # result res
107
- # else
108
- # display_log response_log(res)
109
- # process_result res
110
- # end
111
- #
112
- # rescue EOFError
113
- # raise RestClient::ServerBrokeConnection
114
- # rescue Timeout::Error
115
- # raise RestClient::RequestTimeout
116
- # end
117
- # end
118
56
 
57
+ class Request
58
+ def transmit(uri, req, payload)
59
+ setup_credentials(req)
60
+
61
+ Thread.current[:host] ||= uri.host
62
+ Thread.current[:port] ||= uri.port
63
+
64
+ net = net_http_class.new(uri.host, uri.port)
65
+
66
+ if Thread.current[:connection].nil? || Thread.current[:host] != uri.host
67
+ Thread.current[:connection].finish if (Thread.current[:connection] && Thread.current[:connection].started?)
68
+ net.use_ssl = uri.is_a?(URI::HTTPS)
69
+ net.verify_mode = OpenSSL::SSL::VERIFY_NONE
70
+ Thread.current[:connection] = net
71
+ Thread.current[:connection].start
72
+ end
73
+
74
+ display_log request_log
75
+ http = Thread.current[:connection]
76
+
77
+ http.read_timeout = @timeout if @timeout
78
+ begin
79
+ res = http.request(req, payload)
80
+ rescue
81
+ # p "Net::HTTP connection failed, reconnecting"
82
+ Thread.current[:connection].finish
83
+ http = Thread.current[:connection] = net
84
+ Thread.current[:connection].start
85
+ res = http.request(req, payload)
86
+ display_log response_log(res)
87
+ process_result res
88
+ else
89
+ display_log response_log(res)
90
+ process_result res
91
+ end
92
+
93
+ rescue EOFError
94
+ raise RestClient::ServerBrokeConnection
95
+ rescue Timeout::Error
96
+ raise RestClient::RequestTimeout
97
+ end
98
+ end
119
99
  end
data/lib/couchrest.rb CHANGED
@@ -13,28 +13,24 @@
13
13
  # limitations under the License.
14
14
 
15
15
  require "rubygems"
16
- gem 'json'
17
16
  require 'json'
18
- gem 'rest-client'
19
17
  require 'rest_client'
20
18
 
21
19
  $:.unshift File.dirname(__FILE__) unless
22
20
  $:.include?(File.dirname(__FILE__)) ||
23
21
  $:.include?(File.expand_path(File.dirname(__FILE__)))
24
22
 
25
- $COUCHREST_DEBUG ||= false
26
23
 
27
24
  require 'couchrest/monkeypatches'
28
25
 
29
26
  # = CouchDB, close to the metal
30
27
  module CouchRest
31
- VERSION = '0.2.1.1' unless self.const_defined?("VERSION")
28
+ VERSION = '0.12.6'
32
29
 
33
30
  autoload :Server, 'couchrest/core/server'
34
31
  autoload :Database, 'couchrest/core/database'
35
- autoload :Response, 'couchrest/core/response'
36
32
  autoload :Document, 'couchrest/core/document'
37
- autoload :Design, 'couchrest/core/design'
33
+ autoload :Design, 'couchrest/core/design'
38
34
  autoload :View, 'couchrest/core/view'
39
35
  autoload :Model, 'couchrest/core/model'
40
36
  autoload :Pager, 'couchrest/helper/pager'
@@ -42,7 +38,6 @@ module CouchRest
42
38
  autoload :Streamer, 'couchrest/helper/streamer'
43
39
 
44
40
  autoload :ExtendedDocument, 'couchrest/more/extended_document'
45
- autoload :CastedModel, 'couchrest/more/casted_model'
46
41
 
47
42
  require File.join(File.dirname(__FILE__), 'couchrest', 'mixins')
48
43
 
@@ -51,23 +46,6 @@ module CouchRest
51
46
  # some helpers for tasks like instantiating a new Database or Server instance.
52
47
  class << self
53
48
 
54
- # extracted from Extlib
55
- #
56
- # Constantize tries to find a declared constant with the name specified
57
- # in the string. It raises a NameError when the name is not in CamelCase
58
- # or is not initialized.
59
- #
60
- # @example
61
- # "Module".constantize #=> Module
62
- # "Class".constantize #=> Class
63
- def constantize(camel_cased_word)
64
- unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
65
- raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
66
- end
67
-
68
- Object.module_eval("::#{$1}", __FILE__, __LINE__)
69
- end
70
-
71
49
  # todo, make this parse the url and instantiate a Server or Database instance
72
50
  # depending on the specificity.
73
51
  def new(*opts)
@@ -125,42 +103,18 @@ module CouchRest
125
103
  cr.database(parsed[:database])
126
104
  end
127
105
 
128
- def put(uri, doc = nil)
106
+ def put uri, doc = nil
129
107
  payload = doc.to_json if doc
130
- begin
131
- JSON.parse(RestClient.put(uri, payload))
132
- rescue Exception => e
133
- if $COUCHREST_DEBUG == true
134
- raise "Error while sending a PUT request #{uri}\npayload: #{payload.inspect}\n#{e}"
135
- else
136
- raise e
137
- end
138
- end
108
+ JSON.parse(RestClient.put(uri, payload))
139
109
  end
140
110
 
141
- def get(uri)
142
- begin
143
- JSON.parse(RestClient.get(uri), :max_nesting => false)
144
- rescue => e
145
- if $COUCHREST_DEBUG == true
146
- raise "Error while sending a GET request #{uri}\n: #{e}"
147
- else
148
- raise e
149
- end
150
- end
111
+ def get uri
112
+ JSON.parse(RestClient.get(uri), :max_nesting => false)
151
113
  end
152
114
 
153
115
  def post uri, doc = nil
154
116
  payload = doc.to_json if doc
155
- begin
156
- JSON.parse(RestClient.post(uri, payload))
157
- rescue Exception => e
158
- if $COUCHREST_DEBUG == true
159
- raise "Error while sending a POST request #{uri}\npayload: #{payload.inspect}\n#{e}"
160
- else
161
- raise e
162
- end
163
- end
117
+ JSON.parse(RestClient.post(uri, payload))
164
118
  end
165
119
 
166
120
  def delete uri
@@ -186,4 +140,4 @@ module CouchRest
186
140
  url
187
141
  end
188
142
  end # class << self
189
- end
143
+ end
@@ -244,14 +244,12 @@ describe CouchRest::Database do
244
244
  }
245
245
  }
246
246
  }
247
- @db.save_doc(@doc)
247
+ @db.save(@doc)
248
248
  end
249
249
 
250
- # Depreacated
251
- # it "should get the attachment with the doc's _id" do
252
- # @db.fetch_attachment("mydocwithattachment", "test.html").should == @attach
253
- # end
254
-
250
+ it "should get the attachment with the doc's _id" do
251
+ @db.fetch_attachment("mydocwithattachment", "test.html").should == @attach
252
+ end
255
253
  it "should get the attachment with the doc itself" do
256
254
  @db.fetch_attachment(@db.get('mydocwithattachment'), 'test.html').should == @attach
257
255
  end
@@ -268,8 +266,7 @@ describe CouchRest::Database do
268
266
  it "should save the attachment to a new doc" do
269
267
  r = @db.put_attachment({'_id' => 'attach-this'}, 'couchdb.png', image = @file.read, {:content_type => 'image/png'})
270
268
  r['ok'].should == true
271
- doc = @db.get("attach-this")
272
- attachment = @db.fetch_attachment(doc,"couchdb.png")
269
+ attachment = @db.fetch_attachment("attach-this","couchdb.png")
273
270
  attachment.should == image
274
271
  end
275
272
  end
@@ -277,7 +274,7 @@ describe CouchRest::Database do
277
274
  describe "PUT document with attachment" do
278
275
  before(:each) do
279
276
  @attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
280
- doc = {
277
+ @doc = {
281
278
  "_id" => "mydocwithattachment",
282
279
  "field" => ["some value"],
283
280
  "_attachments" => {
@@ -287,14 +284,14 @@ describe CouchRest::Database do
287
284
  }
288
285
  }
289
286
  }
290
- @db.save_doc(doc)
291
- @doc = @db.get("mydocwithattachment")
287
+ @db.save_doc(@doc)
292
288
  end
293
289
  it "should save and be indicated" do
294
- @doc['_attachments']['test.html']['length'].should == @attach.length
290
+ doc = @db.get("mydocwithattachment")
291
+ doc['_attachments']['test.html']['length'].should == @attach.length
295
292
  end
296
293
  it "should be there" do
297
- attachment = @db.fetch_attachment(@doc,"test.html")
294
+ attachment = @db.fetch_attachment("mydocwithattachment","test.html")
298
295
  attachment.should == @attach
299
296
  end
300
297
  end
@@ -312,15 +309,14 @@ describe CouchRest::Database do
312
309
  }
313
310
  }
314
311
  @db.save_doc(doc)
315
- doc['_rev'].should_not be_nil
312
+ doc = @db.get('mydocwithattachment')
316
313
  doc['field'] << 'another value'
317
- @db.save_doc(doc)["ok"].should be_true
314
+ @db.save_doc(doc)
318
315
  end
319
316
 
320
317
  it 'should be there' do
321
- doc = @db.get('mydocwithattachment')
322
- attachment = @db.fetch_attachment(doc, 'test.html')
323
- Base64.decode64(attachment).should == @attach
318
+ attachment = @db.fetch_attachment('mydocwithattachment', 'test.html')
319
+ attachment.should == @attach
324
320
  end
325
321
  end
326
322
 
@@ -343,18 +339,18 @@ describe CouchRest::Database do
343
339
  }
344
340
  }
345
341
  @db.save_doc(@doc)
346
- @doc = @db.get("mydocwithattachment")
347
342
  end
348
343
  it "should save and be indicated" do
349
- @doc['_attachments']['test.html']['length'].should == @attach.length
350
- @doc['_attachments']['other.html']['length'].should == @attach2.length
344
+ doc = @db.get("mydocwithattachment")
345
+ doc['_attachments']['test.html']['length'].should == @attach.length
346
+ doc['_attachments']['other.html']['length'].should == @attach2.length
351
347
  end
352
348
  it "should be there" do
353
- attachment = @db.fetch_attachment(@doc,"test.html")
349
+ attachment = @db.fetch_attachment("mydocwithattachment","test.html")
354
350
  attachment.should == @attach
355
351
  end
356
352
  it "should be there" do
357
- attachment = @db.fetch_attachment(@doc,"other.html")
353
+ attachment = @db.fetch_attachment("mydocwithattachment","other.html")
358
354
  attachment.should == @attach2
359
355
  end
360
356
  end
@@ -370,13 +366,13 @@ describe CouchRest::Database do
370
366
  }
371
367
  }
372
368
  }
373
- @db.save_doc(doc)
369
+ @db.save(doc)
374
370
  @doc = @db.get('mydocwithattachment')
375
371
  end
376
372
  it "should delete the attachment" do
377
- lambda { @db.fetch_attachment(@doc,'test.html') }.should_not raise_error
373
+ lambda { @db.fetch_attachment('mydocwithattachment','test.html') }.should_not raise_error
378
374
  @db.delete_attachment(@doc, "test.html")
379
- lambda { @db.fetch_attachment(@doc,'test.html') }.should raise_error(RestClient::ResourceNotFound)
375
+ lambda { @db.fetch_attachment('mydocwithattachment','test.html') }.should raise_error(RestClient::ResourceNotFound)
380
376
  end
381
377
  end
382
378
 
@@ -399,8 +395,7 @@ describe CouchRest::Database do
399
395
  doc['_attachments']['http://example.com/stuff.cgi?things=and%20stuff']['length'].should == @attach.length
400
396
  end
401
397
  it "should be there" do
402
- doc = @db.get(@docid)
403
- attachment = @db.fetch_attachment(doc,"http://example.com/stuff.cgi?things=and%20stuff")
398
+ attachment = @db.fetch_attachment(@docid,"http://example.com/stuff.cgi?things=and%20stuff")
404
399
  attachment.should == @attach
405
400
  end
406
401
  end
@@ -677,7 +672,7 @@ describe CouchRest::Database do
677
672
 
678
673
  describe "replicating a database" do
679
674
  before do
680
- @db.save_doc({'_id' => 'test_doc', 'some-value' => 'foo'})
675
+ @db.save({'_id' => 'test_doc', 'some-value' => 'foo'})
681
676
  @other_db = @cr.database 'couchrest-test-replication'
682
677
  @other_db.delete! rescue nil
683
678
  @other_db = @cr.create_db 'couchrest-test-replication'
@@ -709,7 +704,7 @@ describe CouchRest::Database do
709
704
  describe "creating a database" do
710
705
  before(:each) do
711
706
  @db = @cr.database('couchrest-test-db_to_create')
712
- @db.delete! if @cr.databases.include?('couchrest-test-db_to_create')
707
+ @db.delete!
713
708
  end
714
709
 
715
710
  it "should just work fine" do
@@ -747,4 +742,4 @@ describe CouchRest::Database do
747
742
  end
748
743
 
749
744
 
750
- end
745
+ end
@@ -249,7 +249,7 @@ describe "dealing with attachments" do
249
249
  before do
250
250
  @db = reset_test_db!
251
251
  @attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
252
- response = @db.save_doc({'key' => 'value'})
252
+ response = @db.save({'key' => 'value'})
253
253
  @doc = @db.get(response['id'])
254
254
  end
255
255