From 734c76dff4a39c3948b0a0953fe4319f7a08af6f Mon Sep 17 00:00:00 2001 From: Matias Date: Fri, 24 Jan 2020 12:46:39 -0300 Subject: [PATCH 1/5] Add description and format to attributes --- lib/shallow_attributes/class_methods.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/shallow_attributes/class_methods.rb b/lib/shallow_attributes/class_methods.rb index 1353c66..67e4212 100644 --- a/lib/shallow_attributes/class_methods.rb +++ b/lib/shallow_attributes/class_methods.rb @@ -41,6 +41,14 @@ def mandatory_attributes @mandatory_attributes ||= {} end + def descriptions + @descriptions ||= {} + end + + def formats + @formats ||= {} + end + # Returns all class attributes. # # @example Create new User instance @@ -88,6 +96,8 @@ def attribute(name, type, options = {}) default_values[name] = options.delete(:default) mandatory_attributes[name] = options.delete(:present) + descriptions[name] = options.delete(:desc) + formats[name] = options.delete(:format) initialize_setter(name, type, options) initialize_getter(name) From e2c5812ffa39baab729bbee9c88a5a900f75b3ed Mon Sep 17 00:00:00 2001 From: Matias Date: Fri, 24 Jan 2020 13:02:33 -0300 Subject: [PATCH 2/5] add methods in inherited --- lib/shallow_attributes/class_methods.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/shallow_attributes/class_methods.rb b/lib/shallow_attributes/class_methods.rb index 67e4212..127a06e 100644 --- a/lib/shallow_attributes/class_methods.rb +++ b/lib/shallow_attributes/class_methods.rb @@ -17,6 +17,14 @@ def inherited(subclass) if respond_to?(:default_values) subclass.default_values.merge!(default_values) end + + if respond_to?(:descriptions) + subclass.default_values.merge!(default_values) + end + + if respond_to?(:formats) + subclass.default_values.merge!(default_values) + end end # Returns hash that contains default values for each attribute From 5b980d76ed7f8d3943ee793a54664d877b07302d Mon Sep 17 00:00:00 2001 From: Matias Date: Fri, 24 Jan 2020 13:04:40 -0300 Subject: [PATCH 3/5] fix defaults values names --- lib/shallow_attributes/class_methods.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/shallow_attributes/class_methods.rb b/lib/shallow_attributes/class_methods.rb index 127a06e..96fa7c7 100644 --- a/lib/shallow_attributes/class_methods.rb +++ b/lib/shallow_attributes/class_methods.rb @@ -19,11 +19,11 @@ def inherited(subclass) end if respond_to?(:descriptions) - subclass.default_values.merge!(default_values) + subclass.descriptions.merge!(descriptions) end if respond_to?(:formats) - subclass.default_values.merge!(default_values) + subclass.formats.merge!(formats) end end From 23fb6b2dc903187ed729dfef0ec1886be02baa35 Mon Sep 17 00:00:00 2001 From: Matias Date: Fri, 24 Jan 2020 13:07:45 -0300 Subject: [PATCH 4/5] fix defaults values names --- lib/shallow_attributes/class_methods.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/shallow_attributes/class_methods.rb b/lib/shallow_attributes/class_methods.rb index 96fa7c7..71333a2 100644 --- a/lib/shallow_attributes/class_methods.rb +++ b/lib/shallow_attributes/class_methods.rb @@ -14,10 +14,6 @@ module ClassMethods # def inherited(subclass) super - if respond_to?(:default_values) - subclass.default_values.merge!(default_values) - end - if respond_to?(:descriptions) subclass.descriptions.merge!(descriptions) end @@ -25,6 +21,10 @@ def inherited(subclass) if respond_to?(:formats) subclass.formats.merge!(formats) end + + if respond_to?(:default_values) + subclass.default_values.merge!(default_values) + end end # Returns hash that contains default values for each attribute From 80990d91671b2ca5005eca0a06090bc6411171ae Mon Sep 17 00:00:00 2001 From: Javier Seixas Date: Fri, 15 Nov 2019 08:46:03 -0300 Subject: [PATCH 5/5] Adds test for embedded shallow attributes with unexpected parameters --- lib/shallow_attributes/type.rb | 25 +++++++++++++++++++++---- test/custom_types_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/shallow_attributes/type.rb b/lib/shallow_attributes/type.rb index ef66fc1..b99b380 100644 --- a/lib/shallow_attributes/type.rb +++ b/lib/shallow_attributes/type.rb @@ -7,6 +7,13 @@ require 'shallow_attributes/type/time' require 'shallow_attributes/type/date' +# Boolean class for working with bool values +# +# @private +# +# @since 0.9.4 +class Boolean; end + module ShallowAttributes # Namespace for standard type classes # @@ -30,7 +37,8 @@ class InvalidValueError < TypeError ::Integer => ShallowAttributes::Type::Integer.new, ::String => ShallowAttributes::Type::String.new, ::Time => ShallowAttributes::Type::Time.new, - ::Date => ShallowAttributes::Type::Date.new + ::Date => ShallowAttributes::Type::Date.new, + ::Boolean => ShallowAttributes::Type::Boolean.new }.freeze class << self @@ -56,7 +64,10 @@ class << self # # @since 0.1.0 def coerce(type, value, options = {}) - type_instance(type).coerce(value, options) + instance = type_instance(type, value) + return instance.coerce(instance.attributes, options) if instance.respond_to? :attributes + + instance.coerce(value, options) end private @@ -78,8 +89,14 @@ def coerce(type, value, options = {}) # @return [Class] # # @since 0.1.0 - def type_instance(klass) - DEFAULT_TYPE_OBJECTS[klass] || ShallowAttributes::Type.const_get(klass.name).new + def type_instance(klass, value = {}) + return DEFAULT_TYPE_OBJECTS[klass] if DEFAULT_TYPE_OBJECTS[klass] + + instantiable = ShallowAttributes::Type.const_get(klass.name) + + return instantiable.new(value) if instantiable < ShallowAttributes + + instantiable.new end end end diff --git a/test/custom_types_test.rb b/test/custom_types_test.rb index b0955bf..a3473bf 100644 --- a/test/custom_types_test.rb +++ b/test/custom_types_test.rb @@ -152,5 +152,28 @@ class Person end end end + describe 'when custom type receives parameters not considered as attributes' do + let(:person) do + Person.new( + name: 'John', + address: { + street: 'Street', + number: '12' + } + ) + end + + it 'ignores the non existence parameter' do + hash = person.attributes + hash.must_equal({ + name: 'John', + addresses: [], + address: { + street: 'Street', + zipcode: '111111' + } + }) + end + end end end