Skip to content
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,10 @@ user.attributes

### IMPORTANT note about member coercions

ShallowAttributes performs coercions only when a value is being assigned. If you mutate the value
later on using its own interfaces then coercion won't be triggered.
ShallowAttributes guarantee coercions only when a value is being assigned. If you mutate the value
later on using its own interfaces then coercion may not be triggered.

But ShallowAttributes uses `MutableArray < Array` with redefined mutation methods for `Array`s.

Here's an example:

Expand All @@ -331,7 +333,7 @@ library = Library.new
# This will coerce Hash to a Book instance
library.books = [ { :title => 'Introduction' } ]

# This WILL NOT COERCE the value because you mutate the books array with Array#<<
# This WILL COERCE the value because ShallowAttributes uses `MutableArray < Array` with redefined `#<<`
library.books << { :title => 'Another Introduction' }
```

Expand Down
2 changes: 1 addition & 1 deletion lib/shallow_attributes/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def attributes
#
# @since 0.1.0
def attribute(name, type, options = {})
options[:default] ||= [] if type == Array
options[:default] ||= Type::MutableArray.new if type == Array

default_values[name] = options.delete(:default)
mandatory_attributes[name] = options.delete(:present)
Expand Down
1 change: 1 addition & 0 deletions lib/shallow_attributes/type.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'shallow_attributes/type/array'
require 'shallow_attributes/type/mutable_array'
require 'shallow_attributes/type/boolean'
require 'shallow_attributes/type/date_time'
require 'shallow_attributes/type/float'
Expand Down
11 changes: 1 addition & 10 deletions lib/shallow_attributes/type/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,7 @@ def coerce(values, options = {})
unless values.is_a? ::Array
raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{values}" for type "Array")
end
values.map! do |value|
ShallowAttributes::Type.coerce(item_klass(options[:of]), value)
end
end

private

def item_klass(klass)
return klass unless klass.is_a? ::String
Object.const_get(klass)
MutableArray.new(values, **options)
end
end
end
Expand Down
46 changes: 46 additions & 0 deletions lib/shallow_attributes/type/mutable_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module ShallowAttributes
module Type
# Class for mutable arrays.
#
# @abstract
#
# @since 0.9.1
class MutableArray < ::Array
def initialize(items = [], of: nil)
@of = of
super(
items.map { |item| ShallowAttributes::Type.coerce(item_class, item) }
)
end

def <<(item)
super ShallowAttributes::Type.coerce(item_class, item)
end

def push(*items)
super(
*items.map { |item| ShallowAttributes::Type.coerce(item_class, item) }
)
end

def concat(other)
super(
other.map { |item| ShallowAttributes::Type.coerce(item_class, item) }
)
end

def replace(other)
super(
other.map { |item| ShallowAttributes::Type.coerce(item_class, item) }
)
end

private

def item_class
return @of unless @of.is_a? ::String
Object.const_get(@of)
end
end
end
end
58 changes: 58 additions & 0 deletions test/custom_types_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,64 @@ class Person
person.addresses[0].must_be_instance_of Address
end

describe 'change array by embedded values' do
it 'works for #<<' do
person.addresses << { street: 'Street 4/2', city: { name: 'Berlin' } }

person.addresses.must_be_kind_of Array
person.addresses[2].must_be_instance_of Address

person.addresses[2].street.must_be_instance_of String
person.addresses[2].street.must_equal 'Street 4/2'

person.addresses[2].city.must_be_instance_of City
person.addresses[2].city.name.must_equal 'Berlin'
end

it 'works for #push' do
person.addresses.push street: 'Street 4/2', city: { name: 'Berlin' }

person.addresses.must_be_kind_of Array
person.addresses[2].must_be_instance_of Address

person.addresses[2].street.must_be_instance_of String
person.addresses[2].street.must_equal 'Street 4/2'

person.addresses[2].city.must_be_instance_of City
person.addresses[2].city.name.must_equal 'Berlin'
end

it 'works for #concat' do
person.addresses.concat [
{ street: 'Street 4/2', city: { name: 'Berlin' } }
]

person.addresses.must_be_kind_of Array
person.addresses[2].must_be_instance_of Address

person.addresses[2].street.must_be_instance_of String
person.addresses[2].street.must_equal 'Street 4/2'

person.addresses[2].city.must_be_instance_of City
person.addresses[2].city.name.must_equal 'Berlin'
end

it 'works for #replace' do
person.addresses.replace [
{ street: 'Street 4/2', city: { name: 'Berlin' } }
]

person.addresses.must_be_kind_of Array
person.addresses[0].must_be_instance_of Address

person.addresses[0].street.must_be_instance_of String
person.addresses[0].street.must_equal 'Street 4/2'

person.addresses[0].city.must_be_instance_of City
person.addresses[0].city.name.must_equal 'Berlin'
end
end

describe '#attributes' do
it 'returns attributes hash' do
hash = person.attributes
Expand Down