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.
- data/README.md +10 -34
- data/Rakefile +5 -2
- data/bin/couchdir +20 -0
- data/examples/model/example.rb +13 -19
- data/examples/word_count/word_count.rb +24 -3
- data/examples/word_count/word_count_query.rb +6 -7
- data/lib/couchrest/core/database.rb +49 -126
- data/lib/couchrest/core/document.rb +25 -58
- data/lib/couchrest/core/model.rb +612 -0
- data/lib/couchrest/core/server.rb +10 -47
- data/lib/couchrest/core/validations.rb +328 -0
- data/lib/couchrest/monkeypatches.rb +0 -95
- data/lib/couchrest.rb +10 -57
- data/spec/couchrest/core/database_spec.rb +68 -183
- data/spec/couchrest/core/design_spec.rb +1 -1
- data/spec/couchrest/core/document_spec.rb +104 -285
- data/spec/couchrest/core/model_spec.rb +855 -0
- data/spec/couchrest/helpers/pager_spec.rb +1 -1
- data/spec/spec_helper.rb +7 -13
- metadata +17 -83
- data/examples/word_count/word_count_views.rb +0 -26
- data/lib/couchrest/core/response.rb +0 -16
- data/lib/couchrest/mixins/attachments.rb +0 -31
- data/lib/couchrest/mixins/callbacks.rb +0 -483
- data/lib/couchrest/mixins/design_doc.rb +0 -64
- data/lib/couchrest/mixins/document_queries.rb +0 -48
- data/lib/couchrest/mixins/extended_attachments.rb +0 -68
- data/lib/couchrest/mixins/extended_document_mixins.rb +0 -6
- data/lib/couchrest/mixins/properties.rb +0 -125
- data/lib/couchrest/mixins/validation.rb +0 -234
- data/lib/couchrest/mixins/views.rb +0 -168
- data/lib/couchrest/mixins.rb +0 -4
- data/lib/couchrest/more/casted_model.rb +0 -28
- data/lib/couchrest/more/extended_document.rb +0 -217
- data/lib/couchrest/more/property.rb +0 -40
- data/lib/couchrest/support/blank.rb +0 -42
- data/lib/couchrest/support/class.rb +0 -191
- data/lib/couchrest/validation/auto_validate.rb +0 -163
- data/lib/couchrest/validation/contextual_validators.rb +0 -78
- data/lib/couchrest/validation/validation_errors.rb +0 -118
- data/lib/couchrest/validation/validators/absent_field_validator.rb +0 -74
- data/lib/couchrest/validation/validators/confirmation_validator.rb +0 -99
- data/lib/couchrest/validation/validators/format_validator.rb +0 -117
- data/lib/couchrest/validation/validators/formats/email.rb +0 -66
- data/lib/couchrest/validation/validators/formats/url.rb +0 -43
- data/lib/couchrest/validation/validators/generic_validator.rb +0 -120
- data/lib/couchrest/validation/validators/length_validator.rb +0 -134
- data/lib/couchrest/validation/validators/method_validator.rb +0 -89
- data/lib/couchrest/validation/validators/numeric_validator.rb +0 -104
- data/lib/couchrest/validation/validators/required_field_validator.rb +0 -109
- data/spec/couchrest/core/server_spec.rb +0 -35
- data/spec/couchrest/more/casted_extended_doc_spec.rb +0 -40
- data/spec/couchrest/more/casted_model_spec.rb +0 -98
- data/spec/couchrest/more/extended_doc_attachment_spec.rb +0 -130
- data/spec/couchrest/more/extended_doc_spec.rb +0 -509
- data/spec/couchrest/more/extended_doc_view_spec.rb +0 -207
- data/spec/couchrest/more/property_spec.rb +0 -130
- data/spec/couchrest/support/class_spec.rb +0 -59
- data/spec/fixtures/more/article.rb +0 -34
- data/spec/fixtures/more/card.rb +0 -20
- data/spec/fixtures/more/course.rb +0 -14
- data/spec/fixtures/more/event.rb +0 -6
- data/spec/fixtures/more/invoice.rb +0 -17
- data/spec/fixtures/more/person.rb +0 -8
- data/spec/fixtures/more/question.rb +0 -6
- data/spec/fixtures/more/service.rb +0 -12
@@ -1,43 +1,44 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
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
|
-
#
|
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.
|
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.
|
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
|