yaks 0.8.0.beta1 → 0.8.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad5b36d2206adbc6ccc3a79eb484d70eb6a56f62
4
- data.tar.gz: e902f2b6b88e269d4dc788e3e19969f881ec107c
3
+ metadata.gz: e70fb16fe2a8b8d06d5748409ac51ce7de353cba
4
+ data.tar.gz: ed91cadda25837e29c307bed1caa86b7e93bb5b4
5
5
  SHA512:
6
- metadata.gz: 38efaa84bb8b2ae036fd7bc04aae45da7d9ecd8cb386f7dfbe95ee0470c52ffdf1bbf62c81893f599fa030a9859fe1496c764bc744a3d44ddaceecc36f1de619
7
- data.tar.gz: 93e074f082306f8a8bbd6f36a0564a3d30c3b056c2b29061506155ffaec1725268170d427f751fda58959ece321fa625fc00f712877d5040a9727cb88bf01b0e
6
+ metadata.gz: 61c520e3fa43ee9de85d54b1bf478c3d0ef68933f98c31668a8f7330bf75b1306edb1f442824434843e50d0d03068ad7f0a0a05fa8212adbf895ebd24ce97e40
7
+ data.tar.gz: ad0ab6ae6c9d0d385baf8bdaeef55c6cbffdaf2a3b87fadf30e7d28f2cf0a17bfa71967396963771aeebe5decd75ef2e986ebfe634501145e4e56c18eeb5a416
data/Rakefile CHANGED
@@ -82,10 +82,11 @@ task :mutant_chunked do
82
82
  # >> Yaks::Reader::Hal
83
83
  # Yaks::Resource,
84
84
  # Yaks::Resource::Form,
85
- Yaks::Resource::Form::Field,
86
- Yaks::Resource::Form::Field::Option,
87
- Yaks::Resource::Form::Fieldset,
88
- Yaks::Resource::Link,
85
+ # Yaks::Resource::Form::Field,
86
+ # Yaks::Resource::Form::Field::Option,
87
+ # Yaks::Resource::Form::Fieldset,
88
+ #Yaks::Resource::Link,
89
+ Yaks::Resource::HasFields,
89
90
  # >> Yaks::Runner
90
91
  # >> Yaks::RuntimeError
91
92
  # >> Yaks::Serializer
@@ -33,11 +33,12 @@ module Yaks
33
33
  end
34
34
 
35
35
  def add_to_resource(resource, mapper, _context)
36
+ return resource.with(links: resource.links.reject {|link| link.rel?(rel)}) if options[:remove]
37
+
36
38
  resource_link = map_to_resource_link(mapper)
37
39
  return resource unless resource_link
38
- if options[:remove]
39
- resource.with(links: resource.links.reject {|link| link.rel?(rel)})
40
- elsif options[:replace]
40
+
41
+ if options[:replace]
41
42
  resource.with(links: resource.links.reject {|link| link.rel?(rel)} << resource_link)
42
43
  else
43
44
  resource.add_link(resource_link)
@@ -6,12 +6,38 @@ module Yaks
6
6
 
7
7
  def value
8
8
  if type.equal? :select
9
- selected = options.find { |option| option.selected }
9
+ selected = options.find(&:selected)
10
10
  selected.value if selected
11
11
  else
12
12
  @value
13
13
  end
14
14
  end
15
+
16
+ def with_value(value)
17
+ if type.equal? :select
18
+ with(options: select_options_for_value(value))
19
+ else
20
+ with(value: value)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def select_options_for_value(value)
27
+ unset = ->(option) { option.selected && !value().eql?(value) }
28
+ set = ->(option) { !option.selected && option.value.eql?(value) }
29
+
30
+ options.each_with_object([]) do |option, new_opts|
31
+ new_opts << case option
32
+ when unset
33
+ option.update selected: false
34
+ when set
35
+ option.update selected: true
36
+ else
37
+ option
38
+ end
39
+ end
40
+ end
15
41
  end
16
42
  end
17
43
  end
@@ -3,6 +3,7 @@ module Yaks
3
3
  class Form
4
4
  class Fieldset
5
5
  include Attributes.new(:fields)
6
+ include Yaks::Resource::HasFields
6
7
 
7
8
  def type
8
9
  :fieldset
@@ -2,6 +2,7 @@ module Yaks
2
2
  class Resource
3
3
  class Form
4
4
  include Yaks::Mapper::Form::Config.attributes.remove(:dynamic_blocks)
5
+ include Yaks::Resource::HasFields
5
6
 
6
7
  def [](name)
7
8
  fields.find {|field| field.name.equal? name}.value
@@ -0,0 +1,17 @@
1
+ module Yaks
2
+ class Resource
3
+ module HasFields
4
+ def map_fields(&block)
5
+ with(
6
+ fields: fields.map do |field|
7
+ if field.type.equal? :fieldset
8
+ field.map_fields(&block)
9
+ else
10
+ block.call(field)
11
+ end
12
+ end
13
+ )
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/yaks/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yaks
2
- VERSION = '0.8.0.beta1'
2
+ VERSION = '0.8.0.beta2'
3
3
  end
data/lib/yaks.rb CHANGED
@@ -85,6 +85,7 @@ require 'yaks/mapper'
85
85
  require 'yaks/mapper/association_mapper'
86
86
  require 'yaks/collection_mapper'
87
87
 
88
+ require 'yaks/resource/has_fields'
88
89
  require 'yaks/resource/form'
89
90
  require 'yaks/resource/form/field'
90
91
  require 'yaks/resource/form/field/option'
@@ -0,0 +1,88 @@
1
+ RSpec.describe Yaks::Resource::Form::Field do
2
+ subject do
3
+ described_class.new(type: 'text', name: 'foo', value: 123)
4
+ end
5
+
6
+ describe '#value' do
7
+ its(:value) { should eql 123 }
8
+
9
+ context 'with a select box - with selection' do
10
+ subject do
11
+ described_class.new(name: 'foo', type: :select, options: [
12
+ Yaks::Resource::Form::Field::Option.new(label: 'foo', selected: false, value: 1),
13
+ Yaks::Resource::Form::Field::Option.new(label: 'foo', selected: true, value: 2),
14
+ Yaks::Resource::Form::Field::Option.new(label: 'foo', selected: false, value: 3),
15
+ ])
16
+ end
17
+
18
+ it 'should return the selected value' do
19
+ expect( subject.value ).to eql 2
20
+ end
21
+ end
22
+
23
+ context 'with a select box - no selection' do
24
+ subject do
25
+ described_class.new(name: 'foo', type: :select, options: [
26
+ Yaks::Resource::Form::Field::Option.new(label: 'foo', selected: false, value: 1),
27
+ Yaks::Resource::Form::Field::Option.new(label: 'foo', selected: false, value: 2),
28
+ Yaks::Resource::Form::Field::Option.new(label: 'foo', selected: false, value: 3),
29
+ ])
30
+ end
31
+
32
+ it 'should return nothing' do
33
+ expect( subject.value ).to be nil
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '#with_value' do
39
+ context 'with a regular field' do
40
+ it 'should update the given attributes' do
41
+ expect(subject.with_value(321).value).to eql 321
42
+ end
43
+ end
44
+
45
+ context 'with a select field' do
46
+ subject do
47
+ described_class.new(name: 'foo', type: :select, options: [
48
+ Yaks::Resource::Form::Field::Option.new(label: 'f', selected: true, value: "1"),
49
+ Yaks::Resource::Form::Field::Option.new(label: 'f', selected: false, value: "2"),
50
+ Yaks::Resource::Form::Field::Option.new(label: 'f', selected: true, value: "3"),
51
+ Yaks::Resource::Form::Field::Option.new(label: 'f', selected: false, value: "4"),
52
+ ])
53
+ end
54
+
55
+ let(:updated) { subject.with_value("2") }
56
+
57
+ it 'should keep existing attributes' do
58
+ expect([updated.name, updated.type]).to eq ['foo', :select]
59
+ end
60
+
61
+ context 'when changing from a previous value' do
62
+ it 'should update the affected option' do
63
+ expect(updated.value).to eq "2"
64
+ end
65
+
66
+ it 'should reuse existing Option instances' do
67
+ expect(updated.options.last).to equal subject.options.last
68
+ end
69
+
70
+ it 'should unset all selected options' do
71
+ expect(updated.options.map(&:selected)).to eq [false, true, false, false]
72
+ end
73
+ end
74
+
75
+ context 'when keeping the old value value' do
76
+ let(:updated) { subject.with_value("1") }
77
+
78
+ it 'should not change the value' do
79
+ expect(updated.value).to eq "1"
80
+ end
81
+
82
+ it 'should reuse existing Option instances' do
83
+ expect(updated.options.first).to equal subject.options.first
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -48,4 +48,5 @@ RSpec.describe Yaks::Resource::Form do
48
48
  ]
49
49
  end
50
50
  end
51
+
51
52
  end
@@ -0,0 +1,50 @@
1
+ RSpec.describe Yaks::Resource::HasFields do
2
+ let(:class_with_fields) do
3
+ Class.new do
4
+ include Yaks::Resource::HasFields
5
+ include Yaks::Attributes.new(:fields)
6
+ end
7
+ end
8
+
9
+ let(:fields) do
10
+ [
11
+ Yaks::Resource::Form::Fieldset.new(fields: [
12
+ Yaks::Resource::Form::Field.new(name: :foo, value: '123', type: 'text'),
13
+ Yaks::Resource::Form::Field.new(name: :bar, value: '+32 477 123 123', type: 'tel')
14
+ ]),
15
+ Yaks::Resource::Form::Fieldset.new(fields: [
16
+ Yaks::Resource::Form::Fieldset.new(fields: [
17
+ Yaks::Resource::Form::Field.new(name: :quux, value: '999', type: 'tel')
18
+ ]),
19
+ Yaks::Resource::Form::Field.new(name: :qux, value: '777', type: 'text')
20
+ ])
21
+ ]
22
+ end
23
+
24
+ subject(:with_fields) { class_with_fields.new(fields: fields) }
25
+
26
+ describe '#map_fields' do
27
+
28
+ let(:update_fields) do
29
+ ->(field) do
30
+ field.with({ value: "updated" })
31
+ end
32
+ end
33
+
34
+ it 'will map over nested fieldsets' do
35
+ expect(subject.map_fields(&update_fields)).to eql class_with_fields.new(fields: [
36
+ Yaks::Resource::Form::Fieldset.new(fields: [
37
+ Yaks::Resource::Form::Field.new(name: :foo, value: 'updated', type: 'text'),
38
+ Yaks::Resource::Form::Field.new(name: :bar, value: 'updated', type: 'tel')
39
+ ]),
40
+ Yaks::Resource::Form::Fieldset.new(fields: [
41
+ Yaks::Resource::Form::Fieldset.new(fields: [
42
+ Yaks::Resource::Form::Field.new(name: :quux, value: 'updated', type: 'tel')
43
+ ]),
44
+ Yaks::Resource::Form::Field.new(name: :qux, value: 'updated', type: 'text')
45
+ ])
46
+ ])
47
+ end
48
+ end
49
+
50
+ end
@@ -16,4 +16,16 @@ RSpec.describe Yaks::Resource::Link do
16
16
  let(:options) { super().merge(templated: true) }
17
17
  its(:templated?) { should be true }
18
18
  end
19
+
20
+ describe '#rel?' do
21
+ let(:rel) { "/rels/foo" }
22
+
23
+ it 'should be true if the rel matches' do
24
+ expect(link.rel?("/rels/foo")).to be true
25
+ end
26
+
27
+ it 'should be false if the rel does not match' do
28
+ expect(link.rel?(:foo)).to be false
29
+ end
30
+ end
19
31
  end
@@ -162,4 +162,3 @@ RSpec.describe Yaks::Resource do
162
162
  end
163
163
 
164
164
  end
165
- # ~> -:1:in `<main>': uninitialized constant RSpec (NameError)
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.0.beta1
4
+ version: 0.8.0.beta2
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-01-09 00:00:00.000000000 Z
11
+ date: 2015-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inflection
@@ -287,6 +287,7 @@ files:
287
287
  - lib/yaks/resource/form/field.rb
288
288
  - lib/yaks/resource/form/field/option.rb
289
289
  - lib/yaks/resource/form/fieldset.rb
290
+ - lib/yaks/resource/has_fields.rb
290
291
  - lib/yaks/resource/link.rb
291
292
  - lib/yaks/runner.rb
292
293
  - lib/yaks/serializer.rb
@@ -347,8 +348,9 @@ files:
347
348
  - spec/unit/yaks/null_resource_spec.rb
348
349
  - spec/unit/yaks/pipeline_spec.rb
349
350
  - spec/unit/yaks/primitivize_spec.rb
350
- - spec/unit/yaks/resource/form/field_sepc.rb
351
+ - spec/unit/yaks/resource/form/field_spec.rb
351
352
  - spec/unit/yaks/resource/form_spec.rb
353
+ - spec/unit/yaks/resource/has_fields_spec.rb
352
354
  - spec/unit/yaks/resource/link_spec.rb
353
355
  - spec/unit/yaks/resource_spec.rb
354
356
  - spec/unit/yaks/runner_spec.rb
@@ -436,8 +438,9 @@ test_files:
436
438
  - spec/unit/yaks/null_resource_spec.rb
437
439
  - spec/unit/yaks/pipeline_spec.rb
438
440
  - spec/unit/yaks/primitivize_spec.rb
439
- - spec/unit/yaks/resource/form/field_sepc.rb
441
+ - spec/unit/yaks/resource/form/field_spec.rb
440
442
  - spec/unit/yaks/resource/form_spec.rb
443
+ - spec/unit/yaks/resource/has_fields_spec.rb
441
444
  - spec/unit/yaks/resource/link_spec.rb
442
445
  - spec/unit/yaks/resource_spec.rb
443
446
  - spec/unit/yaks/runner_spec.rb
@@ -1,37 +0,0 @@
1
- RSpec.describe Yaks::Resource::Form::Field do
2
- subject do
3
- described_class.new(value: 123)
4
- end
5
-
6
- describe '#value' do
7
- its(:value) { should eql 123 }
8
-
9
- context 'with a select box - with selection' do
10
- subject do
11
- described.class.new(type: :select, options: [
12
- Yaks::Resource::Form::Field::Option.new(selected: false, value: 1),
13
- Yaks::Resource::Form::Field::Option.new(selected: true, value: 2),
14
- Yaks::Resource::Form::Field::Option.new(selected: false, value: 3),
15
- ])
16
- end
17
-
18
- it 'should return the selected value' do
19
- expect( subject.value ).to eql 2
20
- end
21
- end
22
-
23
- context 'with a select box - no selection' do
24
- subject do
25
- described.class.new(type: :select, options: [
26
- Yaks::Resource::Form::Field::Option.new(selected: false, value: 1),
27
- Yaks::Resource::Form::Field::Option.new(selected: false, value: 2),
28
- Yaks::Resource::Form::Field::Option.new(selected: false, value: 3),
29
- ])
30
- end
31
-
32
- it 'should return nothing' do
33
- expect( subject.value ).to be nil
34
- end
35
- end
36
- end
37
- end