pupa 0.0.3 → 0.0.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/pupa/models/base.rb +20 -1
- data/lib/pupa/version.rb +1 -1
- data/spec/models/base_spec.rb +29 -14
- 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: b9732b4a0ea1dbd2eb9d8fff1d533bbfd397db6a
|
4
|
+
data.tar.gz: 659874bfd458daa7c09be7f0f2a5feeb9deec005
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f24268480ad0447df95bbfa82a86c13d4d82e015309932571b102b73813d480e52d35124ca900f94d9740d58d1b8f3f84e2b9b484dc8c3b879ab6efec8593683
|
7
|
+
data.tar.gz: e09836eb51002368554645177bbfbee70e91b3dd5900746e26aa10f3196adb5651e455b1d3a87617e325381169367debc5af0b3d3f2ace604aa8d517c8fc585a
|
data/lib/pupa/models/base.rb
CHANGED
@@ -152,7 +152,7 @@ module Pupa
|
|
152
152
|
def validate!
|
153
153
|
if self.class.json_schema
|
154
154
|
# JSON::Validator#initialize_data runs fastest if given a hash.
|
155
|
-
JSON::Validator.validate!(self.class.json_schema, to_h
|
155
|
+
JSON::Validator.validate!(self.class.json_schema, stringify_keys(to_h))
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
@@ -183,5 +183,24 @@ module Pupa
|
|
183
183
|
b.delete(:_id)
|
184
184
|
a == b
|
185
185
|
end
|
186
|
+
|
187
|
+
private
|
188
|
+
|
189
|
+
def stringify_keys(object)
|
190
|
+
case object
|
191
|
+
when Hash
|
192
|
+
{}.tap do |hash|
|
193
|
+
object.each do |key,value|
|
194
|
+
hash[key.to_s] = stringify_keys(value)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
when Array
|
198
|
+
object.map do |value|
|
199
|
+
stringify_keys(value)
|
200
|
+
end
|
201
|
+
else
|
202
|
+
object
|
203
|
+
end
|
204
|
+
end
|
186
205
|
end
|
187
206
|
end
|
data/lib/pupa/version.rb
CHANGED
data/spec/models/base_spec.rb
CHANGED
@@ -6,20 +6,26 @@ describe Pupa::Base do
|
|
6
6
|
self.schema = {
|
7
7
|
'$schema' => 'http://json-schema.org/draft-03/schema#',
|
8
8
|
'properties' => {
|
9
|
-
'
|
10
|
-
'
|
11
|
-
|
9
|
+
'links' => {
|
10
|
+
'items' => {
|
11
|
+
'properties' => {
|
12
|
+
'url' => {
|
13
|
+
'type' => 'string',
|
14
|
+
'format' => 'uri',
|
15
|
+
},
|
16
|
+
},
|
17
|
+
},
|
12
18
|
},
|
13
19
|
},
|
14
20
|
}
|
15
|
-
attr_accessor :name, :
|
21
|
+
attr_accessor :name, :label, :founding_date, :inactive, :label_id, :manager_id, :links
|
16
22
|
foreign_key :label_id, :manager_id
|
17
23
|
foreign_object :label
|
18
24
|
end
|
19
25
|
end
|
20
26
|
|
21
27
|
let :properties do
|
22
|
-
{name: 'Moderat',
|
28
|
+
{name: 'Moderat', label: {name: 'Mute'}, inactive: false, manager_id: '1', links: [{url: 'http://moderat.fm/'}]}
|
23
29
|
end
|
24
30
|
|
25
31
|
let :object do
|
@@ -28,7 +34,7 @@ describe Pupa::Base do
|
|
28
34
|
|
29
35
|
describe '#attr_accessor' do
|
30
36
|
it 'should add properties' do
|
31
|
-
Music::Band.properties.to_a.should == [:_id, :_type, :extras, :name, :
|
37
|
+
Music::Band.properties.to_a.should == [:_id, :_type, :extras, :name, :label, :founding_date, :inactive, :label_id, :manager_id, :links]
|
32
38
|
end
|
33
39
|
end
|
34
40
|
|
@@ -61,9 +67,15 @@ describe Pupa::Base do
|
|
61
67
|
Music::Band.json_schema.should == {
|
62
68
|
'$schema' => 'http://json-schema.org/draft-03/schema#',
|
63
69
|
'properties' => {
|
64
|
-
'
|
65
|
-
'
|
66
|
-
|
70
|
+
'links' => {
|
71
|
+
'items' => {
|
72
|
+
'properties' => {
|
73
|
+
'url' => {
|
74
|
+
'type' => 'string',
|
75
|
+
'format' => 'uri',
|
76
|
+
},
|
77
|
+
},
|
78
|
+
},
|
67
79
|
},
|
68
80
|
},
|
69
81
|
}
|
@@ -89,7 +101,10 @@ describe Pupa::Base do
|
|
89
101
|
|
90
102
|
it 'should set properties' do
|
91
103
|
object.name.should == 'Moderat'
|
92
|
-
object.
|
104
|
+
object.label.should == {name: 'Mute'}
|
105
|
+
object.inactive.should == false
|
106
|
+
object.manager_id.should == '1'
|
107
|
+
object.links.should == [{url: 'http://moderat.fm/'}]
|
93
108
|
end
|
94
109
|
end
|
95
110
|
|
@@ -135,7 +150,7 @@ describe Pupa::Base do
|
|
135
150
|
|
136
151
|
describe '#fingerprint' do
|
137
152
|
it 'should return the fingerprint' do
|
138
|
-
object.fingerprint.should == {_type: 'music/band', name: 'Moderat',
|
153
|
+
object.fingerprint.should == {_type: 'music/band', name: 'Moderat', inactive: false, manager_id: '1', links: [{url: 'http://moderat.fm/'}]}
|
139
154
|
end
|
140
155
|
end
|
141
156
|
|
@@ -159,18 +174,18 @@ describe Pupa::Base do
|
|
159
174
|
end
|
160
175
|
|
161
176
|
it 'should raise an error if the object is invalid' do
|
162
|
-
object[:url] = 'invalid'
|
177
|
+
object[:links][0][:url] = 'invalid'
|
163
178
|
expect{object.validate!}.to raise_error(JSON::Schema::ValidationError)
|
164
179
|
end
|
165
180
|
end
|
166
181
|
|
167
182
|
describe '#to_h' do
|
168
183
|
it 'should not include foreign objects by default' do
|
169
|
-
object.to_h.should == {_id: object._id, _type: 'music/band', name: 'Moderat',
|
184
|
+
object.to_h.should == {_id: object._id, _type: 'music/band', name: 'Moderat', inactive: false, manager_id: '1', links: [{url: 'http://moderat.fm/'}]}
|
170
185
|
end
|
171
186
|
|
172
187
|
it 'should include foreign objects if desired' do
|
173
|
-
object.to_h(include_foreign_objects: true).should == {_id: object._id, _type: 'music/band', name: 'Moderat',
|
188
|
+
object.to_h(include_foreign_objects: true).should == {_id: object._id, _type: 'music/band', name: 'Moderat', label: {name: 'Mute'}, inactive: false, manager_id: '1', links: [{url: 'http://moderat.fm/'}]}
|
174
189
|
end
|
175
190
|
|
176
191
|
it 'should not include blank properties' do
|