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,32 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Yaks::NullResource do
4
- subject(:null_resource) { described_class.new }
5
-
6
- its(:attributes) { should eq Hash[] }
7
- its(:links) { should eq [] }
8
- its(:subresources) { should eq Hash[] }
9
- its(:collection?) { should be false }
10
- its(:null_resource?) { should be true }
11
-
12
- it { should respond_to :[] }
13
-
14
- its(:type) { should be_nil }
15
-
16
- describe '#each' do
17
- its(:each) { should be_a Enumerator }
18
-
19
- it 'should not yield anything' do
20
- null_resource.each { fail }
21
- end
22
- end
23
-
24
- it 'should contain nothing' do
25
- expect( null_resource[:key] ).to be_nil
26
- end
27
-
28
- context 'when a collection' do
29
- subject(:null_resource) { described_class.new( collection: true ) }
30
- its(:collection?) { should be true }
31
- end
32
- end
@@ -1,77 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Yaks::Primitivize do
4
- subject(:primitivizer) { described_class.create }
5
-
6
- describe '.create' do
7
- it 'should map String, true, false, nil, numbers to themselves' do
8
- [
9
- 'hello',
10
- true,
11
- false,
12
- nil,
13
- 100,
14
- 99.99,
15
- -95.33333
16
- ].each do |object|
17
- expect(primitivizer.call(object)).to eql object
18
- end
19
- end
20
-
21
- it 'should stringify symbols' do
22
- expect(primitivizer.call(:foo)).to eql 'foo'
23
- end
24
-
25
- it 'should recursively handle hashes' do
26
- expect(primitivizer.call(
27
- :foo => {:wassup => :friends, 123 => '456'}
28
- )).to eql('foo' => {'wassup' => 'friends', 123 => '456'})
29
- end
30
-
31
- it 'should handle arrays recursively' do
32
- expect(primitivizer.call(
33
- [:foo, [:wassup, :friends], 123, '456']
34
- )).to eql( ['foo', ['wassup', 'friends'], 123, '456'] )
35
- end
36
- end
37
-
38
- describe '#call' do
39
- require 'ostruct'
40
-
41
- let(:funny_object) {
42
- OpenStruct.new('a' => 'b')
43
- }
44
-
45
- it 'should raise an error when passed an unkown type' do
46
- def funny_object.inspect
47
- "I am funny"
48
- end
49
-
50
- expect { primitivizer.call(funny_object) }.to raise_error "don't know how to turn OpenStruct (I am funny) into a primitive"
51
- end
52
-
53
- context 'with custom mapping' do
54
- require 'matrix'
55
-
56
- let(:primitivizer) do
57
- described_class.new.tap do |p|
58
- p.map Vector do |vec|
59
- vec.map do |i|
60
- call(i)
61
- end.to_a
62
- end
63
-
64
- p.map Symbol do |sym|
65
- sym.to_s.length
66
- end
67
- end
68
- end
69
-
70
- it 'should evaluate in the context of primitivize' do
71
- expect( primitivizer.call( Vector[:foo, :baxxx, :bazz] ) ).to eql( [3, 5, 4] )
72
- end
73
- end
74
-
75
-
76
- end
77
- end
@@ -1,20 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Yaks::Resource::Link do
4
- subject(:link) { described_class.new(rel, uri, options) }
5
- let(:rel) { :foo_rel }
6
- let(:uri) { 'http://api.example.org/rel/foo' }
7
- let(:options) { { title: 'mr. spectacular' } }
8
-
9
- its(:rel) { should eql :foo_rel }
10
- its(:uri) { should eql 'http://api.example.org/rel/foo' }
11
- its(:options) { should eql(title: 'mr. spectacular') }
12
-
13
- its(:title) { should eql('mr. spectacular') }
14
- its(:templated?) { should be false }
15
-
16
- context 'with explicit templated option' do
17
- let(:options) { super().merge(templated: true) }
18
- its(:templated?) { should be true }
19
- end
20
- end
@@ -1,108 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Yaks::Resource do
4
- subject(:resource) { described_class.new(init_opts) }
5
- let(:init_opts) { {} }
6
-
7
- context 'with a zero-arg constructor' do
8
- subject(:resource) { described_class.new }
9
-
10
- its(:type) { should be_nil }
11
- its(:attributes) { should eql({}) }
12
- its(:links) { should eql [] }
13
- its(:subresources) { should eql({}) }
14
- its(:self_link) { should be_nil }
15
- its(:null_resource?) { should be false }
16
- its(:collection?) { should be false }
17
- end
18
-
19
- context 'with a type' do
20
- let(:init_opts) { { type: 'post' } }
21
- its(:type) { should eql 'post' }
22
- end
23
-
24
- context 'with attributes' do
25
- let(:init_opts) { { attributes: {name: 'Arne', age: 31} } }
26
-
27
- it 'should delegate [] to attribute access' do
28
- expect(resource[:name]).to eql 'Arne'
29
- end
30
- end
31
-
32
- context 'with links' do
33
- let(:init_opts) {
34
- {
35
- links: [
36
- Yaks::Resource::Link.new(:profile, '/foo/bar/profile', {}),
37
- Yaks::Resource::Link.new(:self, '/foo/bar', {})
38
- ]
39
- }
40
- }
41
- its(:links) { should eql [
42
- Yaks::Resource::Link.new(:profile, '/foo/bar/profile', {}),
43
- Yaks::Resource::Link.new(:self, '/foo/bar', {})
44
- ]
45
- }
46
-
47
- its(:self_link) { should eql Yaks::Resource::Link.new(:self, '/foo/bar', {}) }
48
- end
49
-
50
- context 'with subresources' do
51
- let(:init_opts) { { subresources: { 'comments' => [Yaks::Resource.new(type: 'comment')] } } }
52
- its(:subresources) { should eql 'comments' => [Yaks::Resource.new(type: 'comment')] }
53
-
54
- it 'should return an enumerator for #each' do
55
- expect(resource.each.with_index.to_a).to eq [ [resource, 0] ]
56
- end
57
- end
58
-
59
-
60
- it 'should act as a collection of one' do
61
- expect(resource.each.to_a).to eql [resource]
62
- end
63
-
64
- describe 'persistent updates' do
65
- let(:resource) {
66
- Yaks::Resource.new(
67
- attributes: {x: :y},
68
- links: [:one],
69
- subresources: {foo_rel: :subres}
70
- )
71
- }
72
-
73
- it 'should do updates without modifying the original' do
74
- expect(
75
- resource
76
- .update_attributes(foo: :bar)
77
- .add_link(:a_link)
78
- .add_subresource(:rel, :a_subresource)
79
- .update_attributes(foo: :baz)
80
- ).to eq Yaks::Resource.new(
81
- attributes: {x: :y, foo: :baz},
82
- links: [:one, :a_link],
83
- subresources: {foo_rel: :subres, rel: :a_subresource}
84
- )
85
-
86
- expect(resource).to eq Yaks::Resource.new(
87
- attributes: {x: :y},
88
- links: [:one],
89
- subresources: {foo_rel: :subres}
90
- )
91
- end
92
- end
93
-
94
- describe '#self_link' do
95
- let(:init_opts) {
96
- { links:
97
- [
98
- Yaks::Resource::Link.new(:self, 'foo', {}),
99
- Yaks::Resource::Link.new(:self, 'bar', {}),
100
- Yaks::Resource::Link.new(:profile, 'baz', {})
101
- ]
102
- }
103
- }
104
- it 'should return the last self link' do
105
- expect(resource.self_link).to eql Yaks::Resource::Link.new(:self, 'bar', {})
106
- end
107
- end
108
- end
@@ -1,260 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe Yaks::Runner do
4
- subject(:runner) {
5
- described_class.new(object: object, config: config, options: options)
6
- }
7
-
8
- let(:object) { Object.new }
9
- let(:config) { Yaks::Config.new }
10
- let(:options) { {} }
11
-
12
- describe '#call' do
13
- let(:runner) {
14
- Class.new(described_class) do
15
- def steps
16
- [ [:step1, ->(x) { x + 35 }],
17
- [:step2, ->(x) { "#{x} #{x}"}] ]
18
- end
19
- end.new(object: object, config: config, options: options)
20
- }
21
-
22
- let(:object) { 7 }
23
-
24
- it 'should go through all the steps' do
25
- expect(runner.call).to eql "42 42"
26
- end
27
- end
28
-
29
- describe '#context' do
30
- it 'should contain the policy, env, and an empty mapper_stack' do
31
- expect(runner.context).to eql(policy: config.policy, env: {}, mapper_stack: [])
32
- end
33
-
34
- context 'with an item mapper' do
35
- let(:options) { { item_mapper: :foo } }
36
-
37
- it 'should contain the item_mapper' do
38
- expect(runner.context).to eql(policy: config.policy, env: {}, mapper_stack: [], item_mapper: :foo)
39
- end
40
- end
41
- end
42
-
43
- describe '#format_class' do
44
- let(:config) do
45
- Yaks::Config.new do
46
- default_format :collection_json
47
- end
48
- end
49
-
50
- let(:rack_env) {
51
- { 'HTTP_ACCEPT' => 'application/hal+json;q=0.8, application/vnd.api+json' }
52
- }
53
-
54
- it 'should fall back to the default when no HTTP_ACCEPT key is present' do
55
- runner = described_class.new(object: nil, config: config, options: { env: {} })
56
- expect(runner.format_class).to equal Yaks::Format::CollectionJson
57
- end
58
-
59
- it 'should detect format based on accept header' do
60
- rack_env = { 'HTTP_ACCEPT' => 'application/hal+json;q=0.8, application/vnd.api+json' }
61
- runner = described_class.new(object: nil, config: config, options: { env: rack_env })
62
- expect(runner.format_class).to equal Yaks::Format::JsonAPI
63
- end
64
-
65
- it 'should know to pick the best match' do
66
- rack_env = { 'HTTP_ACCEPT' => 'application/hal+json;q=0.8, application/vnd.api+json;q=0.7' }
67
- runner = described_class.new(object: nil, config: config, options: { env: rack_env })
68
- expect(runner.format_class).to equal Yaks::Format::Hal
69
- end
70
-
71
- it 'should pick the one given in the options if no header matches' do
72
- rack_env = { 'HTTP_ACCEPT' => 'text/html, application/json' }
73
- runner = described_class.new(object: nil, config: config, options: { format: :hal, env: rack_env })
74
- expect(runner.format_class).to equal Yaks::Format::Hal
75
- end
76
-
77
- it 'should fall back to the default when no mime type is recognized' do
78
- rack_env = { 'HTTP_ACCEPT' => 'text/html, application/json' }
79
- runner = described_class.new(object: nil, config: config, options: { env: rack_env })
80
- expect(runner.format_class).to equal Yaks::Format::CollectionJson
81
- end
82
- end
83
-
84
- describe '#format_name' do
85
- context 'with no format specified' do
86
- it 'should default to :hal' do
87
- expect(runner.format_name).to eql :hal
88
- end
89
- end
90
-
91
- context 'with a default format specified' do
92
- let(:config) { Yaks::Config.new { default_format :collection_json } }
93
-
94
- context 'with a format in the options' do
95
- let(:options) { { format: :json_api } }
96
- it 'should give preference to that one' do
97
- expect(runner.format_name).to eql :json_api
98
- end
99
- end
100
-
101
- context 'without a format in the options' do
102
- it 'should take the specified default' do
103
- expect(runner.format_name).to eql :collection_json
104
- end
105
- end
106
- end
107
- end
108
-
109
- describe '#formatter' do
110
- let(:config) {
111
- Yaks::Config.new do
112
- default_format :json_api
113
- format_options :json_api, {format_option: [:foo]}
114
- end
115
- }
116
-
117
- let(:formatter) { runner.formatter }
118
-
119
- it 'should create a formatter based on class and options' do
120
- expect(formatter).to be_a Yaks::Format::JsonAPI
121
- expect(formatter.send(:options)).to eql(format_option: [:foo])
122
- end
123
- end
124
-
125
- describe '#insert_hooks' do
126
- let(:options) { { mapper: Yaks::Mapper } }
127
- let(:config) { Yaks::Config.new(&hooks) }
128
-
129
- describe 'before' do
130
- let(:hooks) { proc { before(:map) { :before_map_impl } } }
131
-
132
- it 'should insert a hook before the step' do
133
- expect(runner.steps.map(&:first)).to eql [
134
- :before_map, :map, :format, :primitivize, :serialize
135
- ]
136
- expect(runner.steps.assoc(:before_map).last.call).to be :before_map_impl
137
- end
138
- end
139
-
140
- describe 'after' do
141
- let(:hooks) { proc { after(:format) { :after_format_impl } } }
142
-
143
- it 'should insert a hook after the step' do
144
- expect(runner.steps.map(&:first)).to eql [
145
- :map, :format, :after_format, :primitivize, :serialize
146
- ]
147
- expect(runner.steps.assoc(:after_format).last.call).to be :after_format_impl
148
- end
149
- end
150
-
151
- describe 'around' do
152
- let(:hooks) { proc { around(:format) { :around_format_impl } } }
153
-
154
- it 'should insert a hook around the step' do
155
- expect(runner.steps.map(&:first)).to eql [
156
- :map, :format, :primitivize, :serialize
157
- ]
158
- expect(runner.steps.assoc(:format).last.call(nil)).to be :around_format_impl
159
- end
160
- end
161
-
162
- describe 'around' do
163
- let(:hooks) { proc { skip(:serialize) } }
164
-
165
- it 'should insert a hook before the step' do
166
- expect(runner.steps.map(&:first)).to eql [
167
- :map, :format, :primitivize
168
- ]
169
- end
170
- end
171
-
172
- describe 'multiple hooks' do
173
- let(:hooks) {
174
- proc {
175
- after(:format) { :after_format_impl }
176
- skip(:serialize)
177
- }
178
- }
179
-
180
- it 'should insert the hooks' do
181
- expect(runner.steps.map(&:first)).to eql [
182
- :map, :format, :after_format, :primitivize
183
- ]
184
- end
185
- end
186
- end
187
-
188
- describe '#mapper' do
189
- context 'with an explicit mapper in the options' do
190
- let(:mapper_class) { Class.new(Yaks::Mapper) }
191
- let(:options) { { mapper: mapper_class } }
192
-
193
- it 'should take the mapper from options' do
194
- expect(runner.mapper).to be_a mapper_class
195
- end
196
- end
197
-
198
- context 'without a mapper specified' do
199
- let(:object) { Pet.new(id: 7, name: 'fifi', species: 'cat') }
200
-
201
- it 'should infer one from the object to be mapped' do
202
- expect(runner.mapper).to be_a PetMapper
203
- end
204
-
205
- it 'should pass the context to the mapper' do
206
- expect(runner.mapper.context).to be runner.context
207
- end
208
- end
209
- end
210
-
211
- describe '#serializer' do
212
- context 'with a serializer configured' do
213
- let(:config) {
214
- Yaks::Config.new do
215
- json_serializer do |input|
216
- "serialized #{input}"
217
- end
218
- end
219
- }
220
-
221
- it 'should try to find an explicitly configured serializer' do
222
- expect(runner.serializer.call('42')).to eql 'serialized 42'
223
- end
224
- end
225
-
226
- it 'should fall back to the policy' do
227
- expect(runner.serializer.call([1,2,3])).to eql "[\n 1,\n 2,\n 3\n]"
228
- end
229
- end
230
-
231
- describe '#steps' do
232
- let(:options) {{ mapper: Yaks::Mapper }}
233
-
234
- it 'should have all four steps' do
235
- expect(runner.steps).to eql [
236
- [ :map, runner.mapper ],
237
- [ :format, runner.formatter ],
238
- [ :primitivize, runner.primitivizer],
239
- [ :serialize, runner.serializer ]
240
- ]
241
- end
242
-
243
- context 'with hooks' do
244
- let(:config) {
245
- Yaks::Config.new do
246
- after(:format, :my_hook) { :foo }
247
- end
248
- }
249
-
250
- it 'should insert hooks' do
251
- expect(runner.steps.map(&:first)).to eql [:map, :format, :my_hook, :primitivize, :serialize]
252
- end
253
- end
254
- end
255
-
256
- it 'should memoize' do
257
- expect(runner.formatter).to be runner.formatter
258
- end
259
-
260
- end