From 6008302c194fd92d379863080f2105e3510c905c Mon Sep 17 00:00:00 2001 From: Andrey Sidorov Date: Mon, 8 Apr 2019 16:14:09 +0300 Subject: [PATCH 1/4] fix coercion of ShallowAttributes values --- lib/shallow_attributes/instance_methods.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/shallow_attributes/instance_methods.rb b/lib/shallow_attributes/instance_methods.rb index 22959a9..f125eda 100644 --- a/lib/shallow_attributes/instance_methods.rb +++ b/lib/shallow_attributes/instance_methods.rb @@ -139,7 +139,13 @@ def reset_attribute(attribute) # # @since 0.1.0 def coerce(value, _options = {}) - self.attributes = value + self.attributes = + if value.class.included_modules.include?(ShallowAttributes) + value.to_h + else + value + end + self end From 8e66b0f30bab2775941a6b2bce6b4a3cf798e8b5 Mon Sep 17 00:00:00 2001 From: Andrey Sidorov Date: Mon, 8 Apr 2019 17:48:56 +0300 Subject: [PATCH 2/4] bugfix --- lib/shallow_attributes/type/string.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/shallow_attributes/type/string.rb b/lib/shallow_attributes/type/string.rb index 0176958..5237115 100644 --- a/lib/shallow_attributes/type/string.rb +++ b/lib/shallow_attributes/type/string.rb @@ -20,7 +20,7 @@ class String # @return [Sting] # # @since 0.1.0 - def coerce(value, _options = {}) + def coerce(value, options = {}) case value when nil then options[:allow_nil] ? nil : '' when ::Array then value.join From 9f0b031fd6e21f9603ad4413355e6bf2d098f7ce Mon Sep 17 00:00:00 2001 From: Andrey Sidorov Date: Tue, 9 Apr 2019 17:08:01 +0300 Subject: [PATCH 3/4] add test for array of ShallowAttributes objects --- test/custom_types_test.rb | 62 ++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/test/custom_types_test.rb b/test/custom_types_test.rb index b0955bf..6eb3d22 100644 --- a/test/custom_types_test.rb +++ b/test/custom_types_test.rb @@ -58,23 +58,22 @@ class Person end describe 'when one of attribute is array' do - let(:person) do - Person.new( - addresses: [ - { - street: 'Street 1/2', - city: { - name: 'NYC' - } - }, - { - street: 'Street 3/2', - city: { - name: 'Moscow' - } - } - ] - ) + let(:person) { Person.new addresses: [address_1, address_2] } + let(:address_1) do + { + street: 'Street 1/2', + city: { + name: 'NYC' + } + } + end + let(:address_2) do + { + street: 'Street 3/2', + city: { + name: 'Moscow' + } + } end it 'allows embedded values' do @@ -105,6 +104,35 @@ class Person person.addresses[0].must_be_instance_of Address end + describe 'array of ShallowAttributes objects' do + let(:address_1) do + Address.new( + street: 'Street 1/2', + city: { + name: 'NYC' + } + ) + end + let(:address_2) do + Address.new( + street: 'Street 3/2', + city: { + name: 'Moscow' + } + ) + end + + it 'allows embedded ShallowAttributes objects' do + person.addresses.count.must_equal 2 + + person.addresses[0].street.must_equal 'Street 1/2' + person.addresses[0].city.name.must_equal 'NYC' + + person.addresses[1].street.must_equal 'Street 3/2' + person.addresses[1].city.name.must_equal 'Moscow' + end + end + describe '#attributes' do it 'returns attributes hash' do hash = person.attributes From 3f6431e99579ddec87547c56acd3a32fe1107dd7 Mon Sep 17 00:00:00 2001 From: Andrey Sidorov Date: Mon, 20 Jul 2020 16:46:17 +0300 Subject: [PATCH 4/4] remove bundler restriction --- shallow_attributes.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shallow_attributes.gemspec b/shallow_attributes.gemspec index 5029fd8..91cc8c5 100644 --- a/shallow_attributes.gemspec +++ b/shallow_attributes.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - spec.add_development_dependency "bundler", "~> 1.11" + spec.add_development_dependency "bundler", "> 1.11" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "minitest", "~> 5.0" spec.add_development_dependency "dry-types", "~> 0.11"