Permalink
Browse files

allow to have custom serializer on array elements

  • Loading branch information...
1 parent d6e2967 commit 68d063fc810874e8ac42658ea3f009a1563dfb99 @mkristian committed Feb 19, 2013
Showing with 20 additions and 5 deletions.
  1. +2 −2 lib/ixtlan/babel/hash_filter.rb
  2. +2 −2 lib/ixtlan/babel/model_filter.rb
  3. +7 −0 spec/hash_filter_spec.rb
  4. +9 −1 spec/model_filter_spec.rb
View
4 lib/ixtlan/babel/hash_filter.rb
@@ -37,7 +37,7 @@ def filter_array( array, options )
if item.is_a?( Array ) || item.is_a?( Hash )
filter_data( item, options )
else
- item
+ serialize( item )
end
end
end
@@ -59,4 +59,4 @@ def filter_data( data, context )
end
end
end
-end
+end
View
4 lib/ixtlan/babel/model_filter.rb
@@ -39,7 +39,7 @@ def filter_array( models, options, &block )
if i.respond_to? :attributes
filter_data(i, block.call(i), options, &block)
else
- i
+ serialize( i )
end
end
end
@@ -70,4 +70,4 @@ def filter_data(model, data, context, &block)
end
end
end
-end
+end
View
7 spec/hash_filter_spec.rb
@@ -97,4 +97,11 @@ def self.new(hash = nil, &block)
result = deserializer.from_json(json, :include => { 'address' => {}, 'phone_numbers' => { :include => ['area']}})
result.must_equal data
end
+
+ it 'should convert elements from arrays wth custom serializer' do
+ serializer.add_custom_serializers( "Symbol" => Proc.new {|v| v.to_s.capitalize } )
+ data['children_names'] = [:adi, :aromal, :shreedev]
+ d = serializer.to_hash(:include => [ :children_names ])
+ d[ "children_names"].must_equal( ["Adi", "Aromal", "Shreedev"] )
+ end
end
View
10 spec/model_filter_spec.rb
@@ -28,6 +28,7 @@ class Person
attribute :name, String
attribute :address, Address
attribute :phone_numbers, Array[PhoneNumber]
+ attribute :children_names, Array[Symbol]
end
describe Ixtlan::Babel::ModelFilter do
@@ -40,7 +41,8 @@ class Person
:prefix => 12,
:number => '123',
:area => Area.new( :code => '001', :iso => 'us' )
- )]
+ )],
+ :children_names => [:adi, :aromal, :shreedev]
)
end
@@ -199,4 +201,10 @@ class Person
result.name.must_equal person.name
result.id.must_equal person.id
end
+
+ it 'should convert elements from arrays wth custom serializer' do
+ serializer.add_custom_serializers( "Symbol" => Proc.new {|v| v.to_s.capitalize } )
+ data = serializer.to_hash(:include => [ :children_names ])
+ data[ "children_names"].must_equal( ["Adi", "Aromal", "Shreedev"] )
+ end
end

0 comments on commit 68d063f

Please sign in to comment.