cot 0.5.2 → 0.5.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.
- checksums.yaml +8 -8
- data/.travis.yml +1 -1
- data/Gemfile.lock +1 -1
- data/Rakefile +1 -0
- data/lib/cot/collection.rb +3 -0
- data/lib/cot/version.rb +1 -1
- data/spec/lib/cot/collection_spec.rb +30 -18
- data/spec/spec_helper.rb +1 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MmQ0MWNjZWVjMTJkNDI3ZGJmNWFhNTFjZTkxZTc4NWE2MDRjMjRmNw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MzgzYmFlYjM3NjEzNzYxM2UxYTI1ZmRmYzk0NzIzZmVkMjEwZDAyYQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NjA1OWJmYWFmYzQ2OWNhNzdjZWUxMThhMmRlMDcxMWQ1OWI2MTYyOTY3ZjZi
|
10
|
+
Njc1ZTc5ZWQ2OWFlYzc5NjhjZDEwOGNmM2FkM2I2NmU3NjBlYTBlMGViOGEz
|
11
|
+
MWEyMGQwZWZhZmVjMGJhY2NkMDg0YTM4OGE2NDY1ZmIwMTZhNWY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NjA1ZTYxYzIxOTg2ZmQwOTM0ZTVlZDQ5YzFjMTkxZWMxODk2YTdiZmQ5ZmIx
|
14
|
+
N2E2NGU2ZmUxNzMwMjFjMTA3NDEyYjc5N2Y2ODVkNzlmMWQwMjgzYWRlZjMy
|
15
|
+
MjA3ZjNmNDY0Y2YyN2MzYzA3MzkwNzQ4M2ViMmJjYzA4OTE0YjM=
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
data/lib/cot/collection.rb
CHANGED
@@ -11,6 +11,9 @@ module Cot
|
|
11
11
|
def initialize(*params)
|
12
12
|
parse_params(params)
|
13
13
|
|
14
|
+
if self.class.klass.nil?
|
15
|
+
fail 'Collected class not set, please either pass a class to initialize or call collected_class'
|
16
|
+
end
|
14
17
|
# If you pass in different types of things here we can't be friends
|
15
18
|
initialize_objects(@objects) unless @objects.first.instance_of? self.class.klass
|
16
19
|
|
data/lib/cot/version.rb
CHANGED
@@ -4,8 +4,14 @@ class FakeDouble < Cot::Frame
|
|
4
4
|
property :foo
|
5
5
|
property :fooy
|
6
6
|
end
|
7
|
+
class ExampleCollection < Cot::Collection
|
8
|
+
end
|
7
9
|
|
8
10
|
describe Cot::Collection do
|
11
|
+
after :each do
|
12
|
+
ExampleCollection.klass = nil
|
13
|
+
end
|
14
|
+
|
9
15
|
let(:obj1) { FakeDouble.new id: 1, foo: :bar }
|
10
16
|
let(:obj2) { FakeDouble.new id: 2, foo: :bar }
|
11
17
|
subject(:collection) { Cot::Collection.new FakeDouble, [obj1, obj2] }
|
@@ -73,29 +79,29 @@ describe Cot::Collection do
|
|
73
79
|
context 'when passing a class' do
|
74
80
|
context 'with options' do
|
75
81
|
it 'takes an optional sub_key option to pull the object out of the payload' do
|
76
|
-
coll =
|
77
|
-
|
78
|
-
|
82
|
+
coll = ExampleCollection.new FakeDouble,
|
83
|
+
[{ inner: { fooy: :bar } }, { inner: { asdf: :fdas } }],
|
84
|
+
sub_key: :inner
|
79
85
|
expect(coll.first).to be_kind_of FakeDouble
|
80
86
|
expect(coll.first.fooy).to eq :bar
|
81
87
|
end
|
82
88
|
|
83
89
|
it 'takes options as strings and symbols' do
|
84
|
-
coll =
|
85
|
-
|
86
|
-
|
90
|
+
coll = ExampleCollection.new FakeDouble,
|
91
|
+
[{ inner: { fooy: :bar } }, { inner: { asdf: :fdas } }],
|
92
|
+
'sub_key' => :inner
|
87
93
|
expect(coll.first).to be_kind_of FakeDouble
|
88
94
|
expect(coll.first.fooy).to eq :bar
|
89
95
|
end
|
90
96
|
|
91
97
|
it 'takes an optional default_attributes option to add set attributes in every object.' do
|
92
|
-
coll =
|
98
|
+
coll = ExampleCollection.new FakeDouble, [{ fooy: :bar }, { asdf: :fdas }], default_attributes: { foo: :baz }
|
93
99
|
expect(coll).to all be_kind_of FakeDouble
|
94
100
|
expect(coll.map(&:foo).uniq).to eq [:baz]
|
95
101
|
end
|
96
102
|
|
97
103
|
it 'supports a legacy optional sub_key parameter to pull the object out of the payload' do
|
98
|
-
coll =
|
104
|
+
coll = ExampleCollection.new FakeDouble, [{ inner: { fooy: :bar } }, { inner: { asdf: :fdas } }], :inner
|
99
105
|
expect(coll.first).to be_kind_of FakeDouble
|
100
106
|
expect(coll.first.fooy).to eq :bar
|
101
107
|
end
|
@@ -103,22 +109,22 @@ describe Cot::Collection do
|
|
103
109
|
|
104
110
|
context 'without options' do
|
105
111
|
it 'does not process the objects if they are already the correct class' do
|
106
|
-
coll =
|
112
|
+
coll = ExampleCollection.new FakeDouble, [FakeDouble.new(fooy: :bar), FakeDouble.new(asdf: :fdas)]
|
107
113
|
expect(coll.first).to be_kind_of FakeDouble
|
108
114
|
end
|
109
115
|
|
110
116
|
it 'does not set the subkey if none is provided' do
|
111
|
-
coll =
|
117
|
+
coll = ExampleCollection.new FakeDouble, [FakeDouble.new(fooy: :bar), FakeDouble.new(asdf: :fdas)]
|
112
118
|
expect(coll.instance_variable_get :@options).to_not have_key :sub_key
|
113
119
|
end
|
114
120
|
|
115
121
|
it 'can accept empty hashes' do
|
116
|
-
coll =
|
122
|
+
coll = ExampleCollection.new FakeDouble, [{}]
|
117
123
|
expect(coll.first).to be_kind_of FakeDouble
|
118
124
|
end
|
119
125
|
|
120
126
|
it 'creates new instances of the passed klass if the objects are not already the class' do
|
121
|
-
coll =
|
127
|
+
coll = ExampleCollection.new FakeDouble, [{ fooy: :bar }, { asdf: :fdas }]
|
122
128
|
expect(coll.first).to be_kind_of FakeDouble
|
123
129
|
end
|
124
130
|
end
|
@@ -169,28 +175,34 @@ describe Cot::Collection do
|
|
169
175
|
|
170
176
|
context 'without options' do
|
171
177
|
it 'does not process the objects if they are already the correct class' do
|
172
|
-
coll =
|
178
|
+
coll = MyCollection.new [FakeDouble.new(fooy: :bar), FakeDouble.new(asdf: :fdas)]
|
173
179
|
expect(coll.first).to be_kind_of FakeDouble
|
174
180
|
end
|
175
181
|
|
176
182
|
it 'creates new instances of the passed klass if the objects are not already the class' do
|
177
|
-
coll =
|
183
|
+
coll = MyCollection.new [{ fooy: :bar }, { asdf: :fdas }]
|
178
184
|
expect(coll.first).to be_kind_of FakeDouble
|
179
185
|
end
|
180
186
|
end
|
181
187
|
end
|
188
|
+
|
189
|
+
it 'fails if no klass is set' do
|
190
|
+
class BadCollection < Cot::Collection; end
|
191
|
+
error = 'Collected class not set, please either pass a class to initialize or call collected_class'
|
192
|
+
expect { BadCollection.new }.to raise_error error
|
193
|
+
end
|
182
194
|
end
|
183
195
|
|
184
196
|
context 'update members' do
|
185
197
|
it 'updates members' do
|
186
|
-
coll =
|
198
|
+
coll = ExampleCollection.new FakeDouble, [{ fooy: :bar }]
|
187
199
|
expect(coll.length).to eq 1
|
188
200
|
coll.update_members [{ id: 123, foo: :bar }]
|
189
201
|
expect(coll.first.id).to eq 123
|
190
202
|
end
|
191
203
|
|
192
204
|
it 'removes members that are not in the payload' do
|
193
|
-
coll =
|
205
|
+
coll = ExampleCollection.new FakeDouble, [{ fooy: :bar }, { asdf: :fdas }]
|
194
206
|
expect(coll.length).to eq 2
|
195
207
|
coll.update_members [{ id: 123, foo: :bar }]
|
196
208
|
expect(coll.first.id).to eq 123
|
@@ -200,13 +212,13 @@ describe Cot::Collection do
|
|
200
212
|
|
201
213
|
context 'changed?' do
|
202
214
|
it 'returns true if one of the objects has changed' do
|
203
|
-
coll =
|
215
|
+
coll = ExampleCollection.new FakeDouble, [{ fooy: :bar }]
|
204
216
|
coll.first.fooy = 'baz'
|
205
217
|
expect(coll.changed?).to be true
|
206
218
|
end
|
207
219
|
|
208
220
|
it 'return false if none of the objects have changed' do
|
209
|
-
coll =
|
221
|
+
coll = ExampleCollection.new FakeDouble, [{ fooy: :bar }]
|
210
222
|
expect(coll.changed?).to be false
|
211
223
|
end
|
212
224
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joseph Henrich
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|