yaks 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a415152eaf3824112c62b53dcdfb88956ec50b7
4
- data.tar.gz: 2ec4dcbe85142f36ab9205015596225c4119e4c6
3
+ metadata.gz: 64f29f0a260835544b10149465159018b6c4c3cf
4
+ data.tar.gz: 89cbcaece5f55c9cbc24a570318f68d5740867d5
5
5
  SHA512:
6
- metadata.gz: 31371421b3370326cbad06b8384e07f92a926e87d4795e62637e7591cde56e2fc6029c3f9be677109e0e9854ed7dc2a380345579dd01a04cbe441f70e0ea34cc
7
- data.tar.gz: 45bb2d027f706bf0047ace14ed34e875bc74b0eab0cdb103496c8c24a55c58cddfe66775ce9e28524a3c5401a742e62790501400bfcb738187a532367826a654
6
+ metadata.gz: dd0847555ab0d6907f2883acc4ef58d2c6fbf5abc40b07ef45a2f78018d9db62b8eae4ad52877df3ca5882f79d0e197859064ac272dc7d9d38de480a97a7ce55
7
+ data.tar.gz: ff1c2fd464fd5ec9faea3233bde76d0a12f55023fb25d9c4b53bb0f25c38f8b8a2739d5397a804b55c0afa934c6876eb4ea117a578594059e06c50de9e39b458
data/lib/yaks/mapper.rb CHANGED
@@ -55,6 +55,33 @@ module Yaks
55
55
  end
56
56
  alias load_association load_attribute
57
57
 
58
+ def expand_uri(uri, expand)
59
+ case uri
60
+ when nil
61
+ return
62
+ when Symbol
63
+ return load_attribute(uri)
64
+ when Method, Proc
65
+ return Resolve(uri, self)
66
+ end
67
+
68
+ template = URITemplate.new(uri)
69
+ expand_vars = case expand
70
+ when true
71
+ template.variables
72
+ when false
73
+ []
74
+ else
75
+ expand
76
+ end
77
+
78
+ mapping = expand_vars.each_with_object({}) do |name, hsh|
79
+ hsh[name] = load_attribute(name)
80
+ end
81
+
82
+ template.expand_partial(mapping).to_s
83
+ end
84
+
58
85
  private
59
86
 
60
87
  def map_attributes(resource)
@@ -10,10 +10,20 @@ module Yaks
10
10
  new({name: name}.merge(options))
11
11
  end
12
12
 
13
- def add_to_resource(resource, _parent_mapper, _context)
14
- resource.add_control(
15
- Resource::Control.new(to_h.merge(fields: fields.map(&:to_resource_control_field)))
16
- )
13
+ def add_to_resource(resource, parent_mapper, _context)
14
+ resource.add_control(map_to_resource_control(resource, parent_mapper))
15
+ end
16
+
17
+ def map_to_resource_control(resource, parent_mapper)
18
+ attrs = {
19
+ fields: resource_fields,
20
+ href: parent_mapper.expand_uri(href, true)
21
+ }
22
+ Resource::Control.new(to_h.merge(attrs))
23
+ end
24
+
25
+ def resource_fields
26
+ fields.map(&:to_resource_field)
17
27
  end
18
28
 
19
29
  class Field
@@ -27,7 +37,7 @@ module Yaks
27
37
  new(attrs)
28
38
  end
29
39
 
30
- def to_resource_control_field
40
+ def to_resource_field
31
41
  Resource::Control::Field.new(to_h)
32
42
  end
33
43
  end
@@ -28,8 +28,6 @@ module Yaks
28
28
  include Attributes.new(:rel, :template, options: {})
29
29
  include Util
30
30
 
31
- def_delegators :uri_template, :expand_partial
32
-
33
31
  def self.create(rel, template, options = {})
34
32
  new(rel: rel, template: template, options: options)
35
33
  end
@@ -49,29 +47,8 @@ module Yaks
49
47
  !options.fetch(:expand) { true }.equal? true
50
48
  end
51
49
 
52
- def template_variables
53
- case options.fetch(:expand) { true }
54
- when true
55
- uri_template.variables
56
- when false
57
- []
58
- else
59
- options[:expand]
60
- end.map(&:to_sym)
61
- end
62
-
63
- def expansion_mapping(lookup)
64
- template_variables.each_with_object({}) do |name, hsh|
65
- hsh[name] = lookup[name]
66
- end
67
- end
68
-
69
- def uri_template
70
- URITemplate.new(template)
71
- end
72
-
73
50
  def map_to_resource_link(mapper)
74
- uri = expand_with(mapper.method(:load_attribute))
51
+ uri = mapper.expand_uri(template, options.fetch(:expand, true))
75
52
  return if uri.nil?
76
53
 
77
54
  Resource::Link.new(
@@ -81,12 +58,6 @@ module Yaks
81
58
  )
82
59
  end
83
60
 
84
- def expand_with(lookup)
85
- return lookup[template] if template.instance_of? Symbol
86
-
87
- expand_partial(expansion_mapping(lookup)).to_s
88
- end
89
-
90
61
  def resource_link_options(mapper)
91
62
  options = options()
92
63
  options = options.merge(title: Resolve(options[:title], mapper)) if options.key?(:title)
data/lib/yaks/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yaks
2
- VERSION = '0.7.1'
2
+ VERSION = '0.7.2'
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ $VERBOSE=true
2
+
1
3
  require 'rspec/its'
2
4
  require 'bogus/rspec'
3
5
  require 'timeout'
@@ -26,9 +26,9 @@ RSpec.describe Yaks::Mapper::Control::Field do
26
26
  end
27
27
  end
28
28
 
29
- describe 'to_resource_field' do
29
+ describe '#to_resource_field' do
30
30
  it 'creates a Yaks::Resource::Control::Field with the same attributes' do
31
- expect(field.to_resource_control_field).to eql Yaks::Resource::Control::Field.new(full_args)
31
+ expect(field.to_resource_field).to eql Yaks::Resource::Control::Field.new(full_args)
32
32
  end
33
33
  end
34
34
  end
@@ -27,7 +27,7 @@ RSpec.describe Yaks::Mapper::Control do
27
27
  end
28
28
 
29
29
  describe '#add_to_resource' do
30
- let(:resource) { control.add_to_resource(Yaks::Resource.new, nil, nil) }
30
+ let(:resource) { control.add_to_resource(Yaks::Resource.new, Yaks::Mapper.new(nil), nil) }
31
31
 
32
32
  it 'should add a control to the resource' do
33
33
  expect(resource.controls.length).to be 1
@@ -9,9 +9,6 @@ RSpec.describe Yaks::Mapper::Link do
9
9
  let(:template) { '/foo/bar/{x}/{y}' }
10
10
  let(:options) { {} }
11
11
 
12
- its(:template_variables) { should eq [:x, :y] }
13
- its(:uri_template) { should eq URITemplate.new(template) }
14
-
15
12
  let(:object) { Struct.new(:x, :y, :returns_nil).new(3, 4, nil) }
16
13
 
17
14
  let(:mapper_class) do
@@ -71,44 +68,6 @@ RSpec.describe Yaks::Mapper::Link do
71
68
  end
72
69
  end
73
70
 
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
71
  describe '#map_to_resource_link' do
113
72
  subject(:resource_link) { link.map_to_resource_link(mapper) }
114
73
 
@@ -329,4 +329,48 @@ RSpec.describe Yaks::Mapper do
329
329
  expect(mapper.mapper_stack).to eql [:foo]
330
330
  end
331
331
  end
332
+
333
+ describe '#expand_uri' do
334
+ let(:template) { '/foo/bar/{x}/{y}' }
335
+ let(:expand) { true }
336
+
337
+ subject(:expanded) { mapper.expand_uri(template, expand) }
338
+
339
+ before do
340
+ mapper.call( Struct.new(:x, :y) { def foo ; '/foo/foo' ; end }.new(6, 7) )
341
+ end
342
+
343
+ context 'with expansion turned off' do
344
+ let(:expand) { false }
345
+
346
+ it 'should keep the template in the response' do
347
+ expect(expanded).to eq '/foo/bar/{x}/{y}'
348
+ end
349
+ end
350
+
351
+ context 'with a URI without expansion variables' do
352
+ let(:template) { '/orders' }
353
+
354
+ it 'should return the link as is' do
355
+ expect(expanded).to eq '/orders'
356
+ end
357
+ end
358
+
359
+ context 'with partial expansion' do
360
+ let(:expand) { [:y] }
361
+
362
+ it 'should only expand the given variables' do
363
+ expect(expanded).to eql '/foo/bar/{x}/7'
364
+ end
365
+ end
366
+
367
+ context 'with a symbol for a template' do
368
+ let(:template) { :foo }
369
+
370
+ it 'should use the lookup mechanism for finding the link' do
371
+ expect(expanded).to eq '/foo/foo'
372
+ end
373
+ end
374
+ end
375
+
332
376
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arne Brasseur
@@ -346,18 +346,7 @@ homepage: https://github.com/plexus/yaks
346
346
  licenses:
347
347
  - MIT
348
348
  metadata: {}
349
- post_install_message: |2
350
-
351
- Breaking Changes in Yaks 0.7.0
352
- ==============================
353
- Yaks::Resource#subresources is now an array, not a hash. The rel is
354
- stored on the resource itself as Yaks::Resource#rels (an array). This
355
- should only be of concern if you implement custom output formats
356
-
357
- The general signature of all processing steps (mapper, formatter,
358
- hooks) has changed to incldue a second parameter, the rack env. If you
359
- have custom implementations of any of these, or hooks that are not
360
- specified as ruby blocks, you will need to take this into account
349
+ post_install_message:
361
350
  rdoc_options: []
362
351
  require_paths:
363
352
  - lib