blob: 553ab20f652b09726500c51dcdca3cac2031bd09 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
require 'active_model/global_locator'
require 'active_support/core_ext/object/try'
module ActiveJob
class Arguments
TYPE_WHITELIST = [ NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum ]
def self.serialize(arguments)
arguments.collect do |argument|
case argument
when ActiveModel::GlobalIdentification
argument.global_id
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
end
end
def self.deserialize(arguments)
arguments.collect do |argument|
case argument
when Array
deserialize(argument)
when Hash
Hash[argument.map{ |key, value| [ key, deserialize([value]).first ] }].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
|