Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
d2d98d69
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,体验更适合开发者的 AI 搜索 >>
提交
d2d98d69
编写于
2月 09, 2018
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Allow serializers to be used either as classes or objects
上级
b098584f
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
110 addition
and
124 deletion
+110
-124
activejob/lib/active_job/serializers/array_serializer.rb
activejob/lib/active_job/serializers/array_serializer.rb
+9
-11
activejob/lib/active_job/serializers/base_serializer.rb
activejob/lib/active_job/serializers/base_serializer.rb
+18
-14
activejob/lib/active_job/serializers/duration_serializer.rb
activejob/lib/active_job/serializers/duration_serializer.rb
+10
-12
activejob/lib/active_job/serializers/global_id_serializer.rb
activejob/lib/active_job/serializers/global_id_serializer.rb
+14
-16
activejob/lib/active_job/serializers/hash_serializer.rb
activejob/lib/active_job/serializers/hash_serializer.rb
+16
-18
activejob/lib/active_job/serializers/hash_with_indifferent_access_serializer.rb
...ob/serializers/hash_with_indifferent_access_serializer.rb
+12
-14
activejob/lib/active_job/serializers/object_serializer.rb
activejob/lib/active_job/serializers/object_serializer.rb
+5
-7
activejob/lib/active_job/serializers/standard_type_serializer.rb
...ob/lib/active_job/serializers/standard_type_serializer.rb
+11
-13
activejob/lib/active_job/serializers/symbol_serializer.rb
activejob/lib/active_job/serializers/symbol_serializer.rb
+8
-10
activejob/test/cases/serializers_test.rb
activejob/test/cases/serializers_test.rb
+7
-9
未找到文件。
activejob/lib/active_job/serializers/array_serializer.rb
浏览文件 @
d2d98d69
...
...
@@ -3,24 +3,22 @@
module
ActiveJob
module
Serializers
# Provides methods to serialize and deserialize `Array`
class
ArraySerializer
<
BaseSerializer
class
<<
self
alias_method
:deserialize?
,
:serialize?
class
ArraySerializer
<
BaseSerializer
# :nodoc:
alias_method
:deserialize?
,
:serialize?
def
serialize
(
array
)
array
.
map
{
|
arg
|
Serializers
.
serialize
(
arg
)
}
end
def
serialize
(
array
)
array
.
map
{
|
arg
|
Serializers
.
serialize
(
arg
)
}
end
def
deserialize
(
array
)
array
.
map
{
|
arg
|
Serializers
.
deserialize
(
arg
)
}
end
def
deserialize
(
array
)
array
.
map
{
|
arg
|
Serializers
.
deserialize
(
arg
)
}
end
private
private
def
klass
Array
end
end
end
end
end
activejob/lib/active_job/serializers/base_serializer.rb
浏览文件 @
d2d98d69
...
...
@@ -3,29 +3,33 @@
module
ActiveJob
module
Serializers
class
BaseSerializer
include
Singleton
class
<<
self
def
serialize?
(
argument
)
argument
.
is_a?
(
klass
)
end
delegate
:serialize?
,
:deserialize?
,
:serialize
,
:deserialize
,
to: :instance
end
def
deserialize?
(
_
argument
)
raise
NotImplementedError
end
def
serialize?
(
argument
)
argument
.
is_a?
(
klass
)
end
def
serialize
(
_argument
)
raise
NotImplementedError
end
def
deserialize?
(
_argument
)
raise
NotImplementedError
end
def
de
serialize
(
_argument
)
raise
NotImplementedError
end
def
serialize
(
_argument
)
raise
NotImplementedError
end
private
def
deserialize
(
_argument
)
raise
NotImplementedError
end
protected
def
klass
raise
NotImplementedError
end
end
end
end
end
activejob/lib/active_job/serializers/duration_serializer.rb
浏览文件 @
d2d98d69
module
ActiveJob
module
Serializers
class
DurationSerializer
<
ObjectSerializer
class
<<
self
def
serialize
(
duration
)
super
(
"value"
=>
duration
.
value
,
"parts"
=>
Serializers
.
serialize
(
duration
.
parts
))
end
class
DurationSerializer
<
ObjectSerializer
# :nodoc:
def
serialize
(
duration
)
super
(
"value"
=>
duration
.
value
,
"parts"
=>
Serializers
.
serialize
(
duration
.
parts
))
end
def
deserialize
(
hash
)
value
=
hash
[
"value"
]
parts
=
Serializers
.
deserialize
(
hash
[
"parts"
])
def
deserialize
(
hash
)
value
=
hash
[
"value"
]
parts
=
Serializers
.
deserialize
(
hash
[
"parts"
])
klass
.
new
(
value
,
parts
)
end
klass
.
new
(
value
,
parts
)
end
private
private
def
klass
ActiveSupport
::
Duration
end
end
end
end
end
activejob/lib/active_job/serializers/global_id_serializer.rb
浏览文件 @
d2d98d69
...
...
@@ -4,29 +4,27 @@ module ActiveJob
module
Serializers
# Provides methods to serialize and deserialize objects which mixes `GlobalID::Identification`,
# including `ActiveRecord::Base` models
class
GlobalIDSerializer
<
BaseSerializer
class
<<
self
def
serialize
(
object
)
{
GLOBALID_KEY
=>
object
.
to_global_id
.
to_s
}
rescue
URI
::
GID
::
MissingModelIdError
raise
SerializationError
,
"Unable to serialize
#{
object
.
class
}
"
\
"without an id. (Maybe you forgot to call save?)"
end
class
GlobalIDSerializer
<
BaseSerializer
# :nodoc:
def
serialize
(
object
)
{
GLOBALID_KEY
=>
object
.
to_global_id
.
to_s
}
rescue
URI
::
GID
::
MissingModelIdError
raise
SerializationError
,
"Unable to serialize
#{
object
.
class
}
"
\
"without an id. (Maybe you forgot to call save?)"
end
def
deserialize
(
hash
)
GlobalID
::
Locator
.
locate
(
hash
[
GLOBALID_KEY
])
end
def
deserialize
(
hash
)
GlobalID
::
Locator
.
locate
(
hash
[
GLOBALID_KEY
])
end
def
deserialize?
(
argument
)
argument
.
is_a?
(
Hash
)
&&
argument
[
GLOBALID_KEY
]
end
def
deserialize?
(
argument
)
argument
.
is_a?
(
Hash
)
&&
argument
[
GLOBALID_KEY
]
end
private
private
def
klass
GlobalID
::
Identification
end
end
end
end
end
activejob/lib/active_job/serializers/hash_serializer.rb
浏览文件 @
d2d98d69
...
...
@@ -4,26 +4,25 @@ module ActiveJob
module
Serializers
# Provides methods to serialize and deserialize `Hash` (`{key: field, ...}`)
# Only `String` or `Symbol` can be used as a key. Values will be serialized by known serializers
class
HashSerializer
<
BaseSerializer
class
<<
self
def
serialize
(
hash
)
symbol_keys
=
hash
.
each_key
.
grep
(
Symbol
).
map
(
&
:to_s
)
result
=
serialize_hash
(
hash
)
result
[
key
]
=
symbol_keys
result
end
class
HashSerializer
<
BaseSerializer
# :nodoc:
def
serialize
(
hash
)
symbol_keys
=
hash
.
each_key
.
grep
(
Symbol
).
map
(
&
:to_s
)
result
=
serialize_hash
(
hash
)
result
[
key
]
=
symbol_keys
result
end
def
deserialize?
(
argument
)
argument
.
is_a?
(
Hash
)
&&
argument
[
key
]
end
def
deserialize?
(
argument
)
argument
.
is_a?
(
Hash
)
&&
argument
[
key
]
end
def
deserialize
(
hash
)
result
=
hash
.
transform_values
{
|
v
|
Serializers
::
deserialize
(
v
)
}
symbol_keys
=
result
.
delete
(
key
)
transform_symbol_keys
(
result
,
symbol_keys
)
end
def
deserialize
(
hash
)
result
=
hash
.
transform_values
{
|
v
|
Serializers
::
deserialize
(
v
)
}
symbol_keys
=
result
.
delete
(
key
)
transform_symbol_keys
(
result
,
symbol_keys
)
end
private
private
def
key
SYMBOL_KEYS_KEY
...
...
@@ -56,7 +55,6 @@ def transform_symbol_keys(hash, symbol_keys)
def
klass
Hash
end
end
end
end
end
activejob/lib/active_job/serializers/hash_with_indifferent_access_serializer.rb
浏览文件 @
d2d98d69
...
...
@@ -4,21 +4,20 @@ module ActiveJob
module
Serializers
# Provides methods to serialize and deserialize `ActiveSupport::HashWithIndifferentAccess`
# Values will be serialized by known serializers
class
HashWithIndifferentAccessSerializer
<
HashSerializer
class
<<
self
def
serialize
(
hash
)
result
=
serialize_hash
(
hash
)
result
[
key
]
=
Serializers
.
serialize
(
true
)
result
end
class
HashWithIndifferentAccessSerializer
<
HashSerializer
# :nodoc:
def
serialize
(
hash
)
result
=
serialize_hash
(
hash
)
result
[
key
]
=
Serializers
.
serialize
(
true
)
result
end
def
deserialize
(
hash
)
result
=
hash
.
transform_values
{
|
v
|
Serializers
.
deserialize
(
v
)
}
result
.
delete
(
key
)
result
.
with_indifferent_access
end
def
deserialize
(
hash
)
result
=
hash
.
transform_values
{
|
v
|
Serializers
.
deserialize
(
v
)
}
result
.
delete
(
key
)
result
.
with_indifferent_access
end
private
private
def
key
WITH_INDIFFERENT_ACCESS_KEY
...
...
@@ -27,7 +26,6 @@ def key
def
klass
ActiveSupport
::
HashWithIndifferentAccess
end
end
end
end
end
activejob/lib/active_job/serializers/object_serializer.rb
浏览文件 @
d2d98d69
...
...
@@ -3,14 +3,12 @@
module
ActiveJob
module
Serializers
class
ObjectSerializer
<
BaseSerializer
class
<<
self
def
serialize
(
hash
)
{
OBJECT_SERIALIZER_KEY
=>
self
.
name
}.
merge!
(
hash
)
end
def
serialize
(
hash
)
{
OBJECT_SERIALIZER_KEY
=>
self
.
class
.
name
}.
merge!
(
hash
)
end
def
deserialize?
(
argument
)
argument
.
is_a?
(
Hash
)
&&
argument
[
OBJECT_SERIALIZER_KEY
]
==
self
.
name
end
def
deserialize?
(
argument
)
argument
.
is_a?
(
Hash
)
&&
argument
[
OBJECT_SERIALIZER_KEY
]
==
self
.
class
.
name
end
end
end
...
...
activejob/lib/active_job/serializers/standard_type_serializer.rb
浏览文件 @
d2d98d69
...
...
@@ -4,22 +4,20 @@ module ActiveJob
module
Serializers
# Provides methods to serialize and deserialize standard types
# (`NilClass`, `String`, `Integer`, `Fixnum`, `Bignum`, `Float`, `BigDecimal`, `TrueClass`, `FalseClass`)
class
StandardTypeSerializer
<
BaseSerializer
class
<<
self
def
serialize?
(
argument
)
Arguments
::
TYPE_WHITELIST
.
include?
argument
.
class
end
class
StandardTypeSerializer
<
BaseSerializer
# :nodoc:
def
serialize?
(
argument
)
Arguments
::
TYPE_WHITELIST
.
include?
argument
.
class
end
def
serialize
(
argument
)
argument
end
def
serialize
(
argument
)
argument
end
alias_method
:deserialize?
,
:serialize?
alias_method
:deserialize?
,
:serialize?
def
deserialize
(
argument
)
object
=
GlobalID
::
Locator
.
locate
(
argument
)
if
argument
.
is_a?
String
object
||
argument
end
def
deserialize
(
argument
)
object
=
GlobalID
::
Locator
.
locate
(
argument
)
if
argument
.
is_a?
String
object
||
argument
end
end
end
...
...
activejob/lib/active_job/serializers/symbol_serializer.rb
浏览文件 @
d2d98d69
module
ActiveJob
module
Serializers
class
SymbolSerializer
<
ObjectSerializer
class
<<
self
def
serialize
(
argument
)
super
(
"value"
=>
argument
.
to_s
)
end
class
SymbolSerializer
<
ObjectSerializer
# :nodoc:
def
serialize
(
argument
)
super
(
"value"
=>
argument
.
to_s
)
end
def
deserialize
(
argument
)
argument
[
"value"
].
to_sym
end
def
deserialize
(
argument
)
argument
[
"value"
].
to_sym
end
private
private
def
klass
Symbol
end
end
end
end
end
activejob/test/cases/serializers_test.rb
浏览文件 @
d2d98d69
...
...
@@ -17,21 +17,19 @@ def ==(other)
end
class
DummySerializer
<
ActiveJob
::
Serializers
::
ObjectSerializer
class
<<
self
def
serialize
(
object
)
super
({
"value"
=>
object
.
value
})
end
def
serialize
(
object
)
super
({
"value"
=>
object
.
value
})
end
def
deserialize
(
hash
)
DummyValueObject
.
new
(
hash
[
"value"
])
end
def
deserialize
(
hash
)
DummyValueObject
.
new
(
hash
[
"value"
])
end
private
private
def
klass
DummyValueObject
end
end
end
setup
do
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录