模块:Mongoid::Serializable

扩展方式:
ActiveSupport::Concern
包含在:
可组合
定义于:
lib/mongoid/serializable.rb

Overview

此模块提供在 JSON 和 XML 序列化中包含关联的额外行为。

实例方法摘要折叠

实例方法详细信息

#serializable_hash (options = nil) ⇒哈希

以可序列化哈希的形式获取文档,由 ActiveModel 的 JSON 序列化器使用。

例子:

获取可序列化哈希值。

document.serializable_hash

使用选项获取可序列化哈希。

document.serializable_hash(:include => :addresses)

参数:

  • 选项 哈希 (默认为: nil

    要传递的选项。

选项哈希 ( options ):

  • :除了 ( Symbol | string | Array<Symbol | string > )

    请勿包含这些字段。

  • :include ( Symbol | string | Array<Symbol | string > )

    要包含哪些关联。

  • :only ( Symbol | string | Array<Symbol | string > )

    将字段限制为仅这些字段。

  • :methods ( Symbol | string | Array<Symbol | string > )

    要包含哪些方法。

返回:

  • (哈希)

    该文档已准备好进行序列化。



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mongoid/serializable.rb', line 47

def Serializable_hash(选项 = nil)
  选项 ||= {}
  attrs = {}

  名称 = field_names(选项)

  method_names = 阵列.换行(选项[:methods]).map do |名称|
    名称.to_s if respond_to?(名称)
  end.compact

  (名称 + method_names). do |名称|
    without_autobuild do
      Serialize_attribute(attrs, 名称, 名称, 选项)
    end
  end
  Serialize_relations(attrs, 选项) if 选项[:include]
  attrs
end