couch_potato 1.6.3 → 1.6.4
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.
- checksums.yaml +4 -4
- data/lib/couch_potato/persistence/properties.rb +1 -1
- data/lib/couch_potato/version.rb +1 -1
- data/spec/unit/attributes_spec.rb +76 -44
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 838a4fbf0f5af5fd3306a1cc2284d9c077397de1
|
4
|
+
data.tar.gz: 76306355648a9447a58c24b0e802f2f79bcc86e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a914b059c4f92b4141eea0b8278d308a8101ba555f76db3fd83d11020f03605bc71553a99889cc0260da27b7421c347c7ebbca76bafe30843b18e2cd852ef9df
|
7
|
+
data.tar.gz: 13eae7928a7a82b7102cf2d9b47da78352d225e01b28192015a18b63232afe410c29b220ed8012aabe8b1f09ab2dc33a90d5bbf120a129dd3516eb3944de9a5c
|
data/lib/couch_potato/version.rb
CHANGED
@@ -6,54 +6,61 @@ class Branch
|
|
6
6
|
property :leafs
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
9
|
class Plant
|
11
10
|
include CouchPotato::Persistence
|
12
11
|
property :leaf_count
|
13
|
-
property :typed_leaf_count, :
|
14
|
-
property :typed_leaf_size, :
|
15
|
-
property :branch, :
|
12
|
+
property :typed_leaf_count, type: Fixnum
|
13
|
+
property :typed_leaf_size, type: Float
|
14
|
+
property :branch, type: Branch
|
15
|
+
end
|
16
|
+
|
17
|
+
class SpecialPlant < Plant
|
16
18
|
end
|
17
19
|
|
18
20
|
|
19
|
-
describe
|
21
|
+
describe 'attributes' do
|
20
22
|
context 'attributes=' do
|
21
|
-
it
|
23
|
+
it 'assigns the attributes' do
|
22
24
|
plant = Plant.new
|
23
|
-
plant.attributes = {:
|
25
|
+
plant.attributes = {leaf_count: 1}
|
26
|
+
|
24
27
|
expect(plant.leaf_count).to eq(1)
|
25
28
|
end
|
26
29
|
|
27
|
-
it
|
30
|
+
it 'assigns the attributes via []=' do
|
28
31
|
plant = Plant.new
|
29
32
|
plant[:leaf_count] = 1
|
33
|
+
|
30
34
|
expect(plant.leaf_count).to eq(1)
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
34
|
-
context
|
35
|
-
it
|
36
|
-
plant = Plant.new(:
|
37
|
-
|
38
|
-
|
38
|
+
context 'attributes' do
|
39
|
+
it 'returns the attributes' do
|
40
|
+
plant = Plant.new(leaf_count: 1)
|
41
|
+
|
42
|
+
expect(plant.attributes).to eq('leaf_count' => 1, 'created_at' => nil,
|
43
|
+
'updated_at' => nil, 'typed_leaf_count' => nil,
|
44
|
+
'typed_leaf_size' => nil, 'branch' => nil)
|
39
45
|
end
|
40
46
|
|
41
|
-
it
|
42
|
-
plant = Plant.new(:
|
47
|
+
it 'returns the attributes via [symbol]' do
|
48
|
+
plant = Plant.new(leaf_count: 1)
|
49
|
+
|
43
50
|
expect(plant.attributes[:leaf_count]).to eql(plant[:leaf_count])
|
44
51
|
expect(plant.attributes[:leaf_count]).to eql(1)
|
45
52
|
end
|
46
53
|
|
47
|
-
it
|
54
|
+
it 'returns the attributes via [string]' do
|
48
55
|
plant = Plant.new(leaf_count: 1)
|
49
56
|
|
50
|
-
expect(plant.attributes[
|
51
|
-
expect(plant.attributes[
|
57
|
+
expect(plant.attributes['leaf_count']).to eql(plant[:leaf_count])
|
58
|
+
expect(plant.attributes['leaf_count']).to eql(1)
|
52
59
|
end
|
53
60
|
end
|
54
61
|
|
55
|
-
context
|
56
|
-
it '
|
62
|
+
context 'has_key?' do
|
63
|
+
it 'responds to has_key?' do
|
57
64
|
plant = Plant.new(leaf_count: 1)
|
58
65
|
|
59
66
|
expect(plant.has_key?(:leaf_count)).to be_truthy
|
@@ -61,20 +68,21 @@ describe "attributes" do
|
|
61
68
|
end
|
62
69
|
|
63
70
|
# useful when loading models from custom views
|
64
|
-
context
|
65
|
-
it
|
66
|
-
plant = Plant.json_create(
|
71
|
+
context 'accessing ghost attributes' do
|
72
|
+
it 'allows me to access attributes that are in the couchdb document but not defined as a property' do
|
73
|
+
plant = Plant.json_create(JSON.create_id => 'Plant', 'color' => 'red', 'leaf_count' => 1)
|
74
|
+
|
67
75
|
expect(plant.color).to eq('red')
|
68
76
|
end
|
69
77
|
|
70
|
-
it
|
71
|
-
plant = Plant.json_create(
|
78
|
+
it 'raises a no method error when trying to read attributes that are not in the document' do
|
79
|
+
plant = Plant.json_create(JSON.create_id => 'Plant', 'leaf_count' => 1)
|
72
80
|
expect do
|
73
81
|
plant.length
|
74
82
|
end.to raise_error(NoMethodError)
|
75
83
|
end
|
76
84
|
|
77
|
-
it "
|
85
|
+
it "raises a no method error if the document hasn't been loaded from the database" do
|
78
86
|
plant = Plant.new
|
79
87
|
expect do
|
80
88
|
plant.length
|
@@ -82,6 +90,14 @@ describe "attributes" do
|
|
82
90
|
end
|
83
91
|
end
|
84
92
|
|
93
|
+
context 'inherited attributes' do
|
94
|
+
it 'returns inherited attributes' do
|
95
|
+
plant = SpecialPlant.new _document: {leaf_count: 1}
|
96
|
+
|
97
|
+
expect(plant.leaf_count).to eq(1)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
85
101
|
context 'typed attributes' do
|
86
102
|
before(:each) do
|
87
103
|
@plant = Plant.new
|
@@ -89,90 +105,106 @@ describe "attributes" do
|
|
89
105
|
|
90
106
|
context 'nested objects' do
|
91
107
|
it 'assigns the attributes of nested objects' do
|
92
|
-
expect(Plant.new(:
|
108
|
+
expect(Plant.new(branch: {leafs: 3}).branch.leafs).to eq(3)
|
93
109
|
end
|
94
110
|
end
|
95
111
|
|
96
|
-
describe
|
112
|
+
describe 'fixnum' do
|
97
113
|
it 'rounds a float to a fixnum' do
|
98
114
|
@plant.typed_leaf_count = 4.5
|
115
|
+
|
99
116
|
expect(@plant.typed_leaf_count).to eq(5)
|
100
117
|
end
|
101
118
|
|
102
|
-
it
|
119
|
+
it 'converts a string into a fixnum' do
|
103
120
|
@plant.typed_leaf_count = '4'
|
121
|
+
|
104
122
|
expect(@plant.typed_leaf_count).to eq(4)
|
105
123
|
end
|
106
124
|
|
107
|
-
it
|
125
|
+
it 'converts a string into a negative fixnum' do
|
108
126
|
@plant.typed_leaf_count = '-4'
|
127
|
+
|
109
128
|
expect(@plant.typed_leaf_count).to eq(-4)
|
110
129
|
end
|
111
130
|
|
112
|
-
it
|
131
|
+
it 'leaves a fixnum as is' do
|
113
132
|
@plant.typed_leaf_count = 4
|
133
|
+
|
114
134
|
expect(@plant.typed_leaf_count).to eq(4)
|
115
135
|
end
|
116
136
|
|
117
|
-
it
|
137
|
+
it 'leaves nil as is' do
|
118
138
|
@plant.typed_leaf_count = nil
|
139
|
+
|
119
140
|
expect(@plant.typed_leaf_count).to be_nil
|
120
141
|
end
|
121
142
|
|
122
|
-
it
|
143
|
+
it 'sets the attributes to zero if a string given' do
|
123
144
|
@plant.typed_leaf_count = 'x'
|
145
|
+
|
124
146
|
expect(@plant.typed_leaf_count).to eq(0)
|
125
147
|
end
|
126
148
|
|
127
|
-
it
|
149
|
+
it 'parses numbers out of a string' do
|
128
150
|
@plant.typed_leaf_count = 'x123'
|
151
|
+
|
129
152
|
expect(@plant.typed_leaf_count).to eq(123)
|
130
153
|
end
|
131
154
|
|
132
|
-
it
|
155
|
+
it 'sets the attributes to nil if given a blank string' do
|
133
156
|
@plant.typed_leaf_count = ''
|
157
|
+
|
134
158
|
expect(@plant.typed_leaf_count).to be_nil
|
135
159
|
end
|
136
160
|
end
|
137
161
|
|
138
|
-
context
|
139
|
-
it
|
162
|
+
context 'float' do
|
163
|
+
it 'converts a number in a string with a decimal place' do
|
140
164
|
@plant.typed_leaf_size = '0.5001'
|
165
|
+
|
141
166
|
expect(@plant.typed_leaf_size).to eq(0.5001)
|
142
167
|
end
|
143
168
|
|
144
|
-
it
|
169
|
+
it 'converts a number in a string without a decimal place' do
|
145
170
|
@plant.typed_leaf_size = '5'
|
171
|
+
|
146
172
|
expect(@plant.typed_leaf_size).to eq(5.0)
|
147
173
|
end
|
148
174
|
|
149
|
-
it
|
175
|
+
it 'converts a negative number in a string' do
|
150
176
|
@plant.typed_leaf_size = '-5.0'
|
177
|
+
|
151
178
|
expect(@plant.typed_leaf_size).to eq(-5.0)
|
152
179
|
end
|
153
180
|
|
154
|
-
it
|
181
|
+
it 'leaves a float as it is' do
|
155
182
|
@plant.typed_leaf_size = 0.5
|
183
|
+
|
156
184
|
expect(@plant.typed_leaf_size).to eq(0.5)
|
157
185
|
end
|
158
186
|
|
159
|
-
it
|
187
|
+
it 'leaves nil as is' do
|
160
188
|
@plant.typed_leaf_size = nil
|
189
|
+
|
161
190
|
expect(@plant.typed_leaf_size).to be_nil
|
162
191
|
end
|
163
192
|
|
164
|
-
it
|
193
|
+
it 'sets the attributes to zero if a string given' do
|
165
194
|
@plant.typed_leaf_size = 'x'
|
195
|
+
|
166
196
|
expect(@plant.typed_leaf_size).to eq(0)
|
167
197
|
end
|
168
198
|
|
169
|
-
it
|
199
|
+
it 'parses numbers out of a string' do
|
170
200
|
@plant.typed_leaf_size = 'x00.123'
|
201
|
+
|
171
202
|
expect(@plant.typed_leaf_size).to eq(0.123)
|
172
203
|
end
|
173
204
|
|
174
|
-
it
|
205
|
+
it 'sets the attributes to nil if given a blank string' do
|
175
206
|
@plant.typed_leaf_size = ''
|
207
|
+
|
176
208
|
expect(@plant.typed_leaf_size).to be_nil
|
177
209
|
end
|
178
210
|
end
|