模块:Mongoid::Attributes::Dynamic

扩展方式:
ActiveSupport::Concern
定义于:
lib/mongoid/attributes/dynamic.rb

Overview

此模块包含动态属性的行为。

实例方法摘要折叠

动态方法处理

此类通过method_missing方法处理动态方法

# method_missing (name, *args) ⇒对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

用于支持动态属性的访问器方法。

例子:

通过method_missing 调用。

document.method_missing(:test)

参数:

  • 名称 ( string | Symbol )

    方法的名称。

  • *args ( Object... )

    该方法的参数。

返回:

  • ( Object )

    方法调用的结果。



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mongoid/attributes/dynamic.rb', line 122

def method_missing(名称, *args)
  attr = 名称.to_s
  return  除非 属性.has_key?(attr.Reader)
  if attr.writer?
    getter = attr.Reader
    define_dynamic_writer(getter)
    write_attribute(getter, args.first)
  elsif attr.before_type_cast?
    define_dynamic_before_type_cast_reader(attr.Reader)
    attribute_will_change!(attr.Reader)
    read_attribute_before_type_cast(attr.Reader)
  else
    getter = attr.Reader
    define_dynamic_reader(getter)
    attribute_will_change!(attr.Reader)
    read_raw_attribute(getter)
  end
end

实例方法详细信息

#define_dynamic_before_type_cast_reader (name) ⇒对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

在类型转换之前为动态属性定义读取器方法。

例子:

为属性定义读取器方法。

model.define_dynamic_before_type_cast_reader(:field)

参数:

  • 名称 ( string )

    字段的名称。



54
55
56
57
58
59
60
61
# File 'lib/mongoid/attributes/dynamic.rb', line 54

def define_dynamic_before_type_cast_reader(名称)
  class_eval do
    define_method(" #{ name } _before_type_cast ") do
      attribute_will_change!(名称)
      read_attribute_before_type_cast(名称)
    end
  end
end

#define_dynamic_reader (name) ⇒对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

为动态属性定义读取器方法。

例子:

定义读取器方法。

model.define_dynamic_reader(:field)

参数:

  • 名称 ( string )

    字段的名称。



35
36
37
38
39
40
41
42
43
44
# File 'lib/mongoid/attributes/dynamic.rb', line 35

def define_dynamic_reader(名称)
  return 除非 名称.valid_method_name?

  class_eval do
    define_method(名称) do
      attribute_will_change!(名称)
      read_raw_attribute(名称)
    end
  end
end

#define_dynamic_writer (name) ⇒对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

为动态属性定义编写器方法。

例子:

定义一个 writer 方法。

model.define_dynamic_writer(:field)

参数:

  • 名称 ( string )

    字段的名称。



71
72
73
74
75
76
77
78
79
# File 'lib/mongoid/attributes/dynamic.rb', line 71

def define_dynamic_writer(名称)
  return 除非 名称.valid_method_name?

  class_eval do
    define_method(" #{ name } = ") do ||
      write_attribute(名称, )
    end
  end
end

# Inspect_Dynamic_fieldsstring

获取文档的已检查动态字段大量。

例子:

检查动态字段。

document.inspect_dynamic_fields

返回:

  • ( string )

    一大量美观打印的动态字段值。



104
105
106
107
108
109
# File 'lib/mongoid/attributes/dynamic.rb', line 104

def Inspect_Dynamic_Fields
  密钥 = 属性.密钥 - 字段.密钥 - 关系.密钥 - [" _id ", self.class.discriminator_key]
  return 密钥.map do |名称|
    " #{ name}: #{ properties[ name].inspect} "
  end
end

# process_attribute (name, value) ⇒对象

如果属性是动态的,请为其添加一个对象类型的字段并设置值。

例子:

处理属性。

document.process_attribute(name, value)

参数:

  • 名称 (符号)

    字段的名称。

  • ( Object )

    字段的值。



89
90
91
92
93
94
95
96
# File 'lib/mongoid/attributes/dynamic.rb', line 89

def process_attribute(名称, )
  响应 = respond_to?(" #{ name } = ")
  if !响应
    write_attribute(名称, )
  else
    发送(" #{ name } = ", )
  end
end

# respond_to? (name, include_private = false) ⇒ true | false

覆盖 respond_to? 以便正确响应动态属性。

例子:

此对象是否响应该方法?

person.respond_to?(:title)

参数:

  • 名称 ( Array )

    方法的名称。

  • include_private ( true | false ) (默认为: false

返回:

  • ( true | false )

    如果是,则为 True,否则为 False。



20
21
22
23
24
25
# File 'lib/mongoid/attributes/dynamic.rb', line 20

def respond_to?(名称, include_private = false)
   || (
    属性 &&
    属性.has_key?(名称.to_s.Reader)
  )
end