ivy-serializers 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d908e19f0785161aaf56472b179ef00f1e82946d
4
- data.tar.gz: cb49a7e6f89ff4d47457da9371f5857421f09683
3
+ metadata.gz: 965769bbd1ab1c1e7345b7767e4ac2f35ced0890
4
+ data.tar.gz: 8c8b913128925ad947f0810f05cb47d34d7c1f95
5
5
  SHA512:
6
- metadata.gz: 5d798714be3d66d8e57bf66919706b03bae4ff46b1925387a13d65a764e05f4c61cf8fa98cbb4de155ae20e4a07d27fce0e2cabc823b0b668d68af95e417e4c4
7
- data.tar.gz: 520e783e3b0c2860f9dd2a1997dfd1fe8bf9df9cc8667b663b0a3a9814c8ed4477e111499ee949378940f04d1626be3e1ca9473fd2199e7d0228a42bf14f6f63
6
+ metadata.gz: 5ccc6afa7b0b08e61cbb53876c8239192c60683fd54bf707e06e02e34bf2ce2b8ea86b0ad8cc40b6d87471b1ff6e531d1f9f5aed5b0ee0303f3a27631d1bbe0d
7
+ data.tar.gz: 255b0f79b33bec71c516fadcc0d471abec3e4e685a6b520b508076ac39f70033416813a6438fa54d813638dc6511ed492cd6eada2e44a91c0b4d74ff7ec7bf19
@@ -14,11 +14,11 @@ Gem::Specification.new do |spec|
14
14
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
15
  spec.require_paths = ['lib']
16
16
 
17
+ spec.add_dependency 'activesupport', '>= 2.2.1'
18
+ spec.add_dependency 'hash_generator', '~> 1.1'
19
+
17
20
  spec.add_development_dependency 'bundler', '~> 1.6'
18
21
  spec.add_development_dependency 'rake'
19
22
  spec.add_development_dependency 'rspec', '~> 3.2.0'
20
23
  spec.add_development_dependency 'simplecov', '~> 0.10.0'
21
-
22
- spec.add_dependency 'activesupport', '>= 2.2.1'
23
- spec.add_dependency 'hash_generator', '~> 1.1'
24
24
  end
@@ -2,4 +2,3 @@ require 'ivy/serializers/documents'
2
2
  require 'ivy/serializers/formats'
3
3
  require 'ivy/serializers/serializer'
4
4
  require 'ivy/serializers/version'
5
- require 'ivy/serializers/railtie' if defined?(::Rails)
@@ -1,3 +1,4 @@
1
+ require 'active_support/inflector'
1
2
  require 'ivy/serializers/formats/json'
2
3
 
3
4
  module Ivy
@@ -20,10 +21,18 @@ module Ivy
20
21
  if options[:polymorphic]
21
22
  @hash_gen.store_array(name) { polymorphic_resources(resources) }
22
23
  else
23
- ids(:"#{ActiveSupport::Inflector.singularize(name.to_s)}_ids", resources)
24
+ ids(:"#{singularize(name)}_ids", resources)
24
25
  end
25
26
  end
26
27
 
28
+ def primary_resource(primary_resource_name, primary_resource)
29
+ super(singularize(primary_resource_name).to_sym, primary_resource)
30
+ end
31
+
32
+ def primary_resources(primary_resources_name, primary_resources)
33
+ super(primary_resources_name, primary_resources)
34
+ end
35
+
27
36
  private
28
37
 
29
38
  def polymorphic_resource(resource)
@@ -34,6 +43,10 @@ module Ivy
34
43
  def polymorphic_resources(resources)
35
44
  resources.each { |resource| @hash_gen.push_object { polymorphic_resource(resource) } }
36
45
  end
46
+
47
+ def singularize(name)
48
+ ActiveSupport::Inflector.singularize(name.to_s)
49
+ end
37
50
  end
38
51
  end
39
52
  end
@@ -26,7 +26,11 @@ module Ivy
26
26
  end
27
27
 
28
28
  def primary_resource(primary_resource_name, primary_resource)
29
- @hash_gen.store_object(:data) { resource(primary_resource) }
29
+ super(:data, primary_resource)
30
+ end
31
+
32
+ def primary_resources(primary_resources_name, primary_resources)
33
+ super(:data, primary_resources)
30
34
  end
31
35
 
32
36
  def resource(resource)
@@ -1,5 +1,5 @@
1
1
  module Ivy
2
2
  module Serializers
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -5,14 +5,24 @@ RSpec.describe Ivy::Serializers::Formats::ActiveModelSerializers do
5
5
 
6
6
  describe '#as_json' do
7
7
  let(:registry) { Ivy::Serializers::Registry.new }
8
- let(:document) { Ivy::Serializers::Documents.create(registry, :post, post) }
8
+ let(:document) { Ivy::Serializers::Documents.create(registry, :posts, resource) }
9
9
 
10
10
  subject { format.as_json }
11
11
 
12
12
  context 'with default mapping' do
13
13
  let(:post) { double('post', :id => 1) }
14
14
 
15
- it { should eq(:post => {:id => 1}) }
15
+ context 'for an individual resource' do
16
+ let(:resource) { post }
17
+
18
+ it { should eq(:post => {:id => 1}) }
19
+ end
20
+
21
+ context 'for a resource collection' do
22
+ let(:resource) { [post] }
23
+
24
+ it { should eq(:posts => [{:id => 1}]) }
25
+ end
16
26
  end
17
27
 
18
28
  context 'with an attribute' do
@@ -26,7 +36,17 @@ RSpec.describe Ivy::Serializers::Formats::ActiveModelSerializers do
26
36
  end
27
37
  end
28
38
 
29
- it { should eq(:post => {:id => 1, :title => 'title'}) }
39
+ context 'for an individual resource' do
40
+ let(:resource) { post }
41
+
42
+ it { should eq(:post => {:id => 1, :title => 'title'}) }
43
+ end
44
+
45
+ context 'for a resource collection' do
46
+ let(:resource) { [post] }
47
+
48
+ it { should eq(:posts => [{:id => 1, :title => 'title'}]) }
49
+ end
30
50
  end
31
51
 
32
52
  context 'with a block provided' do
@@ -36,7 +56,17 @@ RSpec.describe Ivy::Serializers::Formats::ActiveModelSerializers do
36
56
  end
37
57
  end
38
58
 
39
- it { should eq(:post => {:id => 1, :headline => 'title'}) }
59
+ context 'for an individual resource' do
60
+ let(:resource) { post }
61
+
62
+ it { should eq(:post => {:id => 1, :headline => 'title'}) }
63
+ end
64
+
65
+ context 'for a resource collection' do
66
+ let(:resource) { [post] }
67
+
68
+ it { should eq(:posts => [{:id => 1, :headline => 'title'}]) }
69
+ end
40
70
  end
41
71
  end
42
72
 
@@ -53,7 +83,17 @@ RSpec.describe Ivy::Serializers::Formats::ActiveModelSerializers do
53
83
  end
54
84
  end
55
85
 
56
- it { should eq(:post => {:author_id => 1, :id => 1}) }
86
+ context 'for an individual resource' do
87
+ let(:resource) { post }
88
+
89
+ it { should eq(:post => {:author_id => 1, :id => 1}) }
90
+ end
91
+
92
+ context 'for a resource collection' do
93
+ let(:resource) { [post] }
94
+
95
+ it { should eq(:posts => [{:author_id => 1, :id => 1}]) }
96
+ end
57
97
  end
58
98
 
59
99
  context 'with a block provided' do
@@ -63,7 +103,17 @@ RSpec.describe Ivy::Serializers::Formats::ActiveModelSerializers do
63
103
  end
64
104
  end
65
105
 
66
- it { should eq(:post => {:id => 1, :user_id => 1}) }
106
+ context 'for an individual resource' do
107
+ let(:resource) { post }
108
+
109
+ it { should eq(:post => {:id => 1, :user_id => 1}) }
110
+ end
111
+
112
+ context 'for a resource collection' do
113
+ let(:resource) { [post] }
114
+
115
+ it { should eq(:posts => [{:id => 1, :user_id => 1}]) }
116
+ end
67
117
  end
68
118
 
69
119
  context 'with the :embed_in_root option' do
@@ -73,10 +123,23 @@ RSpec.describe Ivy::Serializers::Formats::ActiveModelSerializers do
73
123
  end
74
124
  end
75
125
 
76
- it { should eq(
77
- :authors => [{:id => 1}],
78
- :post => {:author_id => 1, :id => 1}
79
- ) }
126
+ context 'for an individual resource' do
127
+ let(:resource) { post }
128
+
129
+ it { should eq(
130
+ :authors => [{:id => 1}],
131
+ :post => {:author_id => 1, :id => 1}
132
+ ) }
133
+ end
134
+
135
+ context 'for a resource collection' do
136
+ let(:resource) { [post] }
137
+
138
+ it { should eq(
139
+ :authors => [{:id => 1}],
140
+ :posts => [{:author_id => 1, :id => 1}]
141
+ ) }
142
+ end
80
143
  end
81
144
 
82
145
  context 'with the :polymorphic option' do
@@ -86,7 +149,17 @@ RSpec.describe Ivy::Serializers::Formats::ActiveModelSerializers do
86
149
  end
87
150
  end
88
151
 
89
- it { should eq(:post => {:author => {:id => 1, :type => 'author'}, :id => 1}) }
152
+ context 'for an individual resource' do
153
+ let(:resource) { post }
154
+
155
+ it { should eq(:post => {:author => {:id => 1, :type => 'author'}, :id => 1}) }
156
+ end
157
+
158
+ context 'for a resource collection' do
159
+ let(:resource) { [post] }
160
+
161
+ it { should eq(:posts => [{:author => {:id => 1, :type => 'author'}, :id => 1}]) }
162
+ end
90
163
  end
91
164
  end
92
165
 
@@ -103,7 +176,17 @@ RSpec.describe Ivy::Serializers::Formats::ActiveModelSerializers do
103
176
  end
104
177
  end
105
178
 
106
- it { should eq(:post => {:comment_ids => [1], :id => 1}) }
179
+ context 'for an individual resource' do
180
+ let(:resource) { post }
181
+
182
+ it { should eq(:post => {:comment_ids => [1], :id => 1}) }
183
+ end
184
+
185
+ context 'for a resource collection' do
186
+ let(:resource) { [post] }
187
+
188
+ it { should eq(:posts => [{:comment_ids => [1], :id => 1}]) }
189
+ end
107
190
  end
108
191
 
109
192
  context 'with a block provided' do
@@ -113,7 +196,17 @@ RSpec.describe Ivy::Serializers::Formats::ActiveModelSerializers do
113
196
  end
114
197
  end
115
198
 
116
- it { should eq(:post => {:id => 1, :reply_ids => [1]}) }
199
+ context 'for an individual resource' do
200
+ let(:resource) { post }
201
+
202
+ it { should eq(:post => {:id => 1, :reply_ids => [1]}) }
203
+ end
204
+
205
+ context 'for a resource collection' do
206
+ let(:resource) { [post] }
207
+
208
+ it { should eq(:posts => [{:id => 1, :reply_ids => [1]}]) }
209
+ end
117
210
  end
118
211
 
119
212
  context 'with the :embed_in_root option' do
@@ -123,10 +216,23 @@ RSpec.describe Ivy::Serializers::Formats::ActiveModelSerializers do
123
216
  end
124
217
  end
125
218
 
126
- it { should eq(
127
- :comments => [{:id => 1}],
128
- :post => {:comment_ids => [1], :id => 1}
129
- ) }
219
+ context 'for an individual resource' do
220
+ let(:resource) { post }
221
+
222
+ it { should eq(
223
+ :comments => [{:id => 1}],
224
+ :post => {:comment_ids => [1], :id => 1}
225
+ ) }
226
+ end
227
+
228
+ context 'for a resource collection' do
229
+ let(:resource) { [post] }
230
+
231
+ it { should eq(
232
+ :comments => [{:id => 1}],
233
+ :posts => [{:comment_ids => [1], :id => 1}]
234
+ ) }
235
+ end
130
236
  end
131
237
 
132
238
  context 'with the :polymorphic option' do
@@ -136,7 +242,17 @@ RSpec.describe Ivy::Serializers::Formats::ActiveModelSerializers do
136
242
  end
137
243
  end
138
244
 
139
- it { should eq(:post => {:comments => [{:id => 1, :type => 'comment'}], :id => 1}) }
245
+ context 'for an individual resource' do
246
+ let(:resource) { post }
247
+
248
+ it { should eq(:post => {:comments => [{:id => 1, :type => 'comment'}], :id => 1}) }
249
+ end
250
+
251
+ context 'for a resource collection' do
252
+ let(:resource) { [post] }
253
+
254
+ it { should eq(:posts => [{:comments => [{:id => 1, :type => 'comment'}], :id => 1}]) }
255
+ end
140
256
  end
141
257
  end
142
258
  end
@@ -5,7 +5,7 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
5
5
 
6
6
  describe '#as_json' do
7
7
  let(:registry) { Ivy::Serializers::Registry.new }
8
- let(:document) { Ivy::Serializers::Documents.create(registry, :post, post) }
8
+ let(:document) { Ivy::Serializers::Documents.create(registry, :posts, resource) }
9
9
 
10
10
  subject { format.as_json }
11
11
 
@@ -13,13 +13,29 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
13
13
  let(:post_class) { double('Post', :name => 'Post') }
14
14
  let(:post) { double('post', :class => post_class, :id => 1) }
15
15
 
16
- it { should eq({
17
- :data => {
18
- :type => 'post',
19
- :id => '1',
20
- :links => {}
21
- }
22
- }) }
16
+ context 'for an individual resource' do
17
+ let(:resource) { post }
18
+
19
+ it { should eq({
20
+ :data => {
21
+ :type => 'post',
22
+ :id => '1',
23
+ :links => {}
24
+ }
25
+ }) }
26
+ end
27
+
28
+ context 'for a resource collection' do
29
+ let(:resource) { [post] }
30
+
31
+ it { should eq({
32
+ :data => [{
33
+ :type => 'post',
34
+ :id => '1',
35
+ :links => {}
36
+ }]
37
+ }) }
38
+ end
23
39
  end
24
40
 
25
41
  context 'with an attribute' do
@@ -33,14 +49,31 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
33
49
  end
34
50
  end
35
51
 
36
- it { should eq({
37
- :data => {
38
- :type => 'post',
39
- :id => '1',
40
- :title => 'title',
41
- :links => {}
42
- }
43
- }) }
52
+ context 'for an individual resource' do
53
+ let(:resource) { post }
54
+
55
+ it { should eq({
56
+ :data => {
57
+ :type => 'post',
58
+ :id => '1',
59
+ :title => 'title',
60
+ :links => {}
61
+ }
62
+ }) }
63
+ end
64
+
65
+ context 'for a resource collection' do
66
+ let(:resource) { [post] }
67
+
68
+ it { should eq({
69
+ :data => [{
70
+ :type => 'post',
71
+ :id => '1',
72
+ :title => 'title',
73
+ :links => {}
74
+ }]
75
+ }) }
76
+ end
44
77
  end
45
78
 
46
79
  context 'with a block provided' do
@@ -50,14 +83,31 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
50
83
  end
51
84
  end
52
85
 
53
- it { should eq({
54
- :data => {
55
- :type => 'post',
56
- :id => '1',
57
- :headline => 'title',
58
- :links => {}
59
- }
60
- }) }
86
+ context 'for an individual resource' do
87
+ let(:resource) { post }
88
+
89
+ it { should eq({
90
+ :data => {
91
+ :type => 'post',
92
+ :id => '1',
93
+ :headline => 'title',
94
+ :links => {}
95
+ }
96
+ }) }
97
+ end
98
+
99
+ context 'for a resource collection' do
100
+ let(:resource) { [post] }
101
+
102
+ it { should eq({
103
+ :data => [{
104
+ :type => 'post',
105
+ :id => '1',
106
+ :headline => 'title',
107
+ :links => {}
108
+ }]
109
+ }) }
110
+ end
61
111
  end
62
112
  end
63
113
 
@@ -74,17 +124,37 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
74
124
  end
75
125
  end
76
126
 
77
- it { should eq({
78
- :data => {
79
- :type => 'post',
80
- :id => '1',
81
- :links => {
82
- :author => {
83
- :linkage => {:id => '1', :type => 'author'}
127
+ context 'for an individual resource' do
128
+ let(:resource) { post }
129
+
130
+ it { should eq({
131
+ :data => {
132
+ :type => 'post',
133
+ :id => '1',
134
+ :links => {
135
+ :author => {
136
+ :linkage => {:id => '1', :type => 'author'}
137
+ }
84
138
  }
85
139
  }
86
- }
87
- }) }
140
+ }) }
141
+ end
142
+
143
+ context 'for a resource collection' do
144
+ let(:resource) { [post] }
145
+
146
+ it { should eq({
147
+ :data => [{
148
+ :type => 'post',
149
+ :id => '1',
150
+ :links => {
151
+ :author => {
152
+ :linkage => {:id => '1', :type => 'author'}
153
+ }
154
+ }
155
+ }]
156
+ }) }
157
+ end
88
158
  end
89
159
 
90
160
  context 'with a block provided' do
@@ -94,17 +164,37 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
94
164
  end
95
165
  end
96
166
 
97
- it { should eq({
98
- :data => {
99
- :type => 'post',
100
- :id => '1',
101
- :links => {
102
- :user => {
103
- :linkage => {:id => '1', :type => 'author'}
167
+ context 'for an individual resource' do
168
+ let(:resource) { post }
169
+
170
+ it { should eq({
171
+ :data => {
172
+ :type => 'post',
173
+ :id => '1',
174
+ :links => {
175
+ :user => {
176
+ :linkage => {:id => '1', :type => 'author'}
177
+ }
104
178
  }
105
179
  }
106
- }
107
- }) }
180
+ }) }
181
+ end
182
+
183
+ context 'for a resource collection' do
184
+ let(:resource) { [post] }
185
+
186
+ it { should eq({
187
+ :data => [{
188
+ :type => 'post',
189
+ :id => '1',
190
+ :links => {
191
+ :user => {
192
+ :linkage => {:id => '1', :type => 'author'}
193
+ }
194
+ }
195
+ }]
196
+ }) }
197
+ end
108
198
  end
109
199
 
110
200
  context 'with the :embed_in_root option' do
@@ -114,25 +204,53 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
114
204
  end
115
205
  end
116
206
 
117
- it { should eq({
118
- :data => {
119
- :type => 'post',
120
- :id => '1',
121
- :links => {
122
- :author => {
123
- :linkage => {:id => '1', :type => 'author'}
207
+ context 'for an individual resource' do
208
+ let(:resource) { post }
209
+
210
+ it { should eq({
211
+ :data => {
212
+ :type => 'post',
213
+ :id => '1',
214
+ :links => {
215
+ :author => {
216
+ :linkage => {:id => '1', :type => 'author'}
217
+ }
124
218
  }
219
+ },
220
+
221
+ :included => {
222
+ :authors => [{
223
+ :id => '1',
224
+ :links => {},
225
+ :type => 'author'
226
+ }]
125
227
  }
126
- },
228
+ }) }
229
+ end
127
230
 
128
- :included => {
129
- :authors => [{
231
+ context 'for a resource collection' do
232
+ let(:resource) { [post] }
233
+
234
+ it { should eq({
235
+ :data => [{
236
+ :type => 'post',
130
237
  :id => '1',
131
- :links => {},
132
- :type => 'author'
133
- }]
134
- }
135
- }) }
238
+ :links => {
239
+ :author => {
240
+ :linkage => {:id => '1', :type => 'author'}
241
+ }
242
+ }
243
+ }],
244
+
245
+ :included => {
246
+ :authors => [{
247
+ :id => '1',
248
+ :links => {},
249
+ :type => 'author'
250
+ }]
251
+ }
252
+ }) }
253
+ end
136
254
  end
137
255
 
138
256
  context 'with the :polymorphic option' do
@@ -142,17 +260,37 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
142
260
  end
143
261
  end
144
262
 
145
- it { should eq({
146
- :data => {
147
- :type => 'post',
148
- :id => '1',
149
- :links => {
150
- :author => {
151
- :linkage => {:id => '1', :type => 'author'}
263
+ context 'for an individual resource' do
264
+ let(:resource) { post }
265
+
266
+ it { should eq({
267
+ :data => {
268
+ :type => 'post',
269
+ :id => '1',
270
+ :links => {
271
+ :author => {
272
+ :linkage => {:id => '1', :type => 'author'}
273
+ }
152
274
  }
153
275
  }
154
- }
155
- }) }
276
+ }) }
277
+ end
278
+
279
+ context 'for a resource collection' do
280
+ let(:resource) { [post] }
281
+
282
+ it { should eq({
283
+ :data => [{
284
+ :type => 'post',
285
+ :id => '1',
286
+ :links => {
287
+ :author => {
288
+ :linkage => {:id => '1', :type => 'author'}
289
+ }
290
+ }
291
+ }]
292
+ }) }
293
+ end
156
294
  end
157
295
  end
158
296
 
@@ -169,17 +307,37 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
169
307
  end
170
308
  end
171
309
 
172
- it { should eq({
173
- :data => {
174
- :type => 'post',
175
- :id => '1',
176
- :links => {
177
- :comments => {
178
- :linkage => [{:id => '1', :type => 'comment'}]
310
+ context 'for an individual resource' do
311
+ let(:resource) { post }
312
+
313
+ it { should eq({
314
+ :data => {
315
+ :type => 'post',
316
+ :id => '1',
317
+ :links => {
318
+ :comments => {
319
+ :linkage => [{:id => '1', :type => 'comment'}]
320
+ }
179
321
  }
180
322
  }
181
- }
182
- }) }
323
+ }) }
324
+ end
325
+
326
+ context 'for a resource collection' do
327
+ let(:resource) { [post] }
328
+
329
+ it { should eq({
330
+ :data => [{
331
+ :type => 'post',
332
+ :id => '1',
333
+ :links => {
334
+ :comments => {
335
+ :linkage => [{:id => '1', :type => 'comment'}]
336
+ }
337
+ }
338
+ }]
339
+ }) }
340
+ end
183
341
  end
184
342
 
185
343
  context 'with a block provided' do
@@ -189,17 +347,37 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
189
347
  end
190
348
  end
191
349
 
192
- it { should eq({
193
- :data => {
194
- :type => 'post',
195
- :id => '1',
196
- :links => {
197
- :replies => {
198
- :linkage => [{:id => '1', :type => 'comment'}]
350
+ context 'for an individual resource' do
351
+ let(:resource) { post }
352
+
353
+ it { should eq({
354
+ :data => {
355
+ :type => 'post',
356
+ :id => '1',
357
+ :links => {
358
+ :replies => {
359
+ :linkage => [{:id => '1', :type => 'comment'}]
360
+ }
199
361
  }
200
362
  }
201
- }
202
- }) }
363
+ }) }
364
+ end
365
+
366
+ context 'for a resource collection' do
367
+ let(:resource) { [post] }
368
+
369
+ it { should eq({
370
+ :data => [{
371
+ :type => 'post',
372
+ :id => '1',
373
+ :links => {
374
+ :replies => {
375
+ :linkage => [{:id => '1', :type => 'comment'}]
376
+ }
377
+ }
378
+ }]
379
+ }) }
380
+ end
203
381
  end
204
382
 
205
383
  context 'with the :embed_in_root option' do
@@ -209,25 +387,53 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
209
387
  end
210
388
  end
211
389
 
212
- it { should eq({
213
- :data => {
214
- :type => 'post',
215
- :id => '1',
216
- :links => {
217
- :comments => {
218
- :linkage => [{:id => '1', :type => 'comment'}]
390
+ context 'for an individual resource' do
391
+ let(:resource) { post }
392
+
393
+ it { should eq({
394
+ :data => {
395
+ :type => 'post',
396
+ :id => '1',
397
+ :links => {
398
+ :comments => {
399
+ :linkage => [{:id => '1', :type => 'comment'}]
400
+ }
219
401
  }
402
+ },
403
+
404
+ :included => {
405
+ :comments => [{
406
+ :type => 'comment',
407
+ :id => '1',
408
+ :links => {}
409
+ }]
220
410
  }
221
- },
411
+ }) }
412
+ end
222
413
 
223
- :included => {
224
- :comments => [{
225
- :type => 'comment',
414
+ context 'for a resource collection' do
415
+ let(:resource) { [post] }
416
+
417
+ it { should eq({
418
+ :data => [{
419
+ :type => 'post',
226
420
  :id => '1',
227
- :links => {}
228
- }]
229
- }
230
- }) }
421
+ :links => {
422
+ :comments => {
423
+ :linkage => [{:id => '1', :type => 'comment'}]
424
+ }
425
+ }
426
+ }],
427
+
428
+ :included => {
429
+ :comments => [{
430
+ :type => 'comment',
431
+ :id => '1',
432
+ :links => {}
433
+ }]
434
+ }
435
+ }) }
436
+ end
231
437
  end
232
438
 
233
439
  context 'with the :polymorphic option' do
@@ -237,17 +443,37 @@ RSpec.describe Ivy::Serializers::Formats::JSONAPI do
237
443
  end
238
444
  end
239
445
 
240
- it { should eq({
241
- :data => {
242
- :type => 'post',
243
- :id => '1',
244
- :links => {
245
- :comments => {
246
- :linkage => [{:id => '1', :type => 'comment'}]
446
+ context 'for an individual resource' do
447
+ let(:resource) { post }
448
+
449
+ it { should eq({
450
+ :data => {
451
+ :type => 'post',
452
+ :id => '1',
453
+ :links => {
454
+ :comments => {
455
+ :linkage => [{:id => '1', :type => 'comment'}]
456
+ }
247
457
  }
248
458
  }
249
- }
250
- }) }
459
+ }) }
460
+ end
461
+
462
+ context 'for a resource collection' do
463
+ let(:resource) { [post] }
464
+
465
+ it { should eq({
466
+ :data => [{
467
+ :type => 'post',
468
+ :id => '1',
469
+ :links => {
470
+ :comments => {
471
+ :linkage => [{:id => '1', :type => 'comment'}]
472
+ }
473
+ }
474
+ }]
475
+ }) }
476
+ end
251
477
  end
252
478
  end
253
479
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ivy-serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dray Lacy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-01 00:00:00.000000000 Z
11
+ date: 2015-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: hash_generator
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,34 +94,6 @@ dependencies:
66
94
  - - "~>"
67
95
  - !ruby/object:Gem::Version
68
96
  version: 0.10.0
69
- - !ruby/object:Gem::Dependency
70
- name: activesupport
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: 2.2.1
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: 2.2.1
83
- - !ruby/object:Gem::Dependency
84
- name: hash_generator
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '1.1'
90
- type: :runtime
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '1.1'
97
97
  description:
98
98
  email:
99
99
  - dray@envylabs.com
@@ -111,7 +111,6 @@ files:
111
111
  - ivy-serializers.gemspec
112
112
  - lib/ivy-serializers.rb
113
113
  - lib/ivy/serializers.rb
114
- - lib/ivy/serializers/action_controller/serialization_support.rb
115
114
  - lib/ivy/serializers/attribute.rb
116
115
  - lib/ivy/serializers/documents.rb
117
116
  - lib/ivy/serializers/documents/document.rb
@@ -122,7 +121,6 @@ files:
122
121
  - lib/ivy/serializers/formats/json.rb
123
122
  - lib/ivy/serializers/formats/json_api.rb
124
123
  - lib/ivy/serializers/mapping.rb
125
- - lib/ivy/serializers/railtie.rb
126
124
  - lib/ivy/serializers/registry.rb
127
125
  - lib/ivy/serializers/relationships.rb
128
126
  - lib/ivy/serializers/relationships/belongs_to.rb
@@ -1,35 +0,0 @@
1
- module Ivy
2
- module Serializers
3
- module ActionController
4
- module SerializationSupport
5
- extend ActiveSupport::Concern
6
-
7
- class SerializerNotFound < ::StandardError
8
- end
9
-
10
- included do
11
- class_attribute :serializer, :instance_writer => false
12
- class_attribute :serialization_format, :instance_writer => false
13
- self.serialization_format = Formats::ActiveModelSerializers
14
- end
15
-
16
- private
17
-
18
- %w[ _render_option_json _render_with_renderer_json ].each do |renderer_method|
19
- define_method(renderer_method) do |resource, options|
20
- unless serializer
21
- raise SerializerNotFound, "No serializer found in #{self.class.name}"
22
- end
23
-
24
- unless resource.kind_of?(::Hash)
25
- resource = Documents.create(serializer, controller_name, resource)
26
- resource = serialization_format.new(resource)
27
- end
28
-
29
- super(resource, options)
30
- end
31
- end
32
- end
33
- end
34
- end
35
- end
@@ -1,13 +0,0 @@
1
- require 'ivy/serializers/action_controller/serialization_support'
2
-
3
- module Ivy
4
- module Serializers
5
- class Railtie < ::Rails::Railtie
6
- initializer 'ivy.serializers.controllers' do
7
- ActiveSupport.on_load(:action_controller) do
8
- include ::Ivy::Serializers::ActionController::SerializationSupport
9
- end
10
- end
11
- end
12
- end
13
- end