Skip to content

Fix coercion for embedded shallow attributes type, receiving unexpected parameters #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/shallow_attributes/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ module ClassMethods
#
def inherited(subclass)
super
if respond_to?(:descriptions)
subclass.descriptions.merge!(descriptions)
end

if respond_to?(:formats)
subclass.formats.merge!(formats)
end

if respond_to?(:default_values)
subclass.default_values.merge!(default_values)
end
Expand Down Expand Up @@ -41,6 +49,14 @@ def mandatory_attributes
@mandatory_attributes ||= {}
end

def descriptions
@descriptions ||= {}
end

def formats
@formats ||= {}
end

# Returns all class attributes.
#
# @example Create new User instance
Expand Down Expand Up @@ -88,6 +104,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)
Expand Down
25 changes: 21 additions & 4 deletions lib/shallow_attributes/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to define Boolean class for adding to the DEFAULT_TYPE_OBJECTS.


module ShallowAttributes
# Namespace for standard type classes
#
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/custom_types_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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