couchrest 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/history.txt +5 -0
- data/lib/couchrest/attributes.rb +15 -8
- data/lib/couchrest/version.rb +1 -1
- data/spec/couchrest/document_spec.rb +14 -0
- metadata +1 -1
data/history.txt
CHANGED
data/lib/couchrest/attributes.rb
CHANGED
@@ -20,26 +20,25 @@ module CouchRest
|
|
20
20
|
# that the attributes hash has been initialized before
|
21
21
|
# you attempt to use it.
|
22
22
|
def initialize(attrs = nil)
|
23
|
-
@_attributes = {}
|
24
23
|
attrs.each{|k,v| self[k] = v} unless attrs.nil?
|
25
24
|
end
|
26
25
|
|
27
26
|
# Hash equivilent methods to access the attributes
|
28
|
-
def_delegators
|
27
|
+
def_delegators :_attributes, :to_a, :==, :eql?, :keys, :values, :each,
|
29
28
|
:reject, :reject!, :empty?, :clear, :merge, :merge!,
|
30
29
|
:encode_json, :as_json, :to_json, :frozen?
|
31
30
|
|
32
31
|
def []=(key, value)
|
33
|
-
|
32
|
+
_attributes[key.to_s] = value
|
34
33
|
end
|
35
34
|
def [](key)
|
36
|
-
|
35
|
+
_attributes[key.to_s]
|
37
36
|
end
|
38
37
|
def has_key?(key)
|
39
|
-
|
38
|
+
_attributes.has_key?(key.to_s)
|
40
39
|
end
|
41
40
|
def delete(key)
|
42
|
-
|
41
|
+
_attributes.delete(key.to_s)
|
43
42
|
end
|
44
43
|
def dup
|
45
44
|
new = super
|
@@ -52,14 +51,14 @@ module CouchRest
|
|
52
51
|
new
|
53
52
|
end
|
54
53
|
def to_hash
|
55
|
-
|
54
|
+
_attributes
|
56
55
|
end
|
57
56
|
|
58
57
|
# Freeze the object's attributes instead of the actual document.
|
59
58
|
# This prevents further modifications to stored data, but does allow access
|
60
59
|
# to local variables useful for callbacks or cached data.
|
61
60
|
def freeze
|
62
|
-
|
61
|
+
_attributes.freeze; self
|
63
62
|
end
|
64
63
|
|
65
64
|
# Provide details of the current keys in the reponse. Based on ActiveRecord::Base.
|
@@ -70,6 +69,14 @@ module CouchRest
|
|
70
69
|
"#<#{self.class} #{attributes_as_nice_string}>"
|
71
70
|
end
|
72
71
|
|
72
|
+
protected
|
73
|
+
|
74
|
+
# Define accessor for _attributes hash that will instantiate the
|
75
|
+
# model if this has not already been done.
|
76
|
+
def _attributes
|
77
|
+
@_attributes ||= {}
|
78
|
+
end
|
79
|
+
|
73
80
|
end
|
74
81
|
|
75
82
|
end
|
data/lib/couchrest/version.rb
CHANGED
@@ -27,8 +27,22 @@ describe CouchRest::Document do
|
|
27
27
|
@doc['_id'].should eql('sample')
|
28
28
|
@doc['foo'].should eql('bar')
|
29
29
|
end
|
30
|
+
|
31
|
+
context "replacing initalize" do
|
32
|
+
it "should not raise error" do
|
33
|
+
klass = Class.new(CouchRest::Document)
|
34
|
+
klass.class_eval do
|
35
|
+
def initialize; end # don't do anything, just overwrite
|
36
|
+
end
|
37
|
+
expect {
|
38
|
+
@doc = klass.new
|
39
|
+
@doc['test'] = 'sample'
|
40
|
+
}.to_not raise_error
|
41
|
+
end
|
42
|
+
end
|
30
43
|
end
|
31
44
|
|
45
|
+
|
32
46
|
describe "hash methods" do
|
33
47
|
it "should respond to forwarded hash methods" do
|
34
48
|
@doc = CouchRest::Document.new(:foo => 'bar')
|