yaks 0.6.0.alpha.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +46 -0
- data/lib/yaks/attributes.rb +45 -0
- data/lib/yaks/collection_resource.rb +1 -45
- data/lib/yaks/config/dsl.rb +15 -6
- data/lib/yaks/config.rb +1 -1
- data/lib/yaks/configurable.rb +20 -0
- data/lib/yaks/default_policy.rb +0 -8
- data/lib/yaks/errors.rb +3 -0
- data/lib/yaks/format/hal.rb +13 -2
- data/lib/yaks/format/halo.rb +29 -0
- data/lib/yaks/mapper/association.rb +18 -13
- data/lib/yaks/mapper/association_mapper.rb +1 -1
- data/lib/yaks/mapper/attribute.rb +1 -3
- data/lib/yaks/mapper/class_methods.rb +24 -16
- data/lib/yaks/mapper/config.rb +13 -28
- data/lib/yaks/mapper/control.rb +38 -0
- data/lib/yaks/mapper/has_many.rb +4 -8
- data/lib/yaks/mapper/link.rb +10 -2
- data/lib/yaks/mapper.rb +11 -2
- data/lib/yaks/resource/control.rb +11 -0
- data/lib/yaks/resource/link.rb +1 -7
- data/lib/yaks/resource.rb +9 -13
- data/lib/yaks/runner.rb +2 -4
- data/lib/yaks/serializer.rb +12 -0
- data/lib/yaks/stateful_builder.rb +54 -0
- data/lib/yaks/version.rb +1 -1
- data/lib/yaks.rb +16 -7
- data/spec/acceptance/acceptance_spec.rb +14 -4
- data/spec/acceptance/models.rb +9 -0
- data/spec/integration/map_to_resource_spec.rb +1 -1
- data/spec/json/confucius.halo.json +80 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/support/shared_contexts.rb +1 -1
- data/spec/unit/yaks/attributes_spec.rb +65 -0
- data/spec/unit/yaks/collection_mapper_spec.rb +1 -1
- data/spec/unit/yaks/collection_resource_spec.rb +7 -25
- data/spec/unit/yaks/config/dsl_spec.rb +5 -0
- data/spec/unit/yaks/config_spec.rb +2 -1
- data/spec/unit/yaks/configurable_spec.rb +25 -0
- data/spec/unit/yaks/default_policy_spec.rb +1 -7
- data/spec/unit/yaks/format/collection_json_spec.rb +1 -1
- data/spec/unit/yaks/format/hal_spec.rb +2 -2
- data/spec/unit/yaks/format/html_spec.rb +5 -0
- data/spec/unit/yaks/format/json_api_spec.rb +1 -1
- data/spec/unit/yaks/format_spec.rb +1 -5
- data/spec/unit/yaks/mapper/association_mapper_spec.rb +2 -2
- data/spec/unit/yaks/mapper/association_spec.rb +27 -3
- data/spec/unit/yaks/mapper/class_methods_spec.rb +17 -1
- data/spec/unit/yaks/mapper/config_spec.rb +16 -8
- data/spec/unit/yaks/mapper/has_many_spec.rb +15 -2
- data/spec/unit/yaks/mapper/has_one_spec.rb +1 -1
- data/spec/unit/yaks/mapper/link_spec.rb +12 -2
- data/spec/unit/yaks/mapper_spec.rb +51 -22
- data/spec/unit/yaks/resource/link_spec.rb +2 -1
- data/spec/unit/yaks/resource_spec.rb +16 -9
- data/spec/unit/yaks/runner_spec.rb +22 -2
- data/spec/unit/yaks/serializer_spec.rb +20 -0
- data/spec/unit/yaks/stateful_builder_spec.rb +41 -0
- data/yaks.gemspec +1 -0
- metadata +39 -11
- data/lib/yaks/fp/hash_updatable.rb +0 -19
- data/lib/yaks/fp/updatable.rb +0 -17
- data/spec/unit/yaks/fp/hash_updatable_spec.rb +0 -22
- data/spec/unit/yaks/fp/updatable_spec.rb +0 -22
@@ -4,6 +4,7 @@ RSpec.describe Yaks::Mapper::ClassMethods do
|
|
4
4
|
subject(:mapper_class) do
|
5
5
|
Class.new do
|
6
6
|
extend Yaks::Mapper::ClassMethods
|
7
|
+
config Yaks::Mapper::Config.new
|
7
8
|
attributes :foo, :bar
|
8
9
|
link :some_rel, 'http://some_link'
|
9
10
|
has_one :thing
|
@@ -53,7 +54,7 @@ RSpec.describe Yaks::Mapper::ClassMethods do
|
|
53
54
|
|
54
55
|
it 'should register links' do
|
55
56
|
expect(mapper_class.config.links).to eq [
|
56
|
-
Yaks::Mapper::Link.new(:some_rel, 'http://some_link'
|
57
|
+
Yaks::Mapper::Link.new(rel: :some_rel, template: 'http://some_link')
|
57
58
|
]
|
58
59
|
end
|
59
60
|
|
@@ -64,4 +65,19 @@ RSpec.describe Yaks::Mapper::ClassMethods do
|
|
64
65
|
]
|
65
66
|
end
|
66
67
|
|
68
|
+
describe '#config' do
|
69
|
+
it 'should allow getting the config' do
|
70
|
+
expect(subject.config).to be_a Yaks::Mapper::Config
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should allow setting the config' do
|
74
|
+
subject.config(:foo)
|
75
|
+
expect(subject.config).to be :foo
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'raise an exception when the config is nil' do
|
79
|
+
expect { subject.config(nil) }.to raise_exception
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
67
83
|
end
|
@@ -3,10 +3,18 @@ require 'spec_helper'
|
|
3
3
|
RSpec.describe Yaks::Mapper::Config do
|
4
4
|
Undefined = Yaks::Undefined
|
5
5
|
|
6
|
-
subject(:config) { described_class.new
|
6
|
+
subject(:config) { described_class.new }
|
7
7
|
|
8
8
|
describe '#initialize' do
|
9
|
-
subject(:config)
|
9
|
+
subject(:config) do
|
10
|
+
described_class.new(
|
11
|
+
type: 'foo',
|
12
|
+
attributes: [:a],
|
13
|
+
links: [:b],
|
14
|
+
associations: [:c]
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
10
18
|
|
11
19
|
its(:type) { should eql 'foo' }
|
12
20
|
its(:attributes) { should eql [:a] }
|
@@ -58,8 +66,8 @@ RSpec.describe Yaks::Mapper::Config do
|
|
58
66
|
}
|
59
67
|
|
60
68
|
it 'should have the links in the link list' do
|
61
|
-
expect(config.links).to include Yaks::Mapper::Link.new(:profile, '/profile/foo'
|
62
|
-
expect(config.links).to include Yaks::Mapper::Link.new(:self, '/foo/bar/{id}'
|
69
|
+
expect(config.links).to include Yaks::Mapper::Link.new(rel: :profile, template: '/profile/foo')
|
70
|
+
expect(config.links).to include Yaks::Mapper::Link.new(rel: :self, template: '/foo/bar/{id}')
|
63
71
|
end
|
64
72
|
end
|
65
73
|
|
@@ -72,8 +80,8 @@ RSpec.describe Yaks::Mapper::Config do
|
|
72
80
|
|
73
81
|
it 'should have the links in the defined order' do
|
74
82
|
expect(config.links).to eql [
|
75
|
-
Yaks::Mapper::Link.new(:self, '/foo/self'
|
76
|
-
Yaks::Mapper::Link.new(:self, '/foo/me'
|
83
|
+
Yaks::Mapper::Link.new(rel: :self, template: '/foo/self'),
|
84
|
+
Yaks::Mapper::Link.new(rel: :self, template: '/foo/me')
|
77
85
|
]
|
78
86
|
end
|
79
87
|
end
|
@@ -85,7 +93,7 @@ RSpec.describe Yaks::Mapper::Config do
|
|
85
93
|
|
86
94
|
it 'should have the association configured' do
|
87
95
|
expect(config.associations).to eq [
|
88
|
-
Yaks::Mapper::HasOne.new(name: :mother,
|
96
|
+
Yaks::Mapper::HasOne.new(name: :mother, item_mapper: Yaks::Mapper)
|
89
97
|
]
|
90
98
|
end
|
91
99
|
end
|
@@ -118,7 +126,7 @@ RSpec.describe Yaks::Mapper::Config do
|
|
118
126
|
|
119
127
|
it 'should have the association configured' do
|
120
128
|
expect(config.associations).to eq [
|
121
|
-
Yaks::Mapper::HasMany.new(name: :shoes,
|
129
|
+
Yaks::Mapper::HasMany.new(name: :shoes, item_mapper: Yaks::Mapper)
|
122
130
|
]
|
123
131
|
end
|
124
132
|
end
|
@@ -10,7 +10,7 @@ RSpec.describe Yaks::Mapper::HasMany do
|
|
10
10
|
type 'closet'
|
11
11
|
has_many :shoes,
|
12
12
|
rel: 'http://foo/shoes',
|
13
|
-
|
13
|
+
item_mapper: Class.new(Yaks::Mapper) { type 'shoe' ; attributes :size, :color }
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -70,12 +70,25 @@ RSpec.describe Yaks::Mapper::HasMany do
|
|
70
70
|
|
71
71
|
describe '#collection_mapper' do
|
72
72
|
let(:collection_mapper) { Yaks::Undefined }
|
73
|
-
|
73
|
+
|
74
|
+
subject(:has_many) { described_class.new(name: :name, item_mapper: :mapper, rel: :rel, collection_mapper: collection_mapper) }
|
74
75
|
|
75
76
|
context 'when the collection mapper is undefined' do
|
76
77
|
it 'should derive one from collection and policy' do
|
77
78
|
expect(has_many.collection_mapper([], Yaks::DefaultPolicy.new)).to equal Yaks::CollectionMapper
|
78
79
|
end
|
80
|
+
|
81
|
+
it 'should return nil if no policy is given' do
|
82
|
+
expect(has_many.collection_mapper([])).to be_nil
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should return nil if no collection is given' do
|
86
|
+
expect(has_many.collection_mapper(nil, Yaks::DefaultPolicy.new)).to be_nil
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should return nil if no params are given' do
|
90
|
+
expect(has_many.collection_mapper).to be_nil
|
91
|
+
end
|
79
92
|
end
|
80
93
|
|
81
94
|
context 'when the collection mapper is specified' do
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
RSpec.describe Yaks::Mapper::Link do
|
4
4
|
include_context 'yaks context'
|
5
5
|
|
6
|
-
subject(:link) { described_class.
|
6
|
+
subject(:link) { described_class.create(rel, template, options) }
|
7
7
|
|
8
8
|
let(:rel) { :next }
|
9
9
|
let(:template) { '/foo/bar/{x}/{y}' }
|
@@ -29,7 +29,7 @@ RSpec.describe Yaks::Mapper::Link do
|
|
29
29
|
describe '#add_to_resource' do
|
30
30
|
it 'should add itself to the resource' do
|
31
31
|
expect(link.add_to_resource(Yaks::Resource.new, mapper, yaks_context)).to eql(
|
32
|
-
Yaks::Resource.new(links: [Yaks::Resource::Link.new(:next, "/foo/bar/3/4"
|
32
|
+
Yaks::Resource.new(links: [Yaks::Resource::Link.new(rel: :next, uri: "/foo/bar/3/4")])
|
33
33
|
)
|
34
34
|
end
|
35
35
|
|
@@ -193,4 +193,14 @@ RSpec.describe Yaks::Mapper::Link do
|
|
193
193
|
end
|
194
194
|
end
|
195
195
|
|
196
|
+
describe '.create' do
|
197
|
+
it 'should take positional arguments' do
|
198
|
+
expect(Yaks::Mapper::Link.create(:foo, :bar, {baz: 3}))
|
199
|
+
.to eql Yaks::Mapper::Link.new(rel: :foo, template: :bar, options: {baz: 3})
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'should default options' do
|
203
|
+
expect(Yaks::Mapper::Link.create(:foo, :bar).options).to eql({})
|
204
|
+
end
|
205
|
+
end
|
196
206
|
end
|
@@ -51,12 +51,12 @@ RSpec.describe Yaks::Mapper do
|
|
51
51
|
|
52
52
|
it 'should map the link' do
|
53
53
|
expect(resource.links).to eq [
|
54
|
-
Yaks::Resource::Link.new(:profile, 'http://foo/bar'
|
54
|
+
Yaks::Resource::Link.new(rel: :profile, uri: 'http://foo/bar')
|
55
55
|
]
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'should use the link in the resource' do
|
59
|
-
expect(resource.links).to include Yaks::Resource::Link.new(:profile, 'http://foo/bar'
|
59
|
+
expect(resource.links).to include Yaks::Resource::Link.new(rel: :profile, uri: 'http://foo/bar')
|
60
60
|
end
|
61
61
|
|
62
62
|
context 'with the same link rel defined multiple times' do
|
@@ -70,10 +70,10 @@ RSpec.describe Yaks::Mapper do
|
|
70
70
|
|
71
71
|
it 'should map all the links' do
|
72
72
|
expect(resource.links).to eq [
|
73
|
-
Yaks::Resource::Link.new(:profile, 'http://foo/bar'
|
74
|
-
Yaks::Resource::Link.new(:self, 'http://foo/bam'
|
75
|
-
Yaks::Resource::Link.new(:self, 'http://foo/baz'
|
76
|
-
Yaks::Resource::Link.new(:self, 'http://foo/baq'
|
73
|
+
Yaks::Resource::Link.new(rel: :profile, uri: 'http://foo/bar'),
|
74
|
+
Yaks::Resource::Link.new(rel: :self, uri: 'http://foo/bam'),
|
75
|
+
Yaks::Resource::Link.new(rel: :self, uri: 'http://foo/baz'),
|
76
|
+
Yaks::Resource::Link.new(rel: :self, uri: 'http://foo/baq')
|
77
77
|
]
|
78
78
|
end
|
79
79
|
end
|
@@ -201,7 +201,34 @@ RSpec.describe Yaks::Mapper do
|
|
201
201
|
)
|
202
202
|
end
|
203
203
|
end
|
204
|
-
|
204
|
+
|
205
|
+
context 'with a control' do
|
206
|
+
before do
|
207
|
+
mapper_class.module_eval do
|
208
|
+
control :foo_control do
|
209
|
+
field :bar_field, label: 'a label', type: 'number', value: 7
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
it 'should render the control' do
|
215
|
+
expect(mapper.call(fake))
|
216
|
+
.to eql Yaks::Resource.new(
|
217
|
+
type: 'foo',
|
218
|
+
controls: [
|
219
|
+
Yaks::Resource::Control.new(
|
220
|
+
name: :foo_control,
|
221
|
+
fields: [
|
222
|
+
Yaks::Resource::Control::Field.new(
|
223
|
+
name: :bar_field,
|
224
|
+
label: 'a label',
|
225
|
+
type: 'number',
|
226
|
+
value: 7
|
227
|
+
)])])
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
end # describe '#call'
|
205
232
|
|
206
233
|
describe '.mapper_name' do
|
207
234
|
context 'with a type configured' do
|
@@ -267,30 +294,32 @@ RSpec.describe Yaks::Mapper do
|
|
267
294
|
end
|
268
295
|
end
|
269
296
|
|
270
|
-
|
271
|
-
let(:link) { fake('Link') }
|
272
|
-
|
297
|
+
shared_examples 'something that can be added to a resource' do
|
273
298
|
it 'should receive a context' do
|
274
|
-
stub(
|
299
|
+
stub(object).add_to_resource(any_args) {|r,_,_| Yaks::Resource.new}
|
275
300
|
|
276
|
-
mapper.config.links[0..-1] = [link]
|
277
301
|
mapper.call(instance)
|
278
302
|
|
279
|
-
expect(
|
303
|
+
expect(object).to have_received.add_to_resource(any(Yaks::Resource), mapper, yaks_context)
|
280
304
|
end
|
281
305
|
end
|
282
306
|
|
283
|
-
describe '#
|
284
|
-
let(:
|
285
|
-
|
286
|
-
|
287
|
-
|
307
|
+
describe '#map_links' do
|
308
|
+
let(:object) { fake('Link') }
|
309
|
+
before { mapper.config(mapper.config.append_to(:links, object)) }
|
310
|
+
it_should_behave_like 'something that can be added to a resource'
|
311
|
+
end
|
288
312
|
|
289
|
-
|
290
|
-
|
313
|
+
describe '#map_subresources' do
|
314
|
+
let(:object) { fake('Association') }
|
315
|
+
before { mapper.config(mapper.config.append_to(:associations, object)) }
|
316
|
+
it_should_behave_like 'something that can be added to a resource'
|
317
|
+
end
|
291
318
|
|
292
|
-
|
293
|
-
|
319
|
+
describe '#map_controls' do
|
320
|
+
let(:object) { fake('Control') }
|
321
|
+
before { mapper.config(mapper.config.append_to(:controls, object)) }
|
322
|
+
it_should_behave_like 'something that can be added to a resource'
|
294
323
|
end
|
295
324
|
|
296
325
|
describe '#mapper_stack' do
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Yaks::Resource::Link do
|
4
|
-
subject(:link) { described_class.new(rel, uri, options) }
|
4
|
+
subject(:link) { described_class.new(rel: rel, uri: uri, options: options) }
|
5
|
+
|
5
6
|
let(:rel) { :foo_rel }
|
6
7
|
let(:uri) { 'http://api.example.org/rel/foo' }
|
7
8
|
let(:options) { { title: 'mr. spectacular' } }
|
@@ -33,18 +33,18 @@ RSpec.describe Yaks::Resource do
|
|
33
33
|
let(:init_opts) {
|
34
34
|
{
|
35
35
|
links: [
|
36
|
-
Yaks::Resource::Link.new(:profile, '/foo/bar/profile'
|
37
|
-
Yaks::Resource::Link.new(:self, '/foo/bar'
|
36
|
+
Yaks::Resource::Link.new(rel: :profile, uri: '/foo/bar/profile'),
|
37
|
+
Yaks::Resource::Link.new(rel: :self, uri: '/foo/bar')
|
38
38
|
]
|
39
39
|
}
|
40
40
|
}
|
41
41
|
its(:links) { should eql [
|
42
|
-
Yaks::Resource::Link.new(:profile, '/foo/bar/profile'
|
43
|
-
Yaks::Resource::Link.new(:self, '/foo/bar'
|
42
|
+
Yaks::Resource::Link.new(rel: :profile, uri: '/foo/bar/profile'),
|
43
|
+
Yaks::Resource::Link.new(rel: :self, uri: '/foo/bar')
|
44
44
|
]
|
45
45
|
}
|
46
46
|
|
47
|
-
its(:self_link) { should eql Yaks::Resource::Link.new(:self, '/foo/bar'
|
47
|
+
its(:self_link) { should eql Yaks::Resource::Link.new(rel: :self, uri: '/foo/bar') }
|
48
48
|
end
|
49
49
|
|
50
50
|
context 'with subresources' do
|
@@ -95,14 +95,21 @@ RSpec.describe Yaks::Resource do
|
|
95
95
|
let(:init_opts) {
|
96
96
|
{ links:
|
97
97
|
[
|
98
|
-
Yaks::Resource::Link.new(:self, 'foo'
|
99
|
-
Yaks::Resource::Link.new(:self, 'bar'
|
100
|
-
Yaks::Resource::Link.new(:profile, 'baz'
|
98
|
+
Yaks::Resource::Link.new(rel: :self, uri: 'foo'),
|
99
|
+
Yaks::Resource::Link.new(rel: :self, uri: 'bar'),
|
100
|
+
Yaks::Resource::Link.new(rel: :profile, uri: 'baz')
|
101
101
|
]
|
102
102
|
}
|
103
103
|
}
|
104
104
|
it 'should return the last self link' do
|
105
|
-
expect(resource.self_link).to eql Yaks::Resource::Link.new(:self, 'bar'
|
105
|
+
expect(resource.self_link).to eql Yaks::Resource::Link.new(rel: :self, uri: 'bar')
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe '#add_control' do
|
110
|
+
it 'should append to the controls' do
|
111
|
+
expect(resource.add_control(:a_control))
|
112
|
+
.to eq Yaks::Resource.new(controls: [:a_control])
|
106
113
|
end
|
107
114
|
end
|
108
115
|
end
|
@@ -69,13 +69,13 @@ RSpec.describe Yaks::Runner do
|
|
69
69
|
end
|
70
70
|
|
71
71
|
it 'should pick the one given in the options if no header matches' do
|
72
|
-
rack_env = { 'HTTP_ACCEPT' => 'text/
|
72
|
+
rack_env = { 'HTTP_ACCEPT' => 'text/xml, application/json' }
|
73
73
|
runner = described_class.new(object: nil, config: config, options: { format: :hal, env: rack_env })
|
74
74
|
expect(runner.format_class).to equal Yaks::Format::Hal
|
75
75
|
end
|
76
76
|
|
77
77
|
it 'should fall back to the default when no mime type is recognized' do
|
78
|
-
rack_env = { 'HTTP_ACCEPT' => 'text/
|
78
|
+
rack_env = { 'HTTP_ACCEPT' => 'text/xml, application/json' }
|
79
79
|
runner = described_class.new(object: nil, config: config, options: { env: rack_env })
|
80
80
|
expect(runner.format_class).to equal Yaks::Format::CollectionJson
|
81
81
|
end
|
@@ -253,6 +253,26 @@ RSpec.describe Yaks::Runner do
|
|
253
253
|
end
|
254
254
|
end
|
255
255
|
|
256
|
+
describe '#primitivizer' do
|
257
|
+
describe 'with a non json based format' do
|
258
|
+
let(:config) do
|
259
|
+
Yaks::Config.new do
|
260
|
+
default_format :html
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
it 'should return the identity function' do
|
265
|
+
expect(runner.primitivizer.call(:foo)).to eql :foo
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
describe 'with a json based format' do
|
270
|
+
it 'should return the primitivizer' do
|
271
|
+
expect(runner.primitivizer.call(:foo)).to eql "foo"
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
256
276
|
it 'should memoize' do
|
257
277
|
expect(runner.formatter).to be runner.formatter
|
258
278
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Yaks::Serializer do
|
4
|
+
after do
|
5
|
+
Yaks::Serializer.instance_variable_set("@serializers", nil)
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'allows registering serializers' do
|
9
|
+
Yaks::Serializer.register(:some_format, :some_serializer)
|
10
|
+
expect(Yaks::Serializer.all[:some_format]).to equal :some_serializer
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should by default have a serializer for JSON' do
|
14
|
+
expect(Yaks::Serializer.all[:json].call([1,2,3])).to eql "[\n 1,\n 2,\n 3\n]"
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should warn when registering a key again' do
|
18
|
+
expect { Yaks::Serializer.register(:json, :foo) }.to raise_exception /Serializer for json already registered/
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Yaks::StatefulBuilder do
|
4
|
+
class Buildable
|
5
|
+
include Yaks::Attributes.new(:foo, :bar)
|
6
|
+
|
7
|
+
def self.create(foo, bar)
|
8
|
+
new(foo: foo, bar: bar)
|
9
|
+
end
|
10
|
+
|
11
|
+
def finalize
|
12
|
+
update(foo: 7, bar: 8)
|
13
|
+
end
|
14
|
+
|
15
|
+
def wrong_type(x, y)
|
16
|
+
"foo #{x} #{y}"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
subject { Yaks::StatefulBuilder.new(Buildable, [:foo, :bar, :update, :finalize, :wrong_type]) }
|
22
|
+
|
23
|
+
it 'should keep state' do
|
24
|
+
expect(
|
25
|
+
subject.create(3, 4) do
|
26
|
+
foo 7
|
27
|
+
update bar: 6
|
28
|
+
end.to_h
|
29
|
+
).to eql(foo: 7, bar: 6)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should unwrap again' do
|
33
|
+
expect( subject.create(3, 4) { finalize } ).to eql Buildable.new(foo: 7, bar: 8)
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'kind_of?' do
|
37
|
+
it 'should test if the returned thing is of the right type' do
|
38
|
+
expect { subject.create(3, 4) { wrong_type(1,'2') }}.to raise_exception Yaks::IllegalState, 'Buildable#wrong_type(1, "2") returned "foo 1 2". Expected instance of Buildable'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/yaks.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.0
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arne Brasseur
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: inflection
|
@@ -220,6 +220,20 @@ dependencies:
|
|
220
220
|
- - ">="
|
221
221
|
- !ruby/object:Gem::Version
|
222
222
|
version: '0'
|
223
|
+
- !ruby/object:Gem::Dependency
|
224
|
+
name: yaks-html
|
225
|
+
requirement: !ruby/object:Gem::Requirement
|
226
|
+
requirements:
|
227
|
+
- - ">="
|
228
|
+
- !ruby/object:Gem::Version
|
229
|
+
version: '0'
|
230
|
+
type: :development
|
231
|
+
prerelease: false
|
232
|
+
version_requirements: !ruby/object:Gem::Requirement
|
233
|
+
requirements:
|
234
|
+
- - ">="
|
235
|
+
- !ruby/object:Gem::Version
|
236
|
+
version: '0'
|
223
237
|
description: Serialize to hypermedia. HAL, JSON-API, etc.
|
224
238
|
email:
|
225
239
|
- arne@arnebrasseur.net
|
@@ -231,34 +245,40 @@ files:
|
|
231
245
|
- README.md
|
232
246
|
- Rakefile
|
233
247
|
- lib/yaks.rb
|
248
|
+
- lib/yaks/attributes.rb
|
234
249
|
- lib/yaks/breaking_changes.rb
|
235
250
|
- lib/yaks/collection_mapper.rb
|
236
251
|
- lib/yaks/collection_resource.rb
|
237
252
|
- lib/yaks/config.rb
|
238
253
|
- lib/yaks/config/dsl.rb
|
254
|
+
- lib/yaks/configurable.rb
|
239
255
|
- lib/yaks/default_policy.rb
|
256
|
+
- lib/yaks/errors.rb
|
240
257
|
- lib/yaks/format.rb
|
241
258
|
- lib/yaks/format/collection_json.rb
|
242
259
|
- lib/yaks/format/hal.rb
|
260
|
+
- lib/yaks/format/halo.rb
|
243
261
|
- lib/yaks/format/json_api.rb
|
244
262
|
- lib/yaks/fp.rb
|
245
263
|
- lib/yaks/fp/callable.rb
|
246
|
-
- lib/yaks/fp/hash_updatable.rb
|
247
|
-
- lib/yaks/fp/updatable.rb
|
248
264
|
- lib/yaks/mapper.rb
|
249
265
|
- lib/yaks/mapper/association.rb
|
250
266
|
- lib/yaks/mapper/association_mapper.rb
|
251
267
|
- lib/yaks/mapper/attribute.rb
|
252
268
|
- lib/yaks/mapper/class_methods.rb
|
253
269
|
- lib/yaks/mapper/config.rb
|
270
|
+
- lib/yaks/mapper/control.rb
|
254
271
|
- lib/yaks/mapper/has_many.rb
|
255
272
|
- lib/yaks/mapper/has_one.rb
|
256
273
|
- lib/yaks/mapper/link.rb
|
257
274
|
- lib/yaks/null_resource.rb
|
258
275
|
- lib/yaks/primitivize.rb
|
259
276
|
- lib/yaks/resource.rb
|
277
|
+
- lib/yaks/resource/control.rb
|
260
278
|
- lib/yaks/resource/link.rb
|
261
279
|
- lib/yaks/runner.rb
|
280
|
+
- lib/yaks/serializer.rb
|
281
|
+
- lib/yaks/stateful_builder.rb
|
262
282
|
- lib/yaks/util.rb
|
263
283
|
- lib/yaks/version.rb
|
264
284
|
- spec/acceptance/acceptance_spec.rb
|
@@ -268,6 +288,7 @@ files:
|
|
268
288
|
- spec/integration/map_to_resource_spec.rb
|
269
289
|
- spec/json/confucius.collection.json
|
270
290
|
- spec/json/confucius.hal.json
|
291
|
+
- spec/json/confucius.halo.json
|
271
292
|
- spec/json/confucius.json_api.json
|
272
293
|
- spec/json/john.hal.json
|
273
294
|
- spec/json/plant_collection.collection.json
|
@@ -283,19 +304,20 @@ files:
|
|
283
304
|
- spec/support/pet_peeve_mapper.rb
|
284
305
|
- spec/support/shared_contexts.rb
|
285
306
|
- spec/support/youtypeit_models_mappers.rb
|
307
|
+
- spec/unit/yaks/attributes_spec.rb
|
286
308
|
- spec/unit/yaks/collection_mapper_spec.rb
|
287
309
|
- spec/unit/yaks/collection_resource_spec.rb
|
288
310
|
- spec/unit/yaks/config/dsl_spec.rb
|
289
311
|
- spec/unit/yaks/config_spec.rb
|
312
|
+
- spec/unit/yaks/configurable_spec.rb
|
290
313
|
- spec/unit/yaks/default_policy/derive_mapper_from_object_spec.rb
|
291
314
|
- spec/unit/yaks/default_policy_spec.rb
|
292
315
|
- spec/unit/yaks/format/collection_json_spec.rb
|
293
316
|
- spec/unit/yaks/format/hal_spec.rb
|
317
|
+
- spec/unit/yaks/format/html_spec.rb
|
294
318
|
- spec/unit/yaks/format/json_api_spec.rb
|
295
319
|
- spec/unit/yaks/format_spec.rb
|
296
320
|
- spec/unit/yaks/fp/callable_spec.rb
|
297
|
-
- spec/unit/yaks/fp/hash_updatable_spec.rb
|
298
|
-
- spec/unit/yaks/fp/updatable_spec.rb
|
299
321
|
- spec/unit/yaks/fp_spec.rb
|
300
322
|
- spec/unit/yaks/mapper/association_mapper_spec.rb
|
301
323
|
- spec/unit/yaks/mapper/association_spec.rb
|
@@ -311,6 +333,8 @@ files:
|
|
311
333
|
- spec/unit/yaks/resource/link_spec.rb
|
312
334
|
- spec/unit/yaks/resource_spec.rb
|
313
335
|
- spec/unit/yaks/runner_spec.rb
|
336
|
+
- spec/unit/yaks/serializer_spec.rb
|
337
|
+
- spec/unit/yaks/stateful_builder_spec.rb
|
314
338
|
- spec/unit/yaks/util_spec.rb
|
315
339
|
- spec/yaml/confucius.yaml
|
316
340
|
- spec/yaml/youtypeitwepostit.yaml
|
@@ -330,12 +354,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
330
354
|
version: '0'
|
331
355
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
332
356
|
requirements:
|
333
|
-
- - "
|
357
|
+
- - ">="
|
334
358
|
- !ruby/object:Gem::Version
|
335
|
-
version:
|
359
|
+
version: '0'
|
336
360
|
requirements: []
|
337
361
|
rubyforge_project:
|
338
|
-
rubygems_version: 2.
|
362
|
+
rubygems_version: 2.2.2
|
339
363
|
signing_key:
|
340
364
|
specification_version: 4
|
341
365
|
summary: Serialize to hypermedia. HAL, JSON-API, etc.
|
@@ -347,6 +371,7 @@ test_files:
|
|
347
371
|
- spec/integration/map_to_resource_spec.rb
|
348
372
|
- spec/json/confucius.collection.json
|
349
373
|
- spec/json/confucius.hal.json
|
374
|
+
- spec/json/confucius.halo.json
|
350
375
|
- spec/json/confucius.json_api.json
|
351
376
|
- spec/json/john.hal.json
|
352
377
|
- spec/json/plant_collection.collection.json
|
@@ -362,19 +387,20 @@ test_files:
|
|
362
387
|
- spec/support/pet_peeve_mapper.rb
|
363
388
|
- spec/support/shared_contexts.rb
|
364
389
|
- spec/support/youtypeit_models_mappers.rb
|
390
|
+
- spec/unit/yaks/attributes_spec.rb
|
365
391
|
- spec/unit/yaks/collection_mapper_spec.rb
|
366
392
|
- spec/unit/yaks/collection_resource_spec.rb
|
367
393
|
- spec/unit/yaks/config/dsl_spec.rb
|
368
394
|
- spec/unit/yaks/config_spec.rb
|
395
|
+
- spec/unit/yaks/configurable_spec.rb
|
369
396
|
- spec/unit/yaks/default_policy/derive_mapper_from_object_spec.rb
|
370
397
|
- spec/unit/yaks/default_policy_spec.rb
|
371
398
|
- spec/unit/yaks/format/collection_json_spec.rb
|
372
399
|
- spec/unit/yaks/format/hal_spec.rb
|
400
|
+
- spec/unit/yaks/format/html_spec.rb
|
373
401
|
- spec/unit/yaks/format/json_api_spec.rb
|
374
402
|
- spec/unit/yaks/format_spec.rb
|
375
403
|
- spec/unit/yaks/fp/callable_spec.rb
|
376
|
-
- spec/unit/yaks/fp/hash_updatable_spec.rb
|
377
|
-
- spec/unit/yaks/fp/updatable_spec.rb
|
378
404
|
- spec/unit/yaks/fp_spec.rb
|
379
405
|
- spec/unit/yaks/mapper/association_mapper_spec.rb
|
380
406
|
- spec/unit/yaks/mapper/association_spec.rb
|
@@ -390,6 +416,8 @@ test_files:
|
|
390
416
|
- spec/unit/yaks/resource/link_spec.rb
|
391
417
|
- spec/unit/yaks/resource_spec.rb
|
392
418
|
- spec/unit/yaks/runner_spec.rb
|
419
|
+
- spec/unit/yaks/serializer_spec.rb
|
420
|
+
- spec/unit/yaks/stateful_builder_spec.rb
|
393
421
|
- spec/unit/yaks/util_spec.rb
|
394
422
|
- spec/yaml/confucius.yaml
|
395
423
|
- spec/yaml/youtypeitwepostit.yaml
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module Yaks
|
2
|
-
module FP
|
3
|
-
|
4
|
-
class HashUpdatable < Module
|
5
|
-
# @param [Array] attributes
|
6
|
-
# @return [Symbol]
|
7
|
-
def initialize(*attributes)
|
8
|
-
define_method :update do |updates|
|
9
|
-
self.class.new(
|
10
|
-
attributes.each_with_object({}) {|attr, hsh|
|
11
|
-
hsh[attr] = updates.fetch(attr) { send(attr) }
|
12
|
-
}
|
13
|
-
)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
data/lib/yaks/fp/updatable.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
module Yaks
|
2
|
-
module FP
|
3
|
-
|
4
|
-
class Updatable < Module
|
5
|
-
# @param [Array] attributes
|
6
|
-
# @return [Symbol]
|
7
|
-
def initialize(*attributes)
|
8
|
-
define_method :update do |updates|
|
9
|
-
self.class.new(
|
10
|
-
*attributes.map {|attr| updates.fetch(attr) { send(attr) }}
|
11
|
-
)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|