模块:Mongoid::Document

扩展方式:
ActiveSupport::Concern
包括:
Composable , Touchable::InstanceMethods
包含在:
GlobalDiscriminatorKeyAssignment::InvalidFieldHost
定义于:
lib/mongoid/ 文档.rb

Overview

这是需要作为文档持久保存到数据库中的所有域对象的基本模块。

在命名空间下定义

模块: 类方法

常量摘要折叠

ILLEGAL_KEY =

用于匹配非法 BSON 密钥的正则表达式。 请注意,bson 4.1具有应改用的常量 BSON::String::ILLEGAL_KEY。 当 Ruby 驱动程序2.3.0发布并且可以更新 Mongoid 以要求 >= 2.3.0时,可以使用 BSON 常量。

/(\A[$])|(\.)/.冻结

可组合项中包含的常量

Composable::MODULESComposable::RESERVED_METHOD_NAMES

Interceptable中包含的常量

Interceptable::CALBACKS

关联中包含的常量

Association::MACRO_MAPPING

Association::Depending中包含的常量

Association::Depending::STRATEGIES

Persistable中包含的常量

Persistable::LIST_OPERATIONS

字段中包含的常量

Fields::Boolean Fields::IDS Fields::INVALID_BSON_CLASSES Fields::StringifiedSymbol Fields::TRANSLATIONS_SFX Fields::TYPE_MAPPINGS

Atomic中包含的常量

Atomic::UPDATES

实例属性摘要折叠

Stateful中包含的属性

#destroyed#flag_for_destroy#previously_new_record

关联中包含的属性

#_association

属性中包含的属性

#attributes

Clients::StorageOptions 中包含的属性

#remembered_storage_options

实例方法摘要折叠

Touchable::InstanceMethods中包含的方法

#_clear_touch_updates#_gather_touch_updates#_run_touch_callbacks_from_root#_touchable_parent?#suppress_touch_callbacks#touch#touch_callbacks_suppressed?

可组合项中包含的方法

Barceled_methods

Equality中包含的方法

#, #==, #eql?

Copyable中包含的方法

#clone

Interceptable中包含的方法

#_mongoid_run_child_after_callbacks#_mongoid_run_child_before_callbacks#_mongoid_run_child_callbacks#_mongoid_run_child_callbacks_with_round#_mongoid_run_child_callbacks_Without_round#callback_executable? , #in_callback_state?#pending_callbacks#pending_callbacks=#run_after_callbacks#run_before_callbacks#run_callbacks#run_pending_callbacks

Validatable 中包含的方法

#begin_validate #exit_validate #performing_validations? 、#read_attribute_for_validation、#valid? , #validated? 、#validating、#validating_with_query?

Traversable包含的方法

__redefine #_children #_descendants #_parent #_parent= #_reset_memoized_descendants! 、#_root 、#_root? #collect_children #collect_descendants #flag_descendants_persisted #hereditary? #parentize #remove_child #reset_persisted_descendants

Cacheable中包含的方法

#cache_key

Stateful中包含的方法

#destroyed? , #flag_for_destroy? , #new_record= , #new_record? , #persisted? , #previously_new_record? , #previously_persisted? , #pushable?#readonly! , #readonly? , #settable? , #updateable?

Shardable 中包含的方法

#shard_key_field_value#shard_key_fields#shard_key_selector#shard_key_selector_in_db

Serializable中包含的方法

#serializable_hash

Selectable中包含的方法

#atomic_selector

Reloadable中包含的方法

#reload

关联中包含的方法

#association_name#embedded? , #embedded_many? , #embedded_one? , #referenced_many? , #referenced_one? , #reload_relations

Association::Reflections中包含的方法

#reflect_on_all_association#reflect_on_association

Association::Macros中包含的方法

#associations

Association::Dependent中包含的方法

#apply_destroy_dependencies!define_dependency!验证!

Association::Accessors中包含的方法

#__build__#create_relation#reset_relation_criteria#set_relation

Association::Referenced::Syncable中包含的方法

#_syncable?#_synced#_synced?#remove_inverse_keys#update_inverse_keys

Association::Referenced::CounterCache中包含的方法

define_callbacks! , #reset_counters

Association::Referenced::AutoSave中包含的方法

#__autosaving__, #autosaved?, #changed_for_autosave?, define_autosave!

Persistable中包含的方法

#atomically#fail_due_to_callback! , #fail_due_to_validation!

Persistable::Unsettable 中包含的方法

#unset

Persistable::Upsertable 中包含的方法

#upsert

Persistable::Updatable中包含的方法

#update, #update!, #update_attribute

Persistable::Settable中包含的方法

#set

Persistable::Savable中包含的方法

#save, #save!

Persistable::Renameable 中包含的方法

#rename

Persistable::Pushable 中包含的方法

#add_to_set#push

Persistable::Pullable 中包含的方法

#pull , #pull_all

位置包含的方法

#positionally

Persistable::Poppable 中包含的方法

#pop

Persistable::Multipliable中包含的方法

#mul

Persistable::Minable 中包含的方法

#set_min

Persistable::Maxable中包含的方法

#set_max

Persistable::Logical中包含的方法

#bit

Persistable::Incrementable中包含的方法

#inc

Persistable::Destroyable中包含的方法

#destroy , #destroy!

Persistable::Deletable中包含的方法

# 删除

Persistable::Createtable中包含的方法

#insert

Matchable中包含的方法

#_matches?

字段中包含的方法

#apply_default#apply_defaults#apply_post_processed_defaults#apply_pre_processed_defaults#attribute_namesdatabase_field_name#database_field_name#dot_dollar_field? , #lazy_settable?optionoptionstraverse_association_tree#using_object_ids?#validate_writable_field_name!

包含在Evolvable中的方法

#__evolve_object_id__

属性中包含的方法

#allow_attributes 、#attribute_missing , #attribute_present?#attributes_before_type_cast#has_attribute? , #has_attribute_before_type_cast?#process_raw_attribute#read_attribute#read_attribute_before_type_cast#remove_attribute#typed_attributes#write_attribute#write_attributes

Attributes::Readonly中包含的方法

#attribute_writable?

Attributes::Processing中包含的方法

#process_attributes

客户端包含的方法

clear、clients、 默认 、disconnect、reconnect、 设立 、with_name

Clients::Sessions中包含的方法

包含

Clients::Options中包含的方法

# 集合 , #collection_name , #mongo_client , #persistence_context , #persistence_context? , #with

Clients::StorageOptions 中包含的方法

#remember_storage_options! , #storage_options

Changeable中包含的方法

#attribute_before_last_save#changed#changed?#changed_attributes#changes#children_changed?#children_may_have_changed!#move_changes#post_persist#previous_changes#remove_change#saved_change_to_attribute#saved_change_to_attribute?#setters#will_save_change_to_attribute?

Atomic中包含的方法

#add_atomic_pull , #add_atomic_unset , #atomic_array_add_to_sets , #atomic_array_pulls , #atomic_array_pushes , #atomic_attribute_name , #atomic_delete_modifier , #atomic_insert_modifier , #atomic_path , #atomic_paths , #atomic_position , #atomic_pulls , #atomic_pushes , #atomic_sets , #atomic_unsets , #atomic_updates , #delayed_atomic_pulls#delayed_atomic_sets#delayed_atomic_unsets#flag_as_destroyed#flag_destroys#process_flag_destroys

实例属性详细信息

# __selected_fields对象

返回属性 __selected_fields 的值。



30
31
32
# File 'lib/mongoid/ 文档.rb', 第30行

def __selected_fields
  @__selected_fields
end

# new_record对象(只读)

返回属性 new_record 的值。



31
32
33
# File 'lib/mongoid/ 文档.rb', 第31行

def new_record
  @new_record
end

实例方法详细信息

# _handle_callbacks_after_instantiation (execute_callbacks) ⇒对象

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

如果要执行回调,则处理回调的设置和执行;否则,将适当的回调添加到待处理的回调列表中。

参数:

  • execute_callbacks ( true | false )

    是否应执行回调。



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/mongoid/ 文档.rb', 第186行

def _handle_callbacks_after_instantiation(execute_callbacks)
  if execute_callbacks
    apply_defaults
    产量 self if block_given?
    run_callbacks(:find) 除非 _find_callbacks.空?
    run_callbacks(:initialize) 除非 _initialize_callbacks.空?
  else
    产量 self if block_given?
    self.pending_callbacks += %i[ apply_defaults find 初始化 ]
  end
end

# as_document哈希

返回此文档及以下文档中的整个文档层次结构的哈希值。 当所有内容而不仅仅是当前文档都需要这些属性时使用。

例子:

获取完整的层次结构。

person.as_document

返回:

  • (哈希)

    层次结构中所有属性的哈希值。



136
137
138
# File 'lib/mongoid/ 文档.rb', 第136行

def as_document
  BSON::文档.new(as_attributes)
end

#变为(klass) ⇒文档

返回指定类的实例,其中包含当前文档。

例子:

返回子类文档作为超类实例。

manager.becomes(Person)

参数:

  • klass ( class )

    要成为的类。

返回:

  • (文档)

    指定类的实例。

引发:

  • ( ArgumentError )

    如果类不包含 Mongoid::Document



151
152
153
154
155
156
157
158
# File 'lib/mongoid/ 文档.rb', 第151行

def 变为(klass)
  mongoid_document_check!(klass)

  成为 = klass.new(clone_document)
  成为.Internal_State = Internal_State

  成为
end

#freezeDocument

冻结文档的内部属性。

例子:

冻结文档

document.freeze

返回:



51
52
53
# File 'lib/mongoid/ 文档.rb', 第51行

def 冻结
  as_attributes.冻结  self
end

#frozen?true | false

检查文档是否已冻结

例子:

Check if frozen

document.frozen?

返回:

  • ( true | false )

    如果已冻结,则为 true,否则为 false。



61
62
63
# File 'lib/mongoid/ 文档.rb', 第61行

def 冻结?
  属性.冻结?
end

#哈希整数

委托身份,以允许具有相同身份的两个记录处理如下内容:

[ Person.find(1), Person.find(2), Person.find(3) ] &
[ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]

例子:

获取哈希值。

document.hash

返回:

  • ( Integer )

    文档身份的哈希值。



75
76
77
# File 'lib/mongoid/ 文档.rb', 第75行

def 哈希
  身份.哈希
end

#身份数组

文档由其类和数据库 ID 绝对标识:

Person.first.identity #=> [Person, BSON::ObjectId(' 4 f 775130 a 04745933 a 000003 ')]

例子:

获取身份

document.identity

返回:

  • ( Array )

    一个大量,其中包含 [文档.class, 文档。 _id]



87
88
89
# File 'lib/mongoid/ 文档.rb', 第87行

def 身份
  [ self.class, _id ]
end

#initialize (attrs = nil, &block) ⇒文档

实例化一个新的Document ,设置文档的属性(如果给定)。 如果未提供任何属性,则会使用空的Hash进行初始化。

如果定义了主键,则文档的ID将设置为主键,否则将设置为新的 BSON::ObjectId string 。

例子:

创建一个新文档。

Person.new(:title => 'Sir')

参数:

  • attrs 哈希 (默认为: nil

    用于设立文档的属性。

返回:

  • (文档)

    一个新文档。



104
105
106
# File 'lib/mongoid/ 文档.rb', 第104行

def 初始化(attrs = nil, )
  construct_document(attrs, )
end

# internal_state= (state) ⇒对象

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

设置此文档的内部状态。 仅由 #becomes 用于帮助初始化重新键入的文档。

参数:

  • (哈希)

    内部状态值的映射。



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/mongoid/ 文档.rb', 第166行

def Internal_state=()
  self._id = [:id]
  @changed_attributes = [:changed_attributes]
  @errors = ActiveModel::Errors.new(self).点击 { |e| e.复制!([:errors]) }
  @new_record = [:new_record]
  @destroyed = [:destroyed]

  update_discriminator([:discriminator_key_was])

  mark_persisted_state_for_embedded_documents([:new_record])
end

# model_namestring

返回文档的模型名称。

例子:

返回模型名称。

document.model_name

返回:

  • ( string )

    模型名称。



114
115
116
# File 'lib/mongoid/ 文档.rb', 第114行

def model_name
  self.class.model_name
end

# to_keystring

返回文档的键值。

例子:

返回密钥。

document.to_key

返回:

  • ( string )

    文档的ID ;如果是新文档,则为 nil。



124
125
126
# File 'lib/mongoid/ 文档.rb', 第124行

def to_key
  (持续存在? || 已销毁?) ? [ _id.to_s ] : nil
end