aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorCristian Bica <cristian.bica@gmail.com>2014-05-31 17:31:20 +0300
committerCristian Bica <cristian.bica@gmail.com>2014-06-03 00:46:35 +0300
commitba3ae62a775182c663c6797e21618bb37de3aaba (patch)
tree0bf3394c6f601b40509efcb3fc01c506af9925bf /lib
parent8a3b137f95ad0ce459ce0eb9808850474da863d4 (diff)
downloadrails-ba3ae62a775182c663c6797e21618bb37de3aaba.tar.gz
rails-ba3ae62a775182c663c6797e21618bb37de3aaba.tar.bz2
rails-ba3ae62a775182c663c6797e21618bb37de3aaba.zip
Deep serialization
Diffstat (limited to 'lib')
-rw-r--r--lib/active_job/arguments.rb32
1 files changed, 28 insertions, 4 deletions
diff --git a/lib/active_job/arguments.rb b/lib/active_job/arguments.rb
index d1cc6d3177..6fe52397da 100644
--- a/lib/active_job/arguments.rb
+++ b/lib/active_job/arguments.rb
@@ -3,14 +3,19 @@ require 'active_support/core_ext/object/try'
module ActiveJob
class Arguments
- TYPE_WHITELIST = [ NilClass, Fixnum, Float, String, TrueClass, FalseClass, Hash, Array, Bignum ]
+ TYPE_WHITELIST = [ NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum ]
def self.serialize(arguments)
arguments.collect do |argument|
- if argument.respond_to?(:global_id)
+ case argument
+ when ActiveModel::GlobalIdentification
argument.global_id
- elsif TYPE_WHITELIST.include?(argument.class)
+ when *TYPE_WHITELIST
argument
+ when Hash
+ Hash[ argument.map{ |key, value| [ serialize_hash_key(key), serialize([value]).first ] } ]
+ when Array
+ serialize(argument)
else
raise "Unsupported argument type: #{argument.class.name}"
end
@@ -18,7 +23,26 @@ module ActiveJob
end
def self.deserialize(arguments)
- arguments.collect { |argument| ActiveModel::GlobalLocator.locate(argument) || argument }
+ arguments.collect do |argument|
+ case argument
+ when Array
+ deserialize(argument)
+ when Hash
+ argument.with_indifferent_access
+ else
+ ActiveModel::GlobalLocator.locate(argument) || argument
+ end
+ end
end
+
+ private
+ def self.serialize_hash_key(key)
+ case key
+ when String, Symbol
+ key.to_s
+ else
+ raise "Unsupported hash key type: #{key.class.name}"
+ end
+ end
end
end