Module: Mongoid::Document
- Extended by:
- ActiveSupport::Concern
- Includes:
- Composable
- Defined in:
- lib/mongoid/document.rb,
lib/mongoid/railties/document.rb
Overview
This is the base module for all domain objects that need to be persisted to the database as documents.
Defined Under Namespace
Modules: ClassMethods
Constant Summary
Constants included from Composable
Constants included from Interceptable
Constants included from Matchable
Constants included from Atomic
Instance Attribute Summary collapse
-
#criteria_instance_id ⇒ Object
Returns the value of attribute criteria_instance_id.
-
#new_record ⇒ Object
readonly
Returns the value of attribute new_record.
Attributes included from State
#destroyed, #flagged_for_destroy
Attributes included from Relations
Attributes included from Attributes
Instance Method Summary collapse
-
#_destroy ⇒ Object
Used in conjunction with fields_for to build a form element for the destruction of this association.
-
#as_document ⇒ Hash
Return a hash of the entire document hierarchy from this document and below.
-
#becomes(klass) ⇒ Document
Returns an instance of the specified class with the attributes, errors, and embedded documents of the current document.
-
#cache_key ⇒ String
Print out the cache key.
-
#freeze ⇒ Document
Freezes the internal attributes of the document.
-
#frozen? ⇒ true, false
Checks if the document is frozen.
-
#hash ⇒ Integer
Delegates to identity in order to allow two records of the same identity to work with something like:.
-
#identity ⇒ Array
A Document’s is identified absolutely by its class and database id:.
-
#initialize(attrs = nil) ⇒ Document
Instantiate a new
Document
, setting the Document’s attributes if given. -
#model_name ⇒ String
Return the model name of the document.
-
#to_a ⇒ Array<Document>
Return an array with this
Document
only in it. -
#to_key ⇒ Object
Return the key value for the document.
Methods included from Composable
Methods included from Equality
Methods included from Copyable
Methods included from Interceptable
#callback_executable?, #in_callback_state?, #run_after_callbacks, #run_before_callbacks, #run_callbacks
Methods included from Validatable
#begin_validate, #exit_validate, #performing_validations?, #read_attribute_for_validation, #valid?, #validated?, #validating_with_query?
Methods included from Traversable
#_children, #_root, #_root?, #collect_children, #flag_children_persisted, #hereditary?, #parentize, #remove_child, #reset_persisted_children
Methods included from State
#destroyed?, #flagged_for_destroy?, #new_record?, #persisted?, #pushable?, #settable?, #updateable?
Methods included from Shardable
#shard_key_fields, #shard_key_selector
Methods included from Sessions
clear, #collection, #collection_name, default, disconnect, #mongo_session, with_name
Methods included from Sessions::Options
Methods included from Serializable
Methods included from Selectable
Methods included from Reloadable
Methods included from Relations
#embedded?, #embedded_many?, #embedded_one?, #metadata_name, #referenced_many?, #referenced_one?, #reload_relations
Methods included from Relations::Touchable
Methods included from Relations::Synchronization
#remove_inverse_keys, #syncable?, #synced, #synced?, #update_inverse_keys
Methods included from Relations::Reflections
#reflect_on_all_associations, #reflect_on_association
Methods included from Relations::Macros
Methods included from Relations::Cascading
Methods included from Relations::AutoSave
#__autosaving__, #autosaved?, #changed_for_autosave?, #relation_changed_for_autosave
Methods included from Relations::Accessors
#__build__, #create_relation, #relation_exists?, #reset_relation_criteria, #set_relation
Methods included from Persistable
#atomically, #fail_due_to_callback!, #fail_due_to_validation!
Methods included from Persistable::Unsettable
Methods included from Persistable::Upsertable
Methods included from Persistable::Updatable
#update, #update!, #update_attribute, #update_document
Methods included from Persistable::Settable
Methods included from Persistable::Savable
Methods included from Persistable::Renamable
Methods included from Persistable::Pushable
Methods included from Persistable::Pullable
Methods included from Positional
Methods included from Persistable::Poppable
Methods included from Persistable::Logical
Methods included from Persistable::Incrementable
Methods included from Persistable::Destroyable
Methods included from Persistable::Deletable
Methods included from Persistable::Creatable
Methods included from Matchable
Methods included from Inspectable
Methods included from Fields
#apply_default, #apply_defaults, #apply_post_processed_defaults, #apply_pre_processed_defaults, #attribute_names, #database_field_name, #lazy_settable?, option, options, #using_object_ids?
Methods included from Evolvable
Methods included from Attributes
#assign_attributes, #attribute_present?, #attributes_before_type_cast, #has_attribute?, #has_attribute_before_type_cast?, #read_attribute, #read_attribute_before_type_cast, #remove_attribute, #write_attribute, #write_attributes
Methods included from Attributes::Readonly
Methods included from Attributes::Processing
Methods included from Changeable
#changed, #changed?, #changed_attributes, #changes, #children_changed?, #move_changes, #post_persist, #previous_changes, #remove_change, #setters
Methods included from 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, #flagged_destroys, #process_flagged_destroys
Instance Attribute Details
#criteria_instance_id ⇒ Object
Returns the value of attribute criteria_instance_id.
29 30 31 |
# File 'lib/mongoid/document.rb', line 29 def criteria_instance_id @criteria_instance_id end |
#new_record ⇒ Object (readonly)
Returns the value of attribute new_record.
30 31 32 |
# File 'lib/mongoid/document.rb', line 30 def new_record @new_record end |
Instance Method Details
#_destroy ⇒ Object
Used in conjunction with fields_for to build a form element for the destruction of this association. Always returns false because Mongoid only supports immediate deletion of associations.
See ActionView::Helpers::FormHelper::fields_for for more info.
8 9 10 |
# File 'lib/mongoid/railties/document.rb', line 8 def _destroy false end |
#as_document ⇒ Hash
Return a hash of the entire document hierarchy from this document and below. Used when the attributes are needed for everything and not just the current document.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/mongoid/document.rb', line 168 def as_document return attributes if frozen? .each_pair do |name, | without_autobuild do relation, stored = send(name), .store_as if attributes.has_key?(stored) || !relation.blank? if relation attributes[stored] = relation.as_document else attributes.delete(stored) end end end end attributes end |
#becomes(klass) ⇒ Document
Returns an instance of the specified class with the attributes, errors, and embedded documents of the current document.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/mongoid/document.rb', line 198 def becomes(klass) unless klass.include?(Mongoid::Document) raise ArgumentError, "A class which includes Mongoid::Document is expected" end became = klass.new(clone_document) became.id = id became.instance_variable_set(:@changed_attributes, changed_attributes) became.instance_variable_set(:@errors, ActiveModel::Errors.new(became)) became.errors.instance_variable_set(:@messages, errors.instance_variable_get(:@messages)) became.instance_variable_set(:@new_record, new_record?) became.instance_variable_set(:@destroyed, destroyed?) became.changed_attributes["_type"] = self.class.to_s became._type = klass.to_s # mark embedded docs as persisted .each_pair do |name, | without_autobuild do relation = became.__send__(name) Array.wrap(relation).each do |r| r.instance_variable_set(:@new_record, new_record?) end end end IdentityMap.set(became) unless became.new_record? became end |
#cache_key ⇒ String
Print out the cache key. This will append different values on the plural model name.
If new_record? - will append /new If not - will append /id-updated_at.to_s(:number) Without updated_at - will append /id
This is usually called insode a cache() block
242 243 244 245 246 |
# File 'lib/mongoid/document.rb', line 242 def cache_key return "#{model_key}/new" if new_record? return "#{model_key}/#{id}-#{updated_at.utc.to_s(:number)}" if do_or_do_not(:updated_at) "#{model_key}/#{id}" end |
#freeze ⇒ Document
Freezes the internal attributes of the document.
44 45 46 |
# File 'lib/mongoid/document.rb', line 44 def freeze as_document.freeze and self end |
#frozen? ⇒ true, false
Checks if the document is frozen
56 57 58 |
# File 'lib/mongoid/document.rb', line 56 def frozen? attributes.frozen? end |
#hash ⇒ Integer
Delegates to identity in order to allow two records of the same identity to work with something like:
[ Person.find(1), Person.find(2), Person.find(3) ] &
[ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
72 73 74 |
# File 'lib/mongoid/document.rb', line 72 def hash identity.hash end |
#identity ⇒ Array
A Document’s is identified absolutely by its class and database id:
Person.first.identity #=> [Person, Moped::BSON::ObjectId(‘4f775130a04745933a000003’)]
86 87 88 |
# File 'lib/mongoid/document.rb', line 86 def identity [ self.class, self.id ] end |
#initialize(attrs = nil) ⇒ Document
Instantiate a new Document
, setting the Document’s attributes if given. If no attributes are provided, they will be initialized with an empty Hash
.
If a primary key is defined, the document’s id will be set to that key, otherwise it will be set to a fresh Moped::BSON::ObjectId
string.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mongoid/document.rb', line 105 def initialize(attrs = nil) _building do @new_record = true @attributes ||= {} with(self.class.) apply_pre_processed_defaults apply_default_scoping process_attributes(attrs) do yield(self) if block_given? end apply_post_processed_defaults # @todo: #2586: Need to have access to parent document in these # callbacks. run_callbacks(:initialize) unless _initialize_callbacks.empty? end end |
#model_name ⇒ String
Return the model name of the document.
130 131 132 |
# File 'lib/mongoid/document.rb', line 130 def model_name self.class.model_name end |
#to_a ⇒ Array<Document>
Return an array with this Document
only in it.
154 155 156 |
# File 'lib/mongoid/document.rb', line 154 def to_a [ self ] end |
#to_key ⇒ Object
Return the key value for the document.
142 143 144 |
# File 'lib/mongoid/document.rb', line 142 def to_key (persisted? || destroyed?) ? [ id ] : nil end |