Permalink
Browse files

fixed root declaration via options

  • Loading branch information...
1 parent f3b79fb commit f9aec53aa807021b288d1de8035f7987dedc54b3 @mkristian committed Dec 24, 2012
View
9 lib/ixtlan/babel/deserializer.rb
@@ -34,20 +34,21 @@ def use(context_or_options)
end
def from_array_hash( data )
- if self.class.config.root
- data.collect{ |d| @model_class.new( filter.filter( d[ self.class.filter.root ] ) ) }
+ if filter.options[:root]
+ data.collect{ |d| @model_class.new( filter.filter( d[ filter.options[:root] ] ) ) }
else
data.collect{ |d| @model_class.new( filter.filter( d ) ) }
end
end
private :from_array_hash
def from_hash(data, options = nil)
- filter.options = options if options
+ filter.options = options || {}
+ filter.options[:root] ||= self.class.config.root
if data.is_a? Array
from_array_hash( data )
else
- data = data[ self.class.config.root ] if self.class.config.root
+ data = data[ filter.options[:root] ] if filter.options[:root]
@model_class.new( filter.filter( data ) )
end
end
View
4 spec/hash_filter_spec.rb
@@ -43,8 +43,8 @@ def self.new(hash = nil, &block)
end
it 'should serialize and deserialize a hash with root' do
- json = serializer.to_json :root => :my
- result = deserializer.from_json(json, :root => :my)
+ json = serializer.to_json :root => 'my'
+ result = deserializer.from_json(json, :root => 'my')
result.must_equal Hash['id' => data['id'], 'name' => data['name']]
end
View
4 spec/model_filter_spec.rb
@@ -54,8 +54,8 @@ class Person
end
it 'should serialize and deserialize with root' do
- json = serializer.to_json :root => :my
- result = deserializer.from_json(json, :root => :my)
+ json = serializer.to_json :root => 'my'
+ result = deserializer.from_json(json, :root => 'my')
attributes = result.attributes.delete_if { |k,v| v.nil? }
attributes.must_equal Hash[:id => person['id'], :name => person['name']]
end
View
4 spec/model_filter_with_methods_spec.rb
@@ -78,8 +78,8 @@ def children_ages
end
it 'should serialize and deserialize with root' do
- json = serializer.to_json :root => :my
- result = deserializer.from_json(json, :root => :my)
+ json = serializer.to_json :root => 'my'
+ result = deserializer.from_json(json, :root => 'my')
attributes = result.attributes.delete_if { |k,v| v.nil? }
attributes.must_equal Hash[:id => person['id'], :name => person['name']]
end

0 comments on commit f9aec53

Please sign in to comment.