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
data/README.md CHANGED
@@ -59,35 +59,10 @@ Creating and Querying Views:
59
59
 
60
60
  ## CouchRest::Model
61
61
 
62
- CouchRest::Model has been deprecated and replaced by CouchRest::ExtendedDocument
63
-
64
-
65
- ## CouchRest::ExtendedDocument
66
-
67
- ### Callbacks
68
-
69
- `CouchRest::ExtendedDocuments` instances have 2 callbacks already defined for you:
70
- `create_callback`, `save_callback`, `update_callback` and `destroy_callback`
71
-
72
- In your document inherits from `CouchRest::ExtendedDocument`, define your callback as follows:
73
-
74
- save_callback :before, :generate_slug_from_name
75
-
76
- CouchRest uses a mixin you can find in lib/mixins/callbacks which is extracted from Rails 3, here are some simple usage examples:
77
-
78
- save_callback :before, :before_method
79
- save_callback :after, :after_method, :if => :condition
80
- save_callback :around {|r| stuff; yield; stuff }
81
-
82
- Check the mixin or the ExtendedDocument class to see how to implement your own callbacks.
83
-
84
- ### Casting
85
-
86
- Often, you will want to store multiple objects within a document, to be able to retrieve your objects when you load the document,
87
- you can define some casting rules.
88
-
89
- property :casted_attribute, :cast_as => 'WithCastedModelMixin'
90
- property :keywords, :cast_as => ["String"]
91
-
92
- If you want to cast an array of instances from a specific Class, use the trick shown above ["ClassName"]
62
+ CouchRest::Model is a module designed along the lines of DataMapper::Resource.
63
+ By subclassing, suddenly you get all sorts of powerful sugar, so that working
64
+ with CouchDB in your Rails or Merb app is no harder than working with the
65
+ standard SQL alternatives. See the CouchRest::Model documentation for an
66
+ example article class that illustrates usage.
93
67
 
68
+ CouchRest::Model will be removed from this package.
data/Rakefile CHANGED
@@ -23,7 +23,7 @@ spec = Gem::Specification.new do |s|
23
23
  s.homepage = "http://github.com/jchris/couchrest"
24
24
  s.description = "CouchRest provides a simple interface on top of CouchDB's RESTful HTTP API, as well as including some utility scripts for managing views and attachments."
25
25
  s.has_rdoc = true
26
- s.authors = ["J. Chris Anderson", "Matt Aimonetti"]
26
+ s.authors = ["J. Chris Anderson"]
27
27
  s.files = %w( LICENSE README.md Rakefile THANKS.md ) +
28
28
  Dir["{examples,lib,spec,utils}/**/*"] -
29
29
  Dir["spec/tmp"]
@@ -64,6 +64,3 @@ end
64
64
 
65
65
  desc "Run the rspec"
66
66
  task :default => :spec
67
-
68
-
69
- ::Rake::GemPackageTask.new(spec) { |p| p.gem_spec = spec }
@@ -1,38 +1,31 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'couchrest')
1
+ require 'rubygems'
2
+ require 'couchrest'
2
3
 
3
4
  def show obj
4
5
  puts obj.inspect
5
6
  puts
6
7
  end
7
8
 
8
- SERVER = CouchRest.new
9
- SERVER.default_database = 'couchrest-extendeddoc-example'
9
+ CouchRest::Model.default_database = CouchRest.database!('couchrest-model-example')
10
10
 
11
- class Author < CouchRest::ExtendedDocument
12
- use_database SERVER.default_database
13
- property :name
14
-
11
+ class Author < CouchRest::Model
12
+ key_accessor :name
15
13
  def drink_scotch
16
14
  puts "... glug type glug ... I'm #{name} ... type glug glug ..."
17
15
  end
18
16
  end
19
17
 
20
- class Post < CouchRest::ExtendedDocument
21
- use_database SERVER.default_database
22
-
23
- property :title
24
- property :body
25
- property :author, :cast_as => 'Author'
18
+ class Post < CouchRest::Model
19
+ key_accessor :title, :body, :author
20
+
21
+ cast :author, :as => 'Author'
26
22
 
27
23
  timestamps!
28
24
  end
29
25
 
30
- class Comment < CouchRest::ExtendedDocument
31
- use_database SERVER.default_database
32
-
33
- property :commenter, :cast_as => 'Author'
34
- timestamps!
35
-
26
+ class Comment < CouchRest::Model
27
+ cast :commenter, :as => 'Author'
28
+
36
29
  def post= post
37
30
  self["post_id"] = post.id
38
31
  end
@@ -40,6 +33,7 @@ class Comment < CouchRest::ExtendedDocument
40
33
  Post.get(self['post_id']) if self['post_id']
41
34
  end
42
35
 
36
+ timestamps!
43
37
  end
44
38
 
45
39
  puts "Act I: CRUD"
@@ -20,7 +20,7 @@ module CouchRest
20
20
  @uri = @root = "#{host}/#{CGI.escape(name)}"
21
21
  @streamer = Streamer.new(self)
22
22
  @bulk_save_cache = []
23
- @bulk_save_cache_limit = 500 # must be smaller than the uuid count
23
+ @bulk_save_cache_limit = 50
24
24
  end
25
25
 
26
26
  # returns the database's uri
@@ -61,15 +61,12 @@ module CouchRest
61
61
  # paramaters as described in http://wiki.apache.org/couchdb/HttpViewApi
62
62
  def view(name, params = {}, &block)
63
63
  keys = params.delete(:keys)
64
- name = name.split('/') # I think this will always be length == 2, but maybe not...
65
- dname = name.shift
66
- vname = name.join('/')
67
- url = CouchRest.paramify_url "#{@uri}/_design/#{dname}/_view/#{vname}", params
64
+ url = CouchRest.paramify_url "#{@uri}/_view/#{name}", params
68
65
  if keys
69
66
  CouchRest.post(url, {:keys => keys})
70
67
  else
71
68
  if block_given?
72
- @streamer.view("_design/#{dname}/_view/#{vname}", params, &block)
69
+ @streamer.view(name, params, &block)
73
70
  else
74
71
  CouchRest.get url
75
72
  end
@@ -93,7 +90,9 @@ module CouchRest
93
90
  def fetch_attachment(doc, name)
94
91
  # slug = escape_docid(docid)
95
92
  # name = CGI.escape(name)
93
+
96
94
  uri = uri_for_attachment(doc, name)
95
+
97
96
  RestClient.get uri
98
97
  # "#{@uri}/#{slug}/#{name}"
99
98
  end
@@ -178,7 +177,6 @@ module CouchRest
178
177
  end
179
178
  CouchRest.post "#{@uri}/_bulk_docs", {:docs => docs}
180
179
  end
181
- alias :bulk_delete :bulk_save
182
180
 
183
181
  # DELETE the document from CouchDB that has the given <tt>_id</tt> and
184
182
  # <tt>_rev</tt>.
@@ -282,7 +280,7 @@ module CouchRest
282
280
 
283
281
  private
284
282
 
285
- def uri_for_attachment(doc, name)
283
+ def uri_for_attachment doc, name
286
284
  if doc.is_a?(String)
287
285
  puts "CouchRest::Database#fetch_attachment will eventually require a doc as the first argument, not a doc.id"
288
286
  docid = doc
@@ -1,21 +1,33 @@
1
- require 'delegate'
2
-
3
- module CouchRest
1
+ module CouchRest
2
+ class Response < Hash
3
+ def initialize(keys = {})
4
+ keys.each do |k,v|
5
+ self[k.to_s] = v
6
+ end
7
+ end
8
+ def []= key, value
9
+ super(key.to_s, value)
10
+ end
11
+ def [] key
12
+ super(key.to_s)
13
+ end
14
+ end
15
+
4
16
  class Document < Response
5
- include CouchRest::Mixins::Attachments
17
+ include CouchRest::Mixins::Views
6
18
 
7
- # def self.inherited(subklass)
8
- # subklass.send(:class_inheritable_accessor, :database)
9
- # end
10
-
11
- class_inheritable_accessor :database
12
19
  attr_accessor :database
20
+ @@database = nil
13
21
 
14
22
  # override the CouchRest::Model-wide default_database
15
23
  # This is not a thread safe operation, do not change the model
16
24
  # database at runtime.
17
25
  def self.use_database(db)
18
- self.database = db
26
+ @@database = db
27
+ end
28
+
29
+ def self.database
30
+ @@database
19
31
  end
20
32
 
21
33
  def id
@@ -26,36 +38,6 @@ module CouchRest
26
38
  self['_rev']
27
39
  end
28
40
 
29
- # returns true if the document has never been saved
30
- def new_document?
31
- !rev
32
- end
33
-
34
- # Saves the document to the db using create or update. Also runs the :save
35
- # callbacks. Sets the <tt>_id</tt> and <tt>_rev</tt> fields based on
36
- # CouchDB's response.
37
- # If <tt>bulk</tt> is <tt>true</tt> (defaults to false) the document is cached for bulk save.
38
- def save(bulk = false)
39
- raise ArgumentError, "doc.database required for saving" unless database
40
- result = database.save_doc self, bulk
41
- result['ok']
42
- end
43
-
44
- # Deletes the document from the database. Runs the :delete callbacks.
45
- # Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the
46
- # document to be saved to a new <tt>_id</tt>.
47
- # If <tt>bulk</tt> is <tt>true</tt> (defaults to false) the document won't
48
- # actually be deleted from the db until bulk save.
49
- def destroy(bulk = false)
50
- raise ArgumentError, "doc.database required to destroy" unless database
51
- result = database.delete_doc(self, bulk)
52
- if result['ok']
53
- self['_rev'] = nil
54
- self['_id'] = nil
55
- end
56
- result['ok']
57
- end
58
-
59
41
  # copies the document to a new id. If the destination id currently exists, a rev must be provided.
60
42
  # <tt>dest</tt> can take one of two forms if overwriting: "id_to_overwrite?rev=revision" or the actual doc
61
43
  # hash with a '_rev' key
@@ -91,6 +73,29 @@ module CouchRest
91
73
  @database || self.class.database
92
74
  end
93
75
 
76
+ # saves an attachment directly to couchdb
77
+ def put_attachment(name, file, options={})
78
+ raise ArgumentError, "doc must be saved" unless self.rev
79
+ raise ArgumentError, "doc.database required to put_attachment" unless database
80
+ result = database.put_attachment(self, name, file, options)
81
+ self['_rev'] = result['rev']
82
+ result['ok']
83
+ end
84
+
85
+ # returns an attachment's data
86
+ def fetch_attachment(name)
87
+ raise ArgumentError, "doc must be saved" unless self.rev
88
+ raise ArgumentError, "doc.database required to put_attachment" unless database
89
+ database.fetch_attachment(self, name)
90
+ end
91
+
92
+ # deletes an attachment directly from couchdb
93
+ def delete_attachment(name)
94
+ raise ArgumentError, "doc.database required to delete_attachment" unless database
95
+ result = database.delete_attachment(self, name)
96
+ self['_rev'] = result['rev']
97
+ result['ok']
98
+ end
94
99
  end
95
100
 
96
101
  end