yaks 0.11.0 → 0.12.0

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: 17023a2415d74fcf0ea0c840dfa7f1d2fcb8afb4
4
- data.tar.gz: 3fc6a562a7ba74d7064d2898f04ed32d1ba1b3b4
3
+ metadata.gz: 558f64d7a2d93038fc00cad3ff0c7442b3458e6f
4
+ data.tar.gz: 60e1a98b810c825d8bc44efa69fda28656970153
5
5
  SHA512:
6
- metadata.gz: 159c85fc53b2b7aa8e6fbc9a23fa3c1fa969f667e36e96c58e97ac4bc76b52bf7aaa1e95e309d319c121eeaafd30dc71a7de7fef3cca0173aae844593f3a5238
7
- data.tar.gz: c0ec3d24906a2b12accd6548e574679e239def3d9e71228e1c35812c302c8c92afc118d3af47927b82e5c329bfeee0190df7b8b8f54300c1f3bc7eb1ca6a21db
6
+ metadata.gz: 362873163152bd0ca6466a70b0aeb2c58bb8b024ead9920af7869697aef5d6a52a5b50a5b31973afee16213da5f1dc85734e833ca9a8b9b9b634d10b047bfa1f
7
+ data.tar.gz: 9ab584641aacf534cdd60c42f13229073867f8e3f83973d78ef92cd268574656272c15311a7091cc3764dab74f89612e0d51a09d2a02009867a210406958de1d
@@ -20,7 +20,7 @@ env = Mutant::Env::Bootstrap.call(Mutant::CLI.call(args))
20
20
  integration = env.config.integration
21
21
 
22
22
  integration.setup
23
- binding.pry if integration.all_tests.empty? # rubocop:disable Lint/Debugger
23
+ binding.pry if integration.all_tests.empty? # rubocop:disable Lint/Debugger
24
24
 
25
25
  env.subjects.each do |subject|
26
26
  match_expression = subject.match_expressions.first
@@ -21,7 +21,7 @@ module Yaks
21
21
 
22
22
  def serialize_form(form)
23
23
  raw = form.to_h_compact
24
- raw[:href] = raw.delete(:action) if raw[:action]
24
+ raw[:href] = raw.delete(:action) if raw[:action]
25
25
  raw[:fields] = form.fields.map(&method(:serialize_form_field))
26
26
  raw
27
27
  end
@@ -9,7 +9,7 @@ module Yaks
9
9
  @context = context.merge(
10
10
  mapper_stack: context[:mapper_stack] + [parent_mapper]
11
11
  )
12
- @rel = association.map_rel(policy)
12
+ @rel = association.map_rel(policy) # rubocop:disable Style/ExtraSpacing
13
13
  end
14
14
 
15
15
  def policy
@@ -1,19 +1,22 @@
1
1
  module Yaks
2
2
  class Mapper
3
3
  class Attribute
4
- include Attribs.new(:name, :block)
5
- include Util
4
+ extend Forwardable, Util
5
+ include Attribs.new(:name, :block, if: true), Util
6
6
 
7
- def self.create(name, _options = nil, &block)
8
- new(name: name, block: block)
7
+ def self.create(name, options = {}, &block)
8
+ new(options.merge(name: name, block: block))
9
9
  end
10
10
 
11
11
  def add_to_resource(resource, mapper, _context)
12
+ return resource unless Resolve(self.if, mapper)
13
+
12
14
  if block
13
15
  attribute = Resolve(block, mapper)
14
16
  else
15
17
  attribute = mapper.load_attribute(name)
16
18
  end
19
+
17
20
  resource.merge_attributes(name => attribute)
18
21
  end
19
22
  end
@@ -14,7 +14,7 @@ module Yaks
14
14
  to_enum
15
15
  end
16
16
 
17
- def collection?
17
+ def collection? # rubocop:disable Style/TrivialAccessors
18
18
  @collection
19
19
  end
20
20
 
@@ -7,13 +7,13 @@ module Yaks
7
7
  if parsed_json['data'].is_a?(Array)
8
8
  CollectionResource.new(
9
9
  attributes: parsed_json['meta'].nil? ? nil : {meta: parsed_json['meta']},
10
- members: parsed_json['data'].map { |data| call('data' => data, 'included' => included) }
10
+ members: parsed_json['data'].map { |data| call('data' => data, 'included' => included) }
11
11
  )
12
12
  else
13
13
  attributes = parsed_json['data'].dup
14
14
  links = attributes.delete('links') || {}
15
15
  relationships = attributes.delete('relationships') || {}
16
- type = attributes.delete('type')
16
+ type = attributes.delete('type')
17
17
  attributes.merge!(attributes.delete('attributes') || {})
18
18
 
19
19
  embedded = convert_embedded(Hash[relationships], included)
@@ -48,14 +48,14 @@ module Yaks
48
48
  CollectionResource.new(
49
49
  members: data.map { |link|
50
50
  data = included.find{ |item| (item['id'] == link['id']) && (item['type'] == link['type']) }
51
- call('data' => data, 'included' => included)
51
+ call('data' => data, 'included' => included)
52
52
  },
53
53
  rels: [rel]
54
54
  )
55
55
  end
56
56
  else
57
57
  data = included.find{ |item| (item['id'] == data['id']) && (item['type'] == data['type']) }
58
- call('data' => data, 'included' => included).with(rels: [rel])
58
+ call('data' => data, 'included' => included).with(rels: [rel])
59
59
  end
60
60
  end.compact
61
61
  end
@@ -18,7 +18,7 @@ module Yaks
18
18
  !method.nil? && method.downcase.to_sym == meth.downcase.to_sym
19
19
  end
20
20
 
21
- def has_action? # rubocop:disable Style/PredicateName
21
+ def has_action? # rubocop:disable Style/PredicateName
22
22
  !action.nil?
23
23
  end
24
24
  end
@@ -46,7 +46,7 @@ module Yaks
46
46
  # A proc or a plain value
47
47
  # @param [Object] context
48
48
  # (optional) A context used to instance_eval the proc
49
- def Resolve(maybe_proc, context = nil) # rubocop:disable Style/MethodName
49
+ def Resolve(maybe_proc, context = nil) # rubocop:disable Style/MethodName
50
50
  if maybe_proc.respond_to?(:to_proc) && !maybe_proc.instance_of?(Symbol)
51
51
  if context
52
52
  if maybe_proc.arity > 0
@@ -1,3 +1,3 @@
1
1
  module Yaks
2
- VERSION = '0.11.0'
2
+ VERSION = '0.12.0'
3
3
  end
@@ -70,6 +70,6 @@ RSpec.describe Yaks::Format::CollectionJson do
70
70
  end
71
71
  }
72
72
 
73
- include_examples 'JSON Writer', 'youtypeitwepostit'
73
+ include_examples 'JSON Writer', 'youtypeitwepostit'
74
74
  end
75
75
  end
@@ -1,6 +1,6 @@
1
1
  RSpec.shared_examples_for 'JSON Writer' do |fixture_name|
2
2
  describe 'Yaks::Resource => JSON' do
3
- let(:object) { load_yaml_fixture(fixture_name) }
3
+ let(:object) { load_yaml_fixture(fixture_name) }
4
4
  let(:json_fixture) { load_json_fixture("#{fixture_name}.#{format_name}") }
5
5
  let(:serialized) {
6
6
  yaks_config.call(object, hooks: [[:skip, :serialize]], format: format_name)
@@ -12,9 +12,9 @@ RSpec.describe Yaks::Config do
12
12
  its(:default_format) { should equal :hal }
13
13
  its(:policy_class) { should <= Yaks::DefaultPolicy }
14
14
  its(:primitivize) { should be_a Yaks::Primitivize }
15
- its(:serializers) { should eql(Yaks::Serializer.all) }
16
- its(:hooks) { should eql([]) }
17
- its(:format_options_hash) { should eql({})}
15
+ its(:serializers) { should eql(Yaks::Serializer.all) }
16
+ its(:hooks) { should eql([]) }
17
+ its(:format_options_hash) { should eql({}) }
18
18
  end
19
19
  end
20
20
 
@@ -1,3 +1,5 @@
1
+ require 'securerandom'
2
+
1
3
  class Kitten
2
4
  include Attribs.new(:furriness)
3
5
 
@@ -322,7 +322,7 @@ RSpec.describe Yaks::Format::CollectionJson do
322
322
  ],
323
323
  "queries" => [
324
324
  {"href" => "/foo", "rel" => "search", "prompt" => "My query prompt",
325
- "data" => [
325
+ "data" => [
326
326
  {"name" => "foo", "value" => ""},
327
327
  {"name" => "bar", "value" => "", "prompt" => "My Bar Field"}
328
328
  ]
@@ -56,7 +56,7 @@ RSpec.describe Yaks::Mapper::Association do
56
56
  end
57
57
 
58
58
  context 'with a truthy condition' do
59
- let(:if) { ->{ true } }
59
+ let(:if) { ->{ true } }
60
60
 
61
61
  it 'should add the association' do
62
62
  expect(association.add_to_resource(Yaks::Resource.new, parent_mapper, yaks_context).subresources.length).to be 1
@@ -70,7 +70,7 @@ RSpec.describe Yaks::Mapper::Association do
70
70
  end
71
71
 
72
72
  context 'with a falsey condition' do
73
- let(:if) { ->{ false } }
73
+ let(:if) { ->{ false } }
74
74
 
75
75
  it 'should not add the association' do
76
76
  expect(association.add_to_resource(Yaks::Resource.new, parent_mapper, yaks_context).subresources.length).to be 0
@@ -182,7 +182,7 @@ RSpec.describe Yaks::Mapper::Association do
182
182
  end
183
183
 
184
184
  it 'should not munge the options hash' do
185
- opts = {mapper: :foo}
185
+ opts = {mapper: :foo}
186
186
  association_class.create(:foo, opts)
187
187
  expect(opts).to eql(mapper: :foo)
188
188
  end
@@ -2,7 +2,6 @@ RSpec.describe Yaks::Mapper::Attribute do
2
2
  include_context 'yaks context'
3
3
 
4
4
  let(:attribute_with_block) { described_class.create(:the_name) { "Alice" } }
5
-
6
5
  subject(:attribute) { described_class.create(:the_name) }
7
6
  fake(:mapper)
8
7
 
@@ -13,10 +12,11 @@ RSpec.describe Yaks::Mapper::Attribute do
13
12
 
14
13
  describe ".create" do
15
14
  its(:name) { should be :the_name }
15
+ its(:if) { should be_truthy }
16
16
  its(:block) { should be_nil }
17
17
 
18
- it "should accept two parameter" do
19
- expect{described_class.create(:the_name, {})}.not_to raise_error()
18
+ it "should accept only the name" do
19
+ expect{described_class.create(:the_name)}.not_to raise_error()
20
20
  end
21
21
 
22
22
  context "with block" do
@@ -28,14 +28,95 @@ RSpec.describe Yaks::Mapper::Attribute do
28
28
  expect(subject.block.call).to eq("Alice")
29
29
  end
30
30
  end
31
+
32
+ describe "options" do
33
+
34
+ let(:object) { Struct.new(:x, :y, :returns_nil).new(3, 4, nil) }
35
+
36
+ let(:mapper_class) do
37
+ Class.new(Yaks::Mapper) do
38
+ type 'foo'
39
+ end
40
+ end
41
+
42
+ let(:mapper) do
43
+ mapper_class.new(yaks_context).tap do |mapper|
44
+ mapper.call(object) # set @object
45
+ end
46
+ end
47
+
48
+ subject(:attribute) { described_class.create(:the_name, options) }
49
+
50
+ context 'with :if defined and resolving to false' do
51
+
52
+ let(:options) { {if: ->{ false }} }
53
+
54
+ it 'should not render the attribute' do
55
+ expect(attribute.add_to_resource(Yaks::Resource.new, mapper, yaks_context)).not_to eql(
56
+ Yaks::Resource.new(attributes: {the_name: 123})
57
+ )
58
+ end
59
+ end
60
+
61
+ context 'with :if defined and resolving to true' do
62
+ let(:options) { {if: ->{ true }} }
63
+
64
+ it 'should render the attribute' do
65
+ expect(attribute.add_to_resource(Yaks::Resource.new, mapper, yaks_context)).to eql(
66
+ Yaks::Resource.new(attributes: {the_name: 123})
67
+ )
68
+ end
69
+ end
70
+
71
+ context 'with :if defined and resolving to true via instance_eval' do
72
+ let(:options) { {if: ->{ object.name.length > 2 } } }
73
+
74
+ it 'should render the attribute' do
75
+ expect(attribute.add_to_resource(Yaks::Resource.new, mapper, yaks_context)).to eql(
76
+ Yaks::Resource.new(attributes: {the_name: 123})
77
+ )
78
+ end
79
+ end
80
+ end
81
+
31
82
  end
32
83
 
33
84
  describe "#add_to_resource" do
85
+
86
+ let(:options) { {if: ->{ true }} }
87
+ let(:options_false) { {if: ->{ 0 == 1 }} }
88
+ let(:attribute_with_block_and_false_options) {described_class.create(:the_name,options_false) { "Alice" } }
89
+ let(:attribute_with_block_and_options) { described_class.create(:the_name,options) { "Alice" } }
90
+
91
+
92
+
34
93
  it "should add itself to a resource based on a lookup" do
35
94
  expect(attribute.add_to_resource(Yaks::Resource.new, mapper, yaks_context))
36
95
  .to eql(Yaks::Resource.new(attributes: {the_name: 123}))
37
96
  end
38
97
 
98
+ context "when the attribute has a block and true options" do
99
+ subject(:attribute) { attribute_with_block_and_options }
100
+
101
+ it "should add itself to a resource with the block value" do
102
+ expect(attribute.add_to_resource(Yaks::Resource.new, mapper, yaks_context))
103
+ .to eql(Yaks::Resource.new(attributes: {the_name: "Alice"}))
104
+ end
105
+
106
+
107
+ end
108
+
109
+ context "when the attribute has a block and false options" do
110
+ subject(:attribute) { attribute_with_block_and_false_options }
111
+
112
+ it "should not add itself to a resource with the block value" do
113
+ expect(attribute.add_to_resource(Yaks::Resource.new, mapper, yaks_context))
114
+ .not_to eql(Yaks::Resource.new(attributes: {the_name: "Alice"}))
115
+ end
116
+
117
+
118
+ end
119
+
39
120
  context "when the attribute has a block" do
40
121
  subject(:attribute) { attribute_with_block }
41
122
 
@@ -22,7 +22,7 @@ RSpec.describe Yaks::Mapper::Form::Fieldset do
22
22
 
23
23
  context "with extra options" do
24
24
  let(:fieldset) {
25
- described_class.create if: true do
25
+ described_class.create if: true do
26
26
  text :first_name
27
27
  end
28
28
  }
@@ -72,7 +72,7 @@ RSpec.describe Yaks::Mapper::HasMany do
72
72
  describe '#collection_mapper' do
73
73
  let(:collection_mapper) { Yaks::Undefined }
74
74
 
75
- subject(:has_many) { described_class.new(name: :name, item_mapper: :mapper, rel: :rel, collection_mapper: collection_mapper) }
75
+ subject(:has_many) { described_class.new(name: :name, item_mapper: :mapper, rel: :rel, collection_mapper: collection_mapper) }
76
76
 
77
77
  context 'when the collection mapper is undefined' do
78
78
  it 'should derive one from collection and policy' do
@@ -3,7 +3,7 @@ RSpec.describe Yaks::Mapper::HasOne do
3
3
 
4
4
  AuthorMapper = Class.new(Yaks::Mapper) { attributes :name }
5
5
 
6
- subject(:has_one) do
6
+ subject(:has_one) do
7
7
  described_class.new(
8
8
  name: :author,
9
9
  item_mapper: association_mapper,
@@ -16,7 +16,7 @@ RSpec.describe Yaks::Pipeline do
16
16
  end
17
17
 
18
18
  describe '#insert_hooks' do
19
- let(:hooks) { [] }
19
+ let(:hooks) { [] }
20
20
 
21
21
  describe 'before' do
22
22
  let(:hooks) { [[:before, :step2, :before_step2, ->(i, _e) { i - (i % 100) }]] }
@@ -10,7 +10,7 @@ RSpec.describe Yaks::Resource::Link do
10
10
  its(:options) { should eql(title: 'mr. spectacular') }
11
11
 
12
12
  describe '#title' do
13
- its(:title) { should eql('mr. spectacular') }
13
+ its(:title) { should eql('mr. spectacular') }
14
14
  end
15
15
 
16
16
  describe '#templated?' do
@@ -20,7 +20,7 @@ RSpec.describe Yaks::Runner do
20
20
  let(:runner) {
21
21
  Class.new(described_class) do
22
22
  def steps
23
- [ [:step1, proc { |x| x + 35 }],
23
+ [ [:step1, proc { |x| x + 35 }],
24
24
  [:step2, proc { |x, env| "#{env[:foo]}[#{x} #{x}]" }] ]
25
25
  end
26
26
  end.new(object: object, config: config, options: options)
@@ -337,7 +337,7 @@ RSpec.describe Yaks::Runner do
337
337
  }
338
338
  }
339
339
 
340
- let(:object) { "foo" }
340
+ let(:object) { "foo" }
341
341
 
342
342
  it 'should only run the mapper' do
343
343
  expect(runner.map).to eql "mapped[foo]"
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.11.0
4
+ version: 0.12.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-07-09 00:00:00.000000000 Z
11
+ date: 2016-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: abstract_type
@@ -465,7 +465,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
465
465
  version: '0'
466
466
  requirements: []
467
467
  rubyforge_project:
468
- rubygems_version: 2.2.3
468
+ rubygems_version: 2.5.1
469
469
  signing_key:
470
470
  specification_version: 4
471
471
  summary: Serialize to hypermedia. HAL, JSON-API, etc.