samflores-couchrest 0.2.1 → 0.12.3

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 (66) hide show
  1. data/README.md +10 -34
  2. data/Rakefile +5 -2
  3. data/bin/couchdir +20 -0
  4. data/examples/model/example.rb +13 -19
  5. data/examples/word_count/word_count.rb +24 -3
  6. data/examples/word_count/word_count_query.rb +6 -7
  7. data/lib/couchrest/core/database.rb +49 -126
  8. data/lib/couchrest/core/document.rb +25 -58
  9. data/lib/couchrest/core/model.rb +612 -0
  10. data/lib/couchrest/core/server.rb +10 -47
  11. data/lib/couchrest/core/validations.rb +328 -0
  12. data/lib/couchrest/monkeypatches.rb +0 -95
  13. data/lib/couchrest.rb +10 -57
  14. data/spec/couchrest/core/database_spec.rb +68 -183
  15. data/spec/couchrest/core/design_spec.rb +1 -1
  16. data/spec/couchrest/core/document_spec.rb +104 -285
  17. data/spec/couchrest/core/model_spec.rb +855 -0
  18. data/spec/couchrest/helpers/pager_spec.rb +1 -1
  19. data/spec/spec_helper.rb +7 -13
  20. metadata +17 -83
  21. data/examples/word_count/word_count_views.rb +0 -26
  22. data/lib/couchrest/core/response.rb +0 -16
  23. data/lib/couchrest/mixins/attachments.rb +0 -31
  24. data/lib/couchrest/mixins/callbacks.rb +0 -483
  25. data/lib/couchrest/mixins/design_doc.rb +0 -64
  26. data/lib/couchrest/mixins/document_queries.rb +0 -48
  27. data/lib/couchrest/mixins/extended_attachments.rb +0 -68
  28. data/lib/couchrest/mixins/extended_document_mixins.rb +0 -6
  29. data/lib/couchrest/mixins/properties.rb +0 -125
  30. data/lib/couchrest/mixins/validation.rb +0 -234
  31. data/lib/couchrest/mixins/views.rb +0 -168
  32. data/lib/couchrest/mixins.rb +0 -4
  33. data/lib/couchrest/more/casted_model.rb +0 -28
  34. data/lib/couchrest/more/extended_document.rb +0 -217
  35. data/lib/couchrest/more/property.rb +0 -40
  36. data/lib/couchrest/support/blank.rb +0 -42
  37. data/lib/couchrest/support/class.rb +0 -191
  38. data/lib/couchrest/validation/auto_validate.rb +0 -163
  39. data/lib/couchrest/validation/contextual_validators.rb +0 -78
  40. data/lib/couchrest/validation/validation_errors.rb +0 -118
  41. data/lib/couchrest/validation/validators/absent_field_validator.rb +0 -74
  42. data/lib/couchrest/validation/validators/confirmation_validator.rb +0 -99
  43. data/lib/couchrest/validation/validators/format_validator.rb +0 -117
  44. data/lib/couchrest/validation/validators/formats/email.rb +0 -66
  45. data/lib/couchrest/validation/validators/formats/url.rb +0 -43
  46. data/lib/couchrest/validation/validators/generic_validator.rb +0 -120
  47. data/lib/couchrest/validation/validators/length_validator.rb +0 -134
  48. data/lib/couchrest/validation/validators/method_validator.rb +0 -89
  49. data/lib/couchrest/validation/validators/numeric_validator.rb +0 -104
  50. data/lib/couchrest/validation/validators/required_field_validator.rb +0 -109
  51. data/spec/couchrest/core/server_spec.rb +0 -35
  52. data/spec/couchrest/more/casted_extended_doc_spec.rb +0 -40
  53. data/spec/couchrest/more/casted_model_spec.rb +0 -98
  54. data/spec/couchrest/more/extended_doc_attachment_spec.rb +0 -130
  55. data/spec/couchrest/more/extended_doc_spec.rb +0 -509
  56. data/spec/couchrest/more/extended_doc_view_spec.rb +0 -207
  57. data/spec/couchrest/more/property_spec.rb +0 -130
  58. data/spec/couchrest/support/class_spec.rb +0 -59
  59. data/spec/fixtures/more/article.rb +0 -34
  60. data/spec/fixtures/more/card.rb +0 -20
  61. data/spec/fixtures/more/course.rb +0 -14
  62. data/spec/fixtures/more/event.rb +0 -6
  63. data/spec/fixtures/more/invoice.rb +0 -17
  64. data/spec/fixtures/more/person.rb +0 -8
  65. data/spec/fixtures/more/question.rb +0 -6
  66. data/spec/fixtures/more/service.rb +0 -12
@@ -1,43 +1,44 @@
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
6
17
 
7
- # def self.inherited(subklass)
8
- # subklass.send(:class_inheritable_accessor, :database)
9
- # end
10
-
11
- class_inheritable_accessor :database
12
18
  attr_accessor :database
13
-
14
- # override the CouchRest::Model-wide default_database
15
- # This is not a thread safe operation, do not change the model
16
- # database at runtime.
17
- def self.use_database(db)
18
- self.database = db
19
- end
20
-
19
+
20
+ # alias for self['_id']
21
21
  def id
22
22
  self['_id']
23
23
  end
24
-
24
+
25
+ # alias for self['_rev']
25
26
  def rev
26
27
  self['_rev']
27
28
  end
28
-
29
+
29
30
  # returns true if the document has never been saved
30
31
  def new_document?
31
32
  !rev
32
33
  end
33
-
34
+
34
35
  # Saves the document to the db using create or update. Also runs the :save
35
36
  # callbacks. Sets the <tt>_id</tt> and <tt>_rev</tt> fields based on
36
37
  # CouchDB's response.
37
38
  # If <tt>bulk</tt> is <tt>true</tt> (defaults to false) the document is cached for bulk save.
38
39
  def save(bulk = false)
39
40
  raise ArgumentError, "doc.database required for saving" unless database
40
- result = database.save_doc self, bulk
41
+ result = database.save self, bulk
41
42
  result['ok']
42
43
  end
43
44
 
@@ -48,49 +49,15 @@ module CouchRest
48
49
  # actually be deleted from the db until bulk save.
49
50
  def destroy(bulk = false)
50
51
  raise ArgumentError, "doc.database required to destroy" unless database
51
- result = database.delete_doc(self, bulk)
52
+ result = database.delete(self, bulk)
52
53
  if result['ok']
53
54
  self['_rev'] = nil
54
55
  self['_id'] = nil
55
56
  end
56
57
  result['ok']
57
58
  end
58
-
59
- # copies the document to a new id. If the destination id currently exists, a rev must be provided.
60
- # <tt>dest</tt> can take one of two forms if overwriting: "id_to_overwrite?rev=revision" or the actual doc
61
- # hash with a '_rev' key
62
- def copy(dest)
63
- raise ArgumentError, "doc.database required to copy" unless database
64
- result = database.copy_doc(self, dest)
65
- result['ok']
66
- end
67
-
68
- # moves the document to a new id. If the destination id currently exists, a rev must be provided.
69
- # <tt>dest</tt> can take one of two forms if overwriting: "id_to_overwrite?rev=revision" or the actual doc
70
- # hash with a '_rev' key
71
- def move(dest)
72
- raise ArgumentError, "doc.database required to copy" unless database
73
- result = database.move_doc(self, dest)
74
- result['ok']
75
- end
76
-
77
- # Returns the CouchDB uri for the document
78
- def uri(append_rev = false)
79
- return nil if new_document?
80
- couch_uri = "http://#{database.uri}/#{CGI.escape(id)}"
81
- if append_rev == true
82
- couch_uri << "?rev=#{rev}"
83
- elsif append_rev.kind_of?(Integer)
84
- couch_uri << "?rev=#{append_rev}"
85
- end
86
- couch_uri
87
- end
88
-
89
- # Returns the document's database
90
- def database
91
- @database || self.class.database
92
- end
93
-
59
+
94
60
  end
61
+
95
62
 
96
63
  end