serializable_hash(:only => [], :methods => [:foo]) should work

This commit is contained in:
John Firebaugh 2011-02-21 16:17:30 -08:00
parent 2f00854d22
commit da4e1faf6f
2 changed files with 14 additions and 7 deletions

@ -69,14 +69,11 @@ module Serialization
def serializable_hash(options = nil)
options ||= {}
only = Array.wrap(options[:only]).map(&:to_s)
except = Array.wrap(options[:except]).map(&:to_s)
attribute_names = attributes.keys.sort
if only.any?
attribute_names &= only
elsif except.any?
attribute_names -= except
if only = options[:only]
attribute_names &= Array.wrap(only).map(&:to_s)
elsif except = options[:except]
attribute_names -= Array.wrap(except).map(&:to_s)
end
method_names = Array.wrap(options[:methods]).map { |n| n if respond_to?(n.to_s) }.compact

@ -42,4 +42,14 @@ def test_method_serializable_hash_should_work_with_methods_option
assert_equal expected , @user.serializable_hash(:methods => [:foo])
end
def test_method_serializable_hash_should_work_with_only_and_methods
expected = {:foo=>"i_am_foo"}
assert_equal expected , @user.serializable_hash(:only => [], :methods => [:foo])
end
def test_method_serializable_hash_should_work_with_except_and_methods
expected = {"gender"=>"male", :foo=>"i_am_foo"}
assert_equal expected , @user.serializable_hash(:except => [:name, :email], :methods => [:foo])
end
end