mattetti-couchrest 0.16 → 0.17
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,15 @@
|
|
1
1
|
module CouchRest
|
2
2
|
class Response < Hash
|
3
|
-
def initialize(
|
4
|
-
|
3
|
+
def initialize(pkeys = {})
|
4
|
+
pkeys ||= {}
|
5
|
+
pkeys.each do |k,v|
|
5
6
|
self[k.to_s] = v
|
6
7
|
end
|
7
8
|
end
|
8
|
-
def []=
|
9
|
+
def []=(key, value)
|
9
10
|
super(key.to_s, value)
|
10
11
|
end
|
11
|
-
def []
|
12
|
+
def [](key)
|
12
13
|
super(key.to_s)
|
13
14
|
end
|
14
15
|
end
|
@@ -55,9 +55,14 @@ module CouchRest
|
|
55
55
|
else
|
56
56
|
# Let people use :send as a Time parse arg
|
57
57
|
klass = ::CouchRest.constantize(target)
|
58
|
+
# I'm not convince we should or should not create a new instance if we are casting a doc/extended doc without default value and nothing was passed
|
59
|
+
# unless (property.casted &&
|
60
|
+
# (klass.superclass == CouchRest::ExtendedDocument || klass.superclass == CouchRest::Document) &&
|
61
|
+
# (self[key].nil? || property.default.nil?))
|
58
62
|
klass.send(property.init_method, self[key])
|
63
|
+
#end
|
59
64
|
end
|
60
|
-
self[
|
65
|
+
self[property.name].casted_by = self if self[property.name].respond_to?(:casted_by)
|
61
66
|
end
|
62
67
|
end
|
63
68
|
end
|
@@ -133,7 +133,9 @@ module CouchRest
|
|
133
133
|
fetch_view(name, opts, &block)
|
134
134
|
else
|
135
135
|
begin
|
136
|
-
|
136
|
+
# auto load mentioned documents unless asked differently
|
137
|
+
opts.merge({:include_docs => true}) unless opts.has_key?(:include_docs)
|
138
|
+
view = fetch_view name, opts, &block
|
137
139
|
view['rows'].collect{|r|new(r['doc'])} if view['rows']
|
138
140
|
rescue
|
139
141
|
# fallback for old versions of couchdb that don't
|
@@ -24,15 +24,17 @@ module CouchRest
|
|
24
24
|
subklass.send(:include, CouchRest::Mixins::Properties)
|
25
25
|
end
|
26
26
|
|
27
|
+
# Accessors
|
28
|
+
attr_accessor :casted_by
|
29
|
+
|
27
30
|
# Callbacks
|
28
31
|
define_callbacks :create
|
29
32
|
define_callbacks :save
|
30
33
|
define_callbacks :update
|
31
34
|
define_callbacks :destroy
|
32
35
|
|
33
|
-
def initialize(
|
36
|
+
def initialize(passed_keys={})
|
34
37
|
apply_defaults # defined in CouchRest::Mixins::Properties
|
35
|
-
keys ||= {}
|
36
38
|
super
|
37
39
|
cast_keys # defined in CouchRest::Mixins::Properties
|
38
40
|
unless self['_id'] && self['_rev']
|
data/lib/couchrest.rb
CHANGED
@@ -27,7 +27,7 @@ require 'couchrest/monkeypatches'
|
|
27
27
|
|
28
28
|
# = CouchDB, close to the metal
|
29
29
|
module CouchRest
|
30
|
-
VERSION = '0.
|
30
|
+
VERSION = '0.17' unless self.const_defined?("VERSION")
|
31
31
|
|
32
32
|
autoload :Server, 'couchrest/core/server'
|
33
33
|
autoload :Database, 'couchrest/core/database'
|
@@ -124,18 +124,30 @@ module CouchRest
|
|
124
124
|
cr.database(parsed[:database])
|
125
125
|
end
|
126
126
|
|
127
|
-
def put
|
127
|
+
def put(uri, doc = nil)
|
128
128
|
payload = doc.to_json if doc
|
129
|
-
|
129
|
+
begin
|
130
|
+
JSON.parse(RestClient.put(uri, payload))
|
131
|
+
rescue Exception => e
|
132
|
+
raise "Error while sending a PUT request #{uri}\npayload: #{payload.inspect}\n#{e}"
|
133
|
+
end
|
130
134
|
end
|
131
135
|
|
132
|
-
def get
|
133
|
-
|
136
|
+
def get(uri)
|
137
|
+
begin
|
138
|
+
JSON.parse(RestClient.get(uri), :max_nesting => false)
|
139
|
+
rescue => e
|
140
|
+
raise "Error while sending a GET request #{uri}\n: #{e}"
|
141
|
+
end
|
134
142
|
end
|
135
143
|
|
136
144
|
def post uri, doc = nil
|
137
145
|
payload = doc.to_json if doc
|
138
|
-
|
146
|
+
begin
|
147
|
+
JSON.parse(RestClient.post(uri, payload))
|
148
|
+
rescue Exception => e
|
149
|
+
raise "Error while sending a POST request #{uri}\npayload: #{payload.inspect}\n#{e}"
|
150
|
+
end
|
139
151
|
end
|
140
152
|
|
141
153
|
def delete uri
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
|
2
|
+
require File.join(FIXTURE_PATH, 'more', 'card')
|
3
|
+
|
4
|
+
class Car < CouchRest::ExtendedDocument
|
5
|
+
use_database TEST_SERVER.default_database
|
6
|
+
|
7
|
+
property :name
|
8
|
+
property :driver, :cast_as => 'Driver'
|
9
|
+
end
|
10
|
+
|
11
|
+
class Driver < CouchRest::ExtendedDocument
|
12
|
+
use_database TEST_SERVER.default_database
|
13
|
+
# You have to add a casted_by accessor if you want to reach a casted extended doc parent
|
14
|
+
attr_accessor :casted_by
|
15
|
+
|
16
|
+
property :name
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "casting an extended document" do
|
20
|
+
|
21
|
+
before(:each) do
|
22
|
+
@car = Car.new(:name => 'Renault 306')
|
23
|
+
@driver = Driver.new(:name => 'Matt')
|
24
|
+
end
|
25
|
+
|
26
|
+
# it "should not create an empty casted object" do
|
27
|
+
# @car.driver.should be_nil
|
28
|
+
# end
|
29
|
+
|
30
|
+
it "should let you assign the casted attribute after instantializing an object" do
|
31
|
+
@car.driver = @driver
|
32
|
+
@car.driver.name.should == 'Matt'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should let the casted document who casted it" do
|
36
|
+
Car.new(:name => 'Renault 306', :driver => @driver)
|
37
|
+
@car.driver.casted_by.should == @car
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mattetti-couchrest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.17"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- J. Chris Anderson
|
@@ -136,6 +136,7 @@ files:
|
|
136
136
|
- spec/couchrest/helpers/pager_spec.rb
|
137
137
|
- spec/couchrest/helpers/streamer_spec.rb
|
138
138
|
- spec/couchrest/more
|
139
|
+
- spec/couchrest/more/casted_extended_doc_spec.rb
|
139
140
|
- spec/couchrest/more/casted_model_spec.rb
|
140
141
|
- spec/couchrest/more/extended_doc_attachment_spec.rb
|
141
142
|
- spec/couchrest/more/extended_doc_spec.rb
|