yaks 0.8.3 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,7 @@ RSpec.describe Yaks::Format::JsonAPI do
7
7
 
8
8
  it 'should not include a "linked" key' do
9
9
  expect(format.call(resource)).to eql(
10
- {'wizards' => [{foo: :bar}]}
10
+ {data: [{type: :wizards, foo: :bar}]}
11
11
  )
12
12
  end
13
13
  end
@@ -28,8 +28,9 @@ RSpec.describe Yaks::Format::JsonAPI do
28
28
  # TODO should it really behave this way? better to give preference to self link.
29
29
  it 'should give preference to the href attribute' do
30
30
  expect(format.call(resource)).to eql(
31
- {'wizards' => [
31
+ {data: [
32
32
  {
33
+ type: :wizards,
33
34
  href: '/the/href'
34
35
  }
35
36
  ]
@@ -38,4 +39,92 @@ RSpec.describe Yaks::Format::JsonAPI do
38
39
  end
39
40
  end
40
41
 
42
+ context 'with a self link' do
43
+ let(:resource) {
44
+ Yaks::Resource.new(
45
+ type: 'wizard',
46
+ links: [
47
+ Yaks::Resource::Link.new(rel: :self, uri: '/the/self/link')
48
+ ]
49
+ )
50
+ }
51
+ it 'should use the self link in output' do
52
+ expect(format.call(resource)).to eql(
53
+ {data: [
54
+ {
55
+ type: :wizards,
56
+ href: '/the/self/link'
57
+ }
58
+ ]
59
+ }
60
+ )
61
+
62
+ end
63
+ end
64
+
65
+ context 'with subresources' do
66
+ let(:resource) {
67
+ Yaks::Resource.new(
68
+ type: 'wizard',
69
+ subresources: [
70
+ Yaks::Resource.new(type: 'spell', attributes: {id: 777, name: 'Lucky Sevens'})
71
+ ]
72
+ )
73
+ }
74
+ it 'should include links and linked' do
75
+ expect(format.call(resource)).to eql(
76
+ {
77
+ data: [
78
+ {
79
+ type: :wizards,
80
+ links: {'spell' => {type: 'spells', id: 777}}
81
+ }
82
+ ],
83
+ linked: [{type: :spells, id: 777, name: 'Lucky Sevens'}]
84
+ }
85
+ )
86
+ end
87
+
88
+ end
89
+
90
+ context 'with null subresources' do
91
+ let(:resource) {
92
+ Yaks::Resource.new(
93
+ type: 'wizard',
94
+ subresources: [
95
+ Yaks::NullResource.new
96
+ ]
97
+ )
98
+ }
99
+ it 'should not include links' do
100
+ expect(format.call(resource)).to eql(
101
+ {data: [
102
+ {
103
+ type: :wizards,
104
+ }
105
+ ]
106
+ }
107
+ )
108
+ end
109
+ end
110
+
111
+ context 'with no subresources or links' do
112
+ let(:resource) {
113
+ Yaks::Resource.new(
114
+ type: 'wizard',
115
+ subresources: []
116
+ )
117
+ }
118
+ it 'should not include links' do
119
+ expect(format.call(resource)).to eql(
120
+ {data: [
121
+ {
122
+ type: :wizards
123
+ }
124
+ ]
125
+ }
126
+ )
127
+ end
128
+ end
129
+
41
130
  end
@@ -1,9 +1,10 @@
1
1
  RSpec.describe Yaks::Mapper::Form::Field do
2
2
  include_context 'yaks context'
3
3
 
4
- let(:field) { described_class.new( full_args ) }
4
+ let(:field) { described_class.new( full_args ) }
5
5
  let(:name) { :the_field }
6
- let(:full_args) { {name: name}.merge(args) }
6
+ let(:full_args) { {name: name, options: options}.merge(args) }
7
+ let(:options) { [] }
7
8
  let(:args) {
8
9
  {
9
10
  label: 'a label',
@@ -12,7 +13,11 @@ RSpec.describe Yaks::Mapper::Form::Field do
12
13
  }
13
14
  }
14
15
 
15
- let(:mapper) { Yaks::Mapper.new(yaks_context) }
16
+ let(:mapper) do
17
+ Class.new(Yaks::Mapper) do
18
+ def month ; 'January' ; end
19
+ end.new(yaks_context)
20
+ end
16
21
 
17
22
  describe '.create' do
18
23
  it 'can take all args as a hash' do
@@ -28,9 +33,62 @@ RSpec.describe Yaks::Mapper::Form::Field do
28
33
  end
29
34
  end
30
35
 
31
- describe '#to_resource_field' do
36
+ describe '#to_resource_fields' do
32
37
  it 'creates a Yaks::Resource::Form::Field with the same attributes' do
33
- expect(field.to_resource(mapper)).to eql Yaks::Resource::Form::Field.new(full_args)
38
+ expect(field.to_resource_fields(mapper)).to eql [Yaks::Resource::Form::Field.new(full_args)]
39
+ end
40
+
41
+ context 'with dynamic attributes' do
42
+ let(:name) { ->{ month } }
43
+
44
+ it 'should expand attributes using the mapper' do
45
+ expect(field.to_resource_fields(mapper).first.name).to eql 'January'
46
+ end
47
+ end
48
+
49
+ context 'with a falsey if condition' do
50
+ let(:args) { super().merge(if: ->{ false })}
51
+ it 'returns an empty array' do
52
+ expect(field.to_resource_fields(mapper)).to eql []
53
+ end
54
+ end
55
+
56
+ context 'with a truthy if condition' do
57
+ let(:args) { super().merge(if: ->{ true })}
58
+ it 'returns a field' do
59
+ expect(field.to_resource_fields(mapper).first).to be_a Yaks::Resource::Form::Field
60
+ end
61
+ end
62
+
63
+ context 'with select optons' do
64
+ let(:options) {
65
+ [
66
+ Yaks::Mapper::Form::Field::Option.new(
67
+ label: 'Jan',
68
+ value: ->{ 'January' },
69
+ selected: ->{ month == 'January' }
70
+ ),
71
+ Yaks::Mapper::Form::Field::Option.new(
72
+ label: 'Feb',
73
+ value: ->{ 'February' },
74
+ selected: ->{ month == 'February' }
75
+ )
76
+ ]
77
+ }
78
+
79
+ it 'should convert them to Yaks::Form* objects' do
80
+ form_field = Yaks::Resource::Form::Field.new(
81
+ name: :the_field,
82
+ label: "a label",
83
+ options: [
84
+ Yaks::Resource::Form::Field::Option.new(value: "January", label: "Jan", selected: true),
85
+ Yaks::Resource::Form::Field::Option.new(value: "February", label: "Feb")
86
+ ],
87
+ type: "text",
88
+ value: "hello"
89
+ )
90
+ expect(field.to_resource_fields(mapper)).to eql [form_field]
91
+ end
34
92
  end
35
93
  end
36
94
  end
@@ -0,0 +1,30 @@
1
+ RSpec.describe Yaks::Mapper::Form::Fieldset do
2
+ include_context 'yaks context'
3
+ let(:mapper) { Yaks::Mapper.new(yaks_context) }
4
+
5
+ describe '#to_resource_fields' do
6
+ context 'with dynamic elements' do
7
+ let(:fieldset) do
8
+ described_class.create({}) do
9
+ dynamic do |object|
10
+ text object.name
11
+ end
12
+ end
13
+ end
14
+
15
+ it 'should render them based on the mapped object' do
16
+ mapper.call(fake(name: :anthony)) # hack to set the mapper's object
17
+ expect(fieldset.to_resource_fields(mapper)).to eql(
18
+ [
19
+ Yaks::Resource::Form::Fieldset.new(
20
+ fields: [
21
+ Yaks::Resource::Form::Field.new(name: :anthony, type: :text)
22
+ ]
23
+ )
24
+ ]
25
+ )
26
+ end
27
+ end
28
+ end
29
+
30
+ end
@@ -14,6 +14,7 @@ RSpec.describe Yaks::Mapper::Form do
14
14
  }
15
15
  }
16
16
  let(:fields) { [] }
17
+ let(:mapper) { Yaks::Mapper.new(yaks_context) }
17
18
 
18
19
  describe '.create' do
19
20
  it 'should have a name of nil when ommitted' do
@@ -22,7 +23,6 @@ RSpec.describe Yaks::Mapper::Form do
22
23
  end
23
24
 
24
25
  describe '#add_to_resource' do
25
- let(:mapper) { Yaks::Mapper.new(yaks_context) }
26
26
  let(:resource) { form.new.add_to_resource(Yaks::Resource.new, mapper, nil) }
27
27
 
28
28
  context 'with fields' do
@@ -54,5 +54,29 @@ RSpec.describe Yaks::Mapper::Form do
54
54
  end
55
55
  end
56
56
 
57
+ describe '#to_resource_form' do
58
+ context 'with dynamic elements' do
59
+ let(:form) do
60
+ described_class.create(name) do
61
+ dynamic do |object|
62
+ text object.name
63
+ end
64
+ end
65
+ end
66
+
67
+ it 'should render them based on the mapped object' do
68
+ mapper.call(fake(name: :anthony)) # hack to set the mapper's object
69
+ expect(form.to_resource_form(mapper)).to eql(
70
+ Yaks::Resource::Form.new(
71
+ name: :the_name,
72
+ fields: [
73
+ Yaks::Resource::Form::Field.new(name: :anthony, type: :text)
74
+ ]
75
+ )
76
+ )
77
+ end
78
+ end
79
+ end
80
+
57
81
  end
58
82
  end
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.8.3
4
+ version: 0.9.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: 2015-03-09 00:00:00.000000000 Z
11
+ date: 2015-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inflection
@@ -272,6 +272,7 @@ files:
272
272
  - lib/yaks/mapper/config.rb
273
273
  - lib/yaks/mapper/form.rb
274
274
  - lib/yaks/mapper/form/config.rb
275
+ - lib/yaks/mapper/form/dynamic_field.rb
275
276
  - lib/yaks/mapper/form/field.rb
276
277
  - lib/yaks/mapper/form/field/option.rb
277
278
  - lib/yaks/mapper/form/fieldset.rb
@@ -282,6 +283,7 @@ files:
282
283
  - lib/yaks/pipeline.rb
283
284
  - lib/yaks/primitivize.rb
284
285
  - lib/yaks/reader/hal.rb
286
+ - lib/yaks/reader/json_api.rb
285
287
  - lib/yaks/resource.rb
286
288
  - lib/yaks/resource/form.rb
287
289
  - lib/yaks/resource/form/field.rb
@@ -325,6 +327,7 @@ files:
325
327
  - spec/unit/yaks/collection_mapper_spec.rb
326
328
  - spec/unit/yaks/collection_resource_spec.rb
327
329
  - spec/unit/yaks/config_spec.rb
330
+ - spec/unit/yaks/configurable_spec.rb
328
331
  - spec/unit/yaks/default_policy/derive_mapper_from_object_spec.rb
329
332
  - spec/unit/yaks/default_policy_spec.rb
330
333
  - spec/unit/yaks/format/collection_json_spec.rb
@@ -341,6 +344,7 @@ files:
341
344
  - spec/unit/yaks/mapper/config_spec.rb
342
345
  - spec/unit/yaks/mapper/form/field/option_spec.rb
343
346
  - spec/unit/yaks/mapper/form/field_spec.rb
347
+ - spec/unit/yaks/mapper/form/fieldset_spec.rb
344
348
  - spec/unit/yaks/mapper/form_spec.rb
345
349
  - spec/unit/yaks/mapper/has_many_spec.rb
346
350
  - spec/unit/yaks/mapper/has_one_spec.rb
@@ -416,6 +420,7 @@ test_files:
416
420
  - spec/unit/yaks/collection_mapper_spec.rb
417
421
  - spec/unit/yaks/collection_resource_spec.rb
418
422
  - spec/unit/yaks/config_spec.rb
423
+ - spec/unit/yaks/configurable_spec.rb
419
424
  - spec/unit/yaks/default_policy/derive_mapper_from_object_spec.rb
420
425
  - spec/unit/yaks/default_policy_spec.rb
421
426
  - spec/unit/yaks/format/collection_json_spec.rb
@@ -432,6 +437,7 @@ test_files:
432
437
  - spec/unit/yaks/mapper/config_spec.rb
433
438
  - spec/unit/yaks/mapper/form/field/option_spec.rb
434
439
  - spec/unit/yaks/mapper/form/field_spec.rb
440
+ - spec/unit/yaks/mapper/form/fieldset_spec.rb
435
441
  - spec/unit/yaks/mapper/form_spec.rb
436
442
  - spec/unit/yaks/mapper/has_many_spec.rb
437
443
  - spec/unit/yaks/mapper/has_one_spec.rb