From cce97d2d9a7e45b95a7ea4aaefaa325f5fe4172d Mon Sep 17 00:00:00 2001 From: Ilya Sabanin Date: Thu, 2 Apr 2009 00:58:50 +0800 Subject: [PATCH] Support for nested arrays loading in ActiveResource. --- activeresource/lib/active_resource/base.rb | 34 +++++++++++++++++++--------- activeresource/test/base/load_test.rb | 8 ++++++ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 6cb5beb..375360f 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -942,17 +942,7 @@ module ActiveResource raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash) @prefix_options, attributes = split_options(attributes) attributes.each do |key, value| - @attributes[key.to_s] = - case value - when Array - resource = find_or_create_resource_for_collection(key) - value.map { |attrs| attrs.is_a?(String) ? attrs.dup : resource.new(attrs) } - when Hash - resource = find_or_create_resource_for(key) - resource.new(value) - else - value.dup rescue value - end + @attributes[key.to_s] = typecast_attribute_value(key, value) end self end @@ -1018,6 +1008,7 @@ module ActiveResource end private + # Tries to find a resource for a given collection name; if it fails, then the resource is created def find_or_create_resource_for_collection(name) find_or_create_resource_for(name.to_s.singularize) @@ -1061,6 +1052,27 @@ module ActiveResource self.class.__send__(:split_options, options) end + def typecast_attribute_value(attribute, value) + case value + when Array + resource = find_or_create_resource_for_collection(attribute) + value.map do |attrs| + if attrs.is_a?(String) + attrs.dup + elsif attrs.is_a?(Array) + typecast_attribute_value(attribute, attrs) + else + resource.new(attrs) + end + end + when Hash + resource = find_or_create_resource_for(attribute) + resource.new(value) + else + value.dup rescue value + end + end + def method_missing(method_symbol, *arguments) #:nodoc: method_name = method_symbol.to_s diff --git a/activeresource/test/base/load_test.rb b/activeresource/test/base/load_test.rb index a475fab..6fae32a 100644 --- a/activeresource/test/base/load_test.rb +++ b/activeresource/test/base/load_test.rb @@ -50,9 +50,17 @@ class BaseLoadTest < Test::Unit::TestCase :notable_rivers => [ { :id => 1, :name => 'Willamette' }, { :id => 2, :name => 'Columbia', :rafted_by => @matz }] }}} + + @nested_array = { + :nested_stuff => [['apple', 'green'], ['grape', 'blue'], ['gorilla', 'mad']] + } @person = Person.new end + + def test_load_nested_array + assert_equal @nested_array[:nested_stuff], @person.load(@nested_array).nested_stuff + end def test_load_expects_hash assert_raise(ArgumentError) { @person.load nil } -- 1.5.5