Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
694b5620
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
694b5620
编写于
6月 05, 2014
作者:
D
David Heinemeier Hansson
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #79 from cristianbica/deep-serialization
Deep serialization
上级
f4b42e54
a4a7f027
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
58 addition
and
5 deletion
+58
-5
lib/active_job/arguments.rb
lib/active_job/arguments.rb
+28
-4
test/cases/parameters_test.rb
test/cases/parameters_test.rb
+30
-1
未找到文件。
lib/active_job/arguments.rb
浏览文件 @
694b5620
...
...
@@ -3,14 +3,19 @@
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 @@ def self.serialize(arguments)
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
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
test/cases/parameters_test.rb
浏览文件 @
694b5620
require
'helper'
require
'active_job/arguments'
require
'models/person'
require
'active_support/core_ext/hash/indifferent_access'
class
ParameterSerializationTest
<
ActiveSupport
::
TestCase
test
'should make no change to regular values'
do
...
...
@@ -14,7 +15,7 @@ class ParameterSerializationTest < ActiveSupport::TestCase
assert_equal
[
'a'
],
ActiveJob
::
Arguments
.
serialize
([
'a'
])
assert_equal
[
true
],
ActiveJob
::
Arguments
.
serialize
([
true
])
assert_equal
[
false
],
ActiveJob
::
Arguments
.
serialize
([
false
])
assert_equal
[
{
a:
1
}
],
ActiveJob
::
Arguments
.
serialize
([
{
a:
1
}
])
assert_equal
[
{
"a"
=>
1
,
"b"
=>
2
}
],
ActiveJob
::
Arguments
.
serialize
([
{
a:
1
,
"b"
=>
2
}
])
assert_equal
[
[
1
]
],
ActiveJob
::
Arguments
.
serialize
([
[
1
]
])
assert_equal
[
1_000_000_000_000_000_000_000
],
ActiveJob
::
Arguments
.
serialize
([
1_000_000_000_000_000_000_000
])
...
...
@@ -24,6 +25,25 @@ class ParameterSerializationTest < ActiveSupport::TestCase
assert_equal
"Unsupported argument type:
#{
self
.
class
.
name
}
"
,
err
.
message
end
test
'should dive deep into arrays or hashes'
do
assert_equal
[
{
"a"
=>
Person
.
find
(
5
).
gid
}.
with_indifferent_access
],
ActiveJob
::
Arguments
.
serialize
([
{
a:
Person
.
find
(
5
)
}
])
assert_equal
[
[
Person
.
find
(
5
).
gid
]
],
ActiveJob
::
Arguments
.
serialize
([
[
Person
.
find
(
5
)
]
])
end
test
'should dive deep into arrays or hashes and raise exception on complex objects'
do
err
=
assert_raises
RuntimeError
do
ActiveJob
::
Arguments
.
serialize
([
1
,
[
self
]
])
end
assert_equal
"Unsupported argument type:
#{
self
.
class
.
name
}
"
,
err
.
message
end
test
'shoud dive deep into hashes and allow raise exception on not string/symbol keys'
do
err
=
assert_raises
RuntimeError
do
ActiveJob
::
Arguments
.
serialize
([
[
{
1
=>
2
}
]
])
end
assert_equal
"Unsupported hash key type: Fixnum"
,
err
.
message
end
test
'should serialize records with global id'
do
assert_equal
[
Person
.
find
(
5
).
gid
],
ActiveJob
::
Arguments
.
serialize
([
Person
.
find
(
5
)
])
end
...
...
@@ -45,4 +65,13 @@ class ParameterDeserializationTest < ActiveSupport::TestCase
test
'should serialize values and records together'
do
assert_equal
[
3
,
Person
.
find
(
5
)
],
ActiveJob
::
Arguments
.
deserialize
([
3
,
Person
.
find
(
5
).
gid
])
end
test
'should dive deep when deserialising arrays'
do
assert_equal
[
[
3
,
Person
.
find
(
5
)
]
],
ActiveJob
::
Arguments
.
deserialize
([
[
3
,
Person
.
find
(
5
).
gid
]
])
end
test
'should dive deep when deserialising hashes'
do
assert_equal
[
{
"5"
=>
Person
.
find
(
5
)
}
],
ActiveJob
::
Arguments
.
deserialize
([
{
"5"
=>
Person
.
find
(
5
).
gid
}
])
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录