yaks-html 0.6.0.alpha → 0.6.0.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -525
  3. data/Rakefile +1 -41
  4. data/{yaks-html/lib → lib}/yaks-html.rb +0 -0
  5. data/{yaks-html/lib → lib}/yaks/format/html.rb +0 -0
  6. data/{yaks-html/lib → lib}/yaks/format/template.html +0 -0
  7. data/{yaks-html/spec → spec}/spec_helper.rb +0 -0
  8. data/{yaks-html/yaks-html.gemspec → yaks-html.gemspec} +1 -1
  9. metadata +11 -108
  10. data/.gitignore +0 -7
  11. data/.travis.yml +0 -36
  12. data/ADDING_FORMATS.md +0 -13
  13. data/CHANGELOG.md +0 -149
  14. data/Gemfile +0 -4
  15. data/IDENTIFIERS.md +0 -113
  16. data/LICENSE +0 -7
  17. data/bench/bench.rb +0 -16
  18. data/bench/bench_1000.rb +0 -60
  19. data/notes.org +0 -72
  20. data/shaved_yak.gif +0 -0
  21. data/yaks-html/README.md +0 -3
  22. data/yaks/README.md +0 -526
  23. data/yaks/lib/yaks.rb +0 -62
  24. data/yaks/lib/yaks/breaking_changes.rb +0 -49
  25. data/yaks/lib/yaks/collection_mapper.rb +0 -52
  26. data/yaks/lib/yaks/collection_resource.rb +0 -73
  27. data/yaks/lib/yaks/config.rb +0 -65
  28. data/yaks/lib/yaks/config/dsl.rb +0 -165
  29. data/yaks/lib/yaks/default_policy.rb +0 -100
  30. data/yaks/lib/yaks/format.rb +0 -87
  31. data/yaks/lib/yaks/format/collection_json.rb +0 -42
  32. data/yaks/lib/yaks/format/hal.rb +0 -82
  33. data/yaks/lib/yaks/format/json_api.rb +0 -86
  34. data/yaks/lib/yaks/fp.rb +0 -26
  35. data/yaks/lib/yaks/fp/callable.rb +0 -9
  36. data/yaks/lib/yaks/fp/hash_updatable.rb +0 -19
  37. data/yaks/lib/yaks/fp/updatable.rb +0 -17
  38. data/yaks/lib/yaks/mapper.rb +0 -75
  39. data/yaks/lib/yaks/mapper/association.rb +0 -43
  40. data/yaks/lib/yaks/mapper/association_mapper.rb +0 -42
  41. data/yaks/lib/yaks/mapper/attribute.rb +0 -17
  42. data/yaks/lib/yaks/mapper/class_methods.rb +0 -37
  43. data/yaks/lib/yaks/mapper/config.rb +0 -44
  44. data/yaks/lib/yaks/mapper/has_many.rb +0 -29
  45. data/yaks/lib/yaks/mapper/has_one.rb +0 -15
  46. data/yaks/lib/yaks/mapper/link.rb +0 -91
  47. data/yaks/lib/yaks/null_resource.rb +0 -40
  48. data/yaks/lib/yaks/primitivize.rb +0 -45
  49. data/yaks/lib/yaks/resource.rb +0 -51
  50. data/yaks/lib/yaks/resource/link.rb +0 -21
  51. data/yaks/lib/yaks/runner.rb +0 -101
  52. data/yaks/lib/yaks/util.rb +0 -56
  53. data/yaks/lib/yaks/version.rb +0 -3
  54. data/yaks/spec/acceptance/acceptance_spec.rb +0 -48
  55. data/yaks/spec/acceptance/json_shared_examples.rb +0 -8
  56. data/yaks/spec/acceptance/models.rb +0 -48
  57. data/yaks/spec/fixture_helpers.rb +0 -14
  58. data/yaks/spec/integration/map_to_resource_spec.rb +0 -28
  59. data/yaks/spec/json/confucius.collection.json +0 -28
  60. data/yaks/spec/json/confucius.hal.json +0 -59
  61. data/yaks/spec/json/confucius.json_api.json +0 -51
  62. data/yaks/spec/json/john.hal.json +0 -29
  63. data/yaks/spec/json/plant_collection.collection.json +0 -32
  64. data/yaks/spec/json/plant_collection.hal.json +0 -34
  65. data/yaks/spec/json/youtypeitwepostit.collection.json +0 -45
  66. data/yaks/spec/spec_helper.rb +0 -30
  67. data/yaks/spec/support/classes_for_policy_testing.rb +0 -36
  68. data/yaks/spec/support/deep_eql.rb +0 -123
  69. data/yaks/spec/support/fixtures.rb +0 -6
  70. data/yaks/spec/support/friends_mapper.rb +0 -29
  71. data/yaks/spec/support/models.rb +0 -23
  72. data/yaks/spec/support/pet_mapper.rb +0 -3
  73. data/yaks/spec/support/pet_peeve_mapper.rb +0 -3
  74. data/yaks/spec/support/shared_contexts.rb +0 -57
  75. data/yaks/spec/support/youtypeit_models_mappers.rb +0 -20
  76. data/yaks/spec/unit/yaks/collection_mapper_spec.rb +0 -165
  77. data/yaks/spec/unit/yaks/collection_resource_spec.rb +0 -77
  78. data/yaks/spec/unit/yaks/config/dsl_spec.rb +0 -87
  79. data/yaks/spec/unit/yaks/config_spec.rb +0 -74
  80. data/yaks/spec/unit/yaks/default_policy/derive_mapper_from_object_spec.rb +0 -80
  81. data/yaks/spec/unit/yaks/default_policy_spec.rb +0 -70
  82. data/yaks/spec/unit/yaks/format/collection_json_spec.rb +0 -41
  83. data/yaks/spec/unit/yaks/format/hal_spec.rb +0 -44
  84. data/yaks/spec/unit/yaks/format/json_api_spec.rb +0 -42
  85. data/yaks/spec/unit/yaks/format_spec.rb +0 -37
  86. data/yaks/spec/unit/yaks/fp/callable_spec.rb +0 -13
  87. data/yaks/spec/unit/yaks/fp/hash_updatable_spec.rb +0 -22
  88. data/yaks/spec/unit/yaks/fp/updatable_spec.rb +0 -22
  89. data/yaks/spec/unit/yaks/fp_spec.rb +0 -31
  90. data/yaks/spec/unit/yaks/mapper/association_mapper_spec.rb +0 -60
  91. data/yaks/spec/unit/yaks/mapper/association_spec.rb +0 -135
  92. data/yaks/spec/unit/yaks/mapper/attribute_spec.rb +0 -20
  93. data/yaks/spec/unit/yaks/mapper/class_methods_spec.rb +0 -67
  94. data/yaks/spec/unit/yaks/mapper/config_spec.rb +0 -166
  95. data/yaks/spec/unit/yaks/mapper/has_many_spec.rb +0 -89
  96. data/yaks/spec/unit/yaks/mapper/has_one_spec.rb +0 -51
  97. data/yaks/spec/unit/yaks/mapper/link_spec.rb +0 -196
  98. data/yaks/spec/unit/yaks/mapper_spec.rb +0 -303
  99. data/yaks/spec/unit/yaks/null_resource_spec.rb +0 -32
  100. data/yaks/spec/unit/yaks/primitivize_spec.rb +0 -77
  101. data/yaks/spec/unit/yaks/resource/link_spec.rb +0 -20
  102. data/yaks/spec/unit/yaks/resource_spec.rb +0 -108
  103. data/yaks/spec/unit/yaks/runner_spec.rb +0 -260
  104. data/yaks/spec/unit/yaks/util_spec.rb +0 -49
  105. data/yaks/spec/yaml/confucius.yaml +0 -28
  106. data/yaks/spec/yaml/youtypeitwepostit.yaml +0 -9
  107. data/yaks/yaks.gemspec +0 -41
@@ -1,89 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Yaks::Mapper::HasMany do
4
- include_context 'yaks context'
5
-
6
- let(:closet_mapper) { closet_mapper_class.new(yaks_context) }
7
-
8
- let(:closet_mapper_class) do
9
- Class.new(Yaks::Mapper) do
10
- type 'closet'
11
- has_many :shoes,
12
- rel: 'http://foo/shoes',
13
- mapper: Class.new(Yaks::Mapper) { type 'shoe' ; attributes :size, :color }
14
- end
15
- end
16
-
17
- subject(:shoe_association) { closet_mapper.associations.first }
18
-
19
- its(:singular_name) { should eq 'shoe' }
20
-
21
- let(:closet) {
22
- fake(
23
- :shoes => [
24
- fake(size: 9, color: :blue),
25
- fake(size: 11.5, color: :red),
26
- ]
27
- )
28
- }
29
-
30
- it 'should map the subresources' do
31
- expect(closet_mapper.call(closet).subresources).to eql(
32
- "http://foo/shoes" => Yaks::CollectionResource.new(
33
- type: 'shoe',
34
- members: [
35
- Yaks::Resource.new(type: 'shoe', attributes: {:size => 9, :color => :blue}),
36
- Yaks::Resource.new(type: 'shoe', attributes: {:size => 11.5, :color => :red})
37
- ],
38
- collection_rel: 'rel:shoes'
39
- )
40
- )
41
- end
42
-
43
- it 'should map nil to a NullResource collection' do
44
- expect(closet_mapper.call(fake(shoes: nil)).subresources).to eql(
45
- 'http://foo/shoes' => Yaks::NullResource.new(collection: true)
46
- )
47
- end
48
-
49
- context 'without an explicit mapper' do
50
- let(:dress_mapper) {
51
- Class.new(Yaks::Mapper) { type 'dress' ; attributes :color }
52
- }
53
-
54
- before do
55
- closet_mapper_class.class_eval do
56
- has_many :dresses
57
- end
58
-
59
- stub(closet_mapper.policy).derive_mapper_from_association(any_args) do
60
- dress_mapper
61
- end
62
- end
63
-
64
- it 'should derive it from policy' do
65
- expect(closet_mapper.policy).to equal policy
66
- closet_mapper.call(fake(shoes: [], dresses: [fake(color: 'blue')]))
67
- end
68
- end
69
-
70
-
71
- describe '#collection_mapper' do
72
- let(:collection_mapper) { Yaks::Undefined }
73
- subject(:has_many) { described_class.new(name: :name, mapper: :mapper, rel: :rel, collection_mapper: collection_mapper) }
74
-
75
- context 'when the collection mapper is undefined' do
76
- it 'should derive one from collection and policy' do
77
- expect(has_many.collection_mapper([], Yaks::DefaultPolicy.new)).to equal Yaks::CollectionMapper
78
- end
79
- end
80
-
81
- context 'when the collection mapper is specified' do
82
- let(:collection_mapper) { :foo }
83
-
84
- it 'should use the given collection mapper' do
85
- expect(has_many.collection_mapper([], Yaks::DefaultPolicy.new)).to equal :foo
86
- end
87
- end
88
- end
89
- end
@@ -1,51 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Yaks::Mapper::HasOne do
4
- include_context 'yaks context'
5
-
6
- AuthorMapper = Class.new(Yaks::Mapper) { attributes :name }
7
-
8
- subject(:has_one) do
9
- described_class.new(
10
- name: :author,
11
- mapper: association_mapper,
12
- rel: 'http://rel'
13
- )
14
- end
15
-
16
- let(:association_mapper) { AuthorMapper }
17
- let(:name) { 'William S. Burroughs' }
18
- let(:author) { fake(:name => name) }
19
-
20
- fake(:policy,
21
- derive_type_from_mapper_class: 'author',
22
- derive_mapper_from_association: AuthorMapper
23
- ){ Yaks::DefaultPolicy }
24
-
25
- its(:singular_name) { should eq 'author' }
26
-
27
- it 'should map to a single Resource' do
28
- expect(has_one.map_resource(author, yaks_context)).to eq Yaks::Resource.new(type: 'author', attributes: {name: name})
29
- end
30
-
31
- context 'with no mapper specified' do
32
- subject(:subresource) { has_one.add_to_resource(Yaks::Resource.new, parent_mapper, yaks_context) }
33
- let(:association_mapper) { Yaks::Undefined }
34
- fake(:parent_mapper) { Yaks::Mapper }
35
-
36
- before do
37
- stub(parent_mapper).load_association(:author) { author }
38
- end
39
-
40
- it 'should derive one based on policy' do
41
- expect(subresource).to eql(
42
- Yaks::Resource.new(
43
- subresources: {
44
- 'http://rel' => Yaks::Resource.new(type: 'author', attributes: {name: name})
45
- }
46
- )
47
- )
48
- end
49
-
50
- end
51
- end
@@ -1,196 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Yaks::Mapper::Link do
4
- include_context 'yaks context'
5
-
6
- subject(:link) { described_class.new(rel, template, options) }
7
-
8
- let(:rel) { :next }
9
- let(:template) { '/foo/bar/{x}/{y}' }
10
- let(:options) { {} }
11
-
12
- its(:template_variables) { should eq [:x, :y] }
13
- its(:uri_template) { should eq URITemplate.new(template) }
14
-
15
- let(:object) { Struct.new(:x, :y, :returns_nil).new(3, 4, nil) }
16
-
17
- let(:mapper_class) do
18
- Class.new(Yaks::Mapper) do
19
- type 'foo'
20
- end
21
- end
22
-
23
- let(:mapper) do
24
- mapper_class.new(yaks_context).tap do |mapper|
25
- mapper.call(object) # set @object
26
- end
27
- end
28
-
29
- describe '#add_to_resource' do
30
- it 'should add itself to the resource' do
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", {})])
33
- )
34
- end
35
-
36
- context 'with a link function returning nothing' do
37
- let(:template) { :link_computer }
38
- before do
39
- mapper_class.class_eval do
40
- def link_computer ; end
41
- end
42
- end
43
-
44
- it 'should not render the link' do
45
- expect(link.add_to_resource(Yaks::Resource.new, mapper, yaks_context)).to eql(
46
- Yaks::Resource.new
47
- )
48
- end
49
- end
50
- end
51
-
52
- describe '#rel?' do
53
- it 'should return true if the relation matches' do
54
- expect(link.rel?(:next)).to be true
55
- end
56
-
57
- it 'should return false if the relation does not match' do
58
- expect(link.rel?(:previous)).to be false
59
- end
60
-
61
- context 'with URI rels' do
62
- let(:rel) { 'http://foo/bar/rel' }
63
-
64
- it 'should return true if the relation matches' do
65
- expect(link.rel?('http://foo/bar/rel')).to be true
66
- end
67
-
68
- it 'should return false if the relation does not match' do
69
- expect(link.rel?('http://foo/bar/other')).to be false
70
- end
71
- end
72
- end
73
-
74
- describe '#expand_with' do
75
- it 'should look up expansion values through the provided callable' do
76
- expect(link.expand_with(->(var){ var.upcase })).to eq '/foo/bar/X/Y'
77
- end
78
-
79
- context 'with expansion turned off' do
80
- let(:options) { {expand: false} }
81
-
82
- it 'should keep the template in the response' do
83
- expect(link.expand_with(->{ })).to eq '/foo/bar/{x}/{y}'
84
- end
85
- end
86
-
87
- context 'with a URI without expansion variables' do
88
- let(:template) { '/orders' }
89
-
90
- it 'should return the link as is' do
91
- expect(link.expand_with(->{ })).to eq '/orders'
92
- end
93
- end
94
-
95
- context 'with partial expansion' do
96
- let(:options) { { expand: [:y] } }
97
-
98
- it 'should only expand the given variables' do
99
- expect(link.expand_with({:y => 7}.method(:[]))).to eql '/foo/bar/{x}/7'
100
- end
101
- end
102
-
103
- context 'with a symbol for a template' do
104
- let(:template) { :a_symbol }
105
-
106
- it 'should use the lookup mechanism for finding the link' do
107
- expect(link.expand_with({:a_symbol => '/foo/foo'}.method(:[]))).to eq '/foo/foo'
108
- end
109
- end
110
- end
111
-
112
- describe '#map_to_resource_link' do
113
- subject(:resource_link) { link.map_to_resource_link(mapper) }
114
-
115
- its(:rel) { should eq :next }
116
-
117
- context 'with attributes' do
118
- it 'should not have a title' do
119
- expect(resource_link.options.key?(:title)).to be false
120
- end
121
-
122
- it 'should not be templated' do
123
- expect(resource_link.options[:templated]).to be_falsey
124
- end
125
-
126
- context 'with extra options' do
127
- let(:options) { {title: 'foo', expand: [:x], foo: :bar} }
128
-
129
- it 'should pass on unknown options' do
130
- expect(resource_link.options[:foo]).to eql :bar
131
- end
132
- end
133
-
134
- it 'should create an instance of Yaks::Resource::Link' do
135
- expect(resource_link).to be_a(Yaks::Resource::Link)
136
- end
137
-
138
- it 'should expand the URI template' do
139
- expect(resource_link.uri).to eq '/foo/bar/3/4'
140
- end
141
- end
142
-
143
- context 'with expansion turned off' do
144
- let(:options) { {expand: false} }
145
-
146
- it 'should be templated' do
147
- expect(resource_link.options[:templated]).to be true
148
- end
149
-
150
- it 'should not propagate :expand' do
151
- expect(resource_link.options.key?(:expand)).to be false
152
- end
153
- end
154
-
155
- context 'with partial expansion' do
156
- let(:options) { {expand: [:x]} }
157
-
158
- it 'should be templated' do
159
- expect(resource_link.options[:templated]).to be true
160
- end
161
- end
162
-
163
- context 'with a title set' do
164
- let(:options) { { title: 'link title' } }
165
-
166
- it 'should set the title on the resource link' do
167
- expect(resource_link.title).to eq 'link title'
168
- end
169
- end
170
-
171
- context 'with a title lambda' do
172
- let(:options) { { title: -> { "say #{mapper_method}" } } }
173
-
174
- before do
175
- mapper_class.class_eval do
176
- def mapper_method
177
- 'hello'
178
- end
179
- end
180
- end
181
-
182
- it 'should evaluate the lambda in the context of the mapper' do
183
- expect(resource_link.title).to eq 'say hello'
184
- end
185
- end
186
-
187
- context 'with a link generation method that returns nil' do
188
- let(:template) { :returns_nil }
189
-
190
- it 'should return nil' do
191
- expect(resource_link).to be_nil
192
- end
193
- end
194
- end
195
-
196
- end
@@ -1,303 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Yaks::Mapper do
4
- include_context 'yaks context'
5
-
6
- subject(:mapper) { mapper_class.new(yaks_context) }
7
- let(:resource) { mapper.call(instance) }
8
-
9
- let(:mapper_class) { Class.new(Yaks::Mapper) { type 'foo' } }
10
-
11
- let(:instance) { fake(foo: 'hello', bar: 'world') }
12
-
13
- its(:env) { should equal rack_env }
14
-
15
- describe '#call' do
16
- context 'with attributes' do
17
- before do
18
- mapper_class.attributes :foo, :bar
19
- end
20
-
21
- it 'should make the configured attributes available on the instance' do
22
- expect(mapper.attributes).to eq [
23
- Yaks::Mapper::Attribute.new(:foo),
24
- Yaks::Mapper::Attribute.new(:bar)
25
- ]
26
- end
27
-
28
- it 'should load them from the model' do
29
- expect(resource.attributes).to eq(foo: 'hello', bar: 'world')
30
- end
31
-
32
- context 'with attribute filtering' do
33
- before do
34
- mapper_class.class_eval do
35
- def attributes
36
- super.reject {|attr| attr.name == :foo}
37
- end
38
- end
39
- end
40
-
41
- it 'should only map the non-filtered attributes' do
42
- expect(resource.attributes).to eq(:bar => 'world')
43
- end
44
- end
45
- end
46
-
47
- context 'with links' do
48
- before do
49
- mapper_class.link :profile, 'http://foo/bar'
50
- end
51
-
52
- it 'should map the link' do
53
- expect(resource.links).to eq [
54
- Yaks::Resource::Link.new(:profile, 'http://foo/bar', {})
55
- ]
56
- end
57
-
58
- it 'should use the link in the resource' do
59
- expect(resource.links).to include Yaks::Resource::Link.new(:profile, 'http://foo/bar', {})
60
- end
61
-
62
- context 'with the same link rel defined multiple times' do
63
- before do
64
- mapper_class.class_eval do
65
- link(:self, 'http://foo/bam')
66
- link(:self, 'http://foo/baz')
67
- link(:self, 'http://foo/baq')
68
- end
69
- end
70
-
71
- it 'should map all the links' do
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', {})
77
- ]
78
- end
79
- end
80
- end
81
-
82
- context 'with subresources' do
83
- let(:widget) { fake(type: 'super_widget') }
84
- let(:instance) { fake(widget: widget) }
85
- let(:widget_mapper) { Class.new(Yaks::Mapper) { type 'widget' } }
86
- fake(:policy) { Yaks::DefaultPolicy }
87
-
88
- describe 'has_one' do
89
- let(:has_one_opts) do
90
- { mapper: widget_mapper,
91
- rel: 'http://foo.bar/rels/widgets' }
92
- end
93
-
94
- before do
95
- widget_mapper.attributes :type
96
- mapper_class.has_one(:widget, has_one_opts)
97
- end
98
-
99
-
100
- it 'should have the subresource in the resource' do
101
- expect(resource.subresources).to eq("http://foo.bar/rels/widgets" => Yaks::Resource.new(type: 'widget', attributes: {:type => "super_widget"}))
102
- end
103
-
104
- context 'with explicit mapper and rel' do
105
- it 'should delegate to the given mapper' do
106
- expect(resource.subresources).to eq(
107
- "http://foo.bar/rels/widgets" => Yaks::Resource.new(type: 'widget', attributes: {:type => "super_widget"})
108
- )
109
- end
110
- end
111
-
112
- context 'with unspecified mapper' do
113
- let(:has_one_opts) do
114
- { rel: 'http://foo.bar/rels/widgets' }
115
- end
116
-
117
- before do
118
- stub(policy).derive_mapper_from_association(mapper.associations.first) do
119
- widget_mapper
120
- end
121
- end
122
-
123
- it 'should derive the mapper based on policy' do
124
- expect(resource.subresources).to eq(
125
- "http://foo.bar/rels/widgets" => Yaks::Resource.new(type: 'widget', attributes: {:type => "super_widget"})
126
- )
127
- end
128
- end
129
-
130
- context 'with unspecified rel' do
131
- let(:has_one_opts) do
132
- { mapper: widget_mapper }
133
- end
134
-
135
- before do
136
- stub(policy).derive_rel_from_association(mapper.associations.first) do
137
- 'http://rel/rel'
138
- end
139
- end
140
-
141
- it 'should derive the rel based on policy' do
142
- expect(resource.subresources).to eq(
143
- "http://rel/rel" => Yaks::Resource.new(type: 'widget', attributes: {:type => "super_widget"})
144
- )
145
- end
146
- end
147
-
148
- context 'with the association filtered out' do
149
- before do
150
- mapper_class.class_eval do
151
- def associations
152
- []
153
- end
154
- end
155
- end
156
-
157
- it 'should not map the resource' do
158
- expect(resource.subresources).to eq({})
159
- end
160
- end
161
- end
162
- end
163
-
164
- context 'when the mapper implements a method with the attribute name' do
165
- before do
166
- mapper_class.class_eval do
167
- attributes :fooattr, :bar
168
-
169
- def fooattr
170
- "#{object.foo} my friend"
171
- end
172
- end
173
- end
174
-
175
- it 'should get the attribute from the mapper' do
176
- expect(resource.attributes).to eq(fooattr: 'hello my friend', bar: 'world')
177
- end
178
- end
179
-
180
- context 'with a nil subject' do
181
- it 'should return a NullResource when the subject is nil' do
182
- expect(mapper.call(nil)).to be_a Yaks::NullResource
183
- end
184
- end
185
-
186
- context 'with a link generated by a method that returns nil' do
187
- before do
188
- mapper_class.class_eval do
189
- attributes :id
190
- link :bar_link, :link_generating_method
191
-
192
- def link_generating_method
193
- end
194
- end
195
- end
196
-
197
- it 'should not render the link' do
198
- expect(mapper.call(fake(id: 123))).to eql Yaks::Resource.new(
199
- type: 'foo',
200
- attributes: {id: 123}
201
- )
202
- end
203
- end
204
- end
205
-
206
- describe '.mapper_name' do
207
- context 'with a type configured' do
208
- it 'should use the type' do
209
- expect(mapper_class.mapper_name(policy)).to eql 'foo'
210
- end
211
- end
212
-
213
- context 'without a type configured' do
214
- let(:mapper_class) { PetMapper }
215
-
216
- it 'should infer the type from the name' do
217
- expect(mapper_class.mapper_name(policy)).to eql 'pet'
218
- end
219
- end
220
- end
221
-
222
- describe '#mapper_name' do
223
- context 'with a type configured' do
224
- it 'should use the type' do
225
- expect(mapper.mapper_name).to eql 'foo'
226
- end
227
- end
228
-
229
- context 'without a type configured' do
230
- let(:mapper_class) { PetMapper }
231
-
232
- it 'should infer the type from the name' do
233
- expect(mapper.mapper_name).to eql 'pet'
234
- end
235
- end
236
- end
237
-
238
- describe '#load_attribute' do
239
- context 'with the attribute defined as a method on the mapper' do
240
- it 'should call the local implementation' do
241
- def mapper.foo
242
- 14
243
- end
244
-
245
- expect(mapper.load_attribute(:foo)).to eql 14
246
- end
247
- end
248
-
249
- context 'without a mapper method with the attribute name' do
250
- it 'should call the method on the object being mapped' do
251
- mapper.call(instance) # set @object
252
- expect(mapper.load_attribute(:foo)).to eql 'hello'
253
- end
254
- end
255
- end
256
-
257
- describe '#map_attributes' do
258
- let(:attribute) { fake('Attribute') }
259
-
260
- it 'should receive a context' do
261
- stub(attribute).add_to_resource(any_args) {|r,_,_| Yaks::Resource.new}
262
-
263
- mapper.config.attributes[0..-1] = [attribute]
264
- mapper.call(instance)
265
-
266
- expect(attribute).to have_received.add_to_resource(any(Yaks::Resource), mapper, yaks_context)
267
- end
268
- end
269
-
270
- describe '#map_links' do
271
- let(:link) { fake('Link') }
272
-
273
- it 'should receive a context' do
274
- stub(link).add_to_resource(any_args) {|r,_,_| Yaks::Resource.new}
275
-
276
- mapper.config.links[0..-1] = [link]
277
- mapper.call(instance)
278
-
279
- expect(link).to have_received.add_to_resource(any(Yaks::Resource), mapper, yaks_context)
280
- end
281
- end
282
-
283
- describe '#map_subresources' do
284
- let(:association) { fake('Association') }
285
-
286
- it 'should receive a context' do
287
- stub(association).add_to_resource(any_args) {|r,_,_| Yaks::Resource.new}
288
-
289
- mapper.config.associations[0..-1] = [association]
290
- mapper.call(instance)
291
-
292
- expect(association).to have_received.add_to_resource(any(Yaks::Resource), mapper, yaks_context)
293
- end
294
- end
295
-
296
- describe '#mapper_stack' do
297
- let(:yaks_context) { super().merge(mapper_stack: [:foo]) }
298
-
299
- it 'should delegate to context' do
300
- expect(mapper.mapper_stack).to eql [:foo]
301
- end
302
- end
303
- end