Module: Mongoid::Document::ClassMethods
- Defined in:
- lib/mongoid/document.rb
Instance Method Summary collapse
-
#_mongoid_clear_types ⇒ Object
private
Clear the @_type cache.
-
#_types ⇒ Array<Class>
Returns all types to query for when using this class as the base.
-
#construct_document(attrs = nil, options = {}) ⇒ Document
private
Allocates and constructs a document.
-
#i18n_scope ⇒ Symbol
Set the i18n scope to overwrite ActiveModel.
-
#instantiate(attrs = nil, selected_fields = nil, &block) ⇒ Document
Instantiate a new object, only when loaded from the database or when the attributes have already been typecast.
-
#instantiate_document(attrs = nil, selected_fields = nil, options = {}) ⇒ Document
private
Instantiate the document.
-
#logger ⇒ Logger
Returns the logger.
-
#with_callbacks(execute_callbacks) ⇒ Object
Indicate whether callbacks should be invoked by default or not, within the block.
Instance Method Details
#_mongoid_clear_types ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Clear the @_type cache. This is generally called when changing the discriminator key/value on a class.
408 409 410 411 |
# File 'lib/mongoid/document.rb', line 408 def _mongoid_clear_types @_type = nil superclass._mongoid_clear_types if hereditary? end |
#_types ⇒ Array<Class>
Returns all types to query for when using this class as the base.
397 398 399 |
# File 'lib/mongoid/document.rb', line 397 def _types @_type ||= (descendants + [ self ]).uniq.map(&:discriminator_value) end |
#construct_document(attrs = nil, options = {}) ⇒ Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A Ruby 2.x bug prevents the options hash from being keyword arguments. Once we drop support for Ruby 2.x, we can reimplement the options hash as keyword arguments. See bugs.ruby-lang.org/issues/15753
Allocates and constructs a document.
386 387 388 389 |
# File 'lib/mongoid/document.rb', line 386 def construct_document(attrs = nil, = {}) execute_callbacks = .fetch(:execute_callbacks, Threaded.execute_callbacks?) with_callbacks(execute_callbacks) { new(attrs) } end |
#i18n_scope ⇒ Symbol
Set the i18n scope to overwrite ActiveModel.
416 417 418 |
# File 'lib/mongoid/document.rb', line 416 def i18n_scope :mongoid end |
#instantiate(attrs = nil, selected_fields = nil, &block) ⇒ Document
Instantiate a new object, only when loaded from the database or when the attributes have already been typecast.
320 321 322 |
# File 'lib/mongoid/document.rb', line 320 def instantiate(attrs = nil, selected_fields = nil, &block) instantiate_document(attrs, selected_fields, &block) end |
#instantiate_document(attrs = nil, selected_fields = nil, options = {}) ⇒ Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A Ruby 2.x bug prevents the options hash from being keyword arguments. Once we drop support for Ruby 2.x, we can reimplement the options hash as keyword arguments.
Instantiate the document.
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/mongoid/document.rb', line 341 def instantiate_document(attrs = nil, selected_fields = nil, = {}) execute_callbacks = .fetch(:execute_callbacks, Threaded.execute_callbacks?) attributes = if Mongoid.legacy_attributes attrs else attrs&.to_h end || {} doc = allocate doc.__selected_fields = selected_fields doc.instance_variable_set(:@attributes, attributes) # TODO: remove the to_h when the legacy_attributes flag is removed. # The to_h ensures that we don't accidentally make attributes_before_type_cast # a BSON::Document. doc.instance_variable_set(:@attributes_before_type_cast, attributes&.to_h.dup) if execute_callbacks doc.apply_defaults yield(doc) if block_given? doc.run_callbacks(:find) unless doc._find_callbacks.empty? doc.run_callbacks(:initialize) unless doc._initialize_callbacks.empty? else yield(doc) if block_given? doc.pending_callbacks += [:apply_defaults, :find, :initialize] end doc end |
#logger ⇒ Logger
Returns the logger
426 427 428 |
# File 'lib/mongoid/document.rb', line 426 def logger Mongoid.logger end |
#with_callbacks(execute_callbacks) ⇒ Object
Indicate whether callbacks should be invoked by default or not, within the block. Callbacks may always be explicitly invoked by passing ‘execute_callbacks: true` where available.
299 300 301 302 303 304 305 |
# File 'lib/mongoid/document.rb', line 299 def with_callbacks(execute_callbacks) saved, Threaded.execute_callbacks = Threaded.execute_callbacks?, execute_callbacks yield ensure Threaded.execute_callbacks = saved end |