module Mongoid::Criteria::Modifiable
def build(attrs = {}, &block)
-
(Document)
- A non-persisted document.
Other tags:
- Example: Build with selectors getting ignored. -
Example: build the document. -
def build(attrs = {}, &block) create_document(:new, attrs, &block) end
def create(attrs = {}, &block)
-
(Document)
- A newly created document.
Other tags:
- Example: Create with selectors getting ignored. -
Example: Create the document. -
def create(attrs = {}, &block) create_document(:create, attrs, &block) end
def create!(attrs = {}, &block)
-
(Document)
- A newly created document.
Raises:
-
(Errors::Validations)
- on a validation error.
Other tags:
- Example: Create with selectors getting ignored. -
Example: Create the document. -
def create!(attrs = {}, &block) create_document(:create!, attrs, &block) end
def create_document(method, attrs = nil, &block)
-
(Document)
- The new or saved document.
Parameters:
-
attrs
(Hash
) -- Additional attributes to use. -
method
(Symbol
) -- Either :new or :create.
Other tags:
- Example: Create a new document. -
Other tags:
- Api: - private
def create_document(method, attrs = nil, &block) attrs = (create_attrs || {}).merge(attrs || {}) attributes = selector.reduce(attrs) do |hash, (key, value)| unless invalid_key?(hash, key) || invalid_embedded_doc?(value) hash[key] = value end hash end if embedded? attributes[:_parent] = parent_document attributes[:_association] = association end if polymorphic? && @criterion klass.__send__(method, attributes.merge(@criterion), &block) else klass.__send__(method, attributes, &block) end end
def create_with(attrs = {})
-
(Mongoid::Criteria)
- A criteria.
Other tags:
- Example: Define attributes to be used when a new document is created. -
def create_with(attrs = {}) tap do @create_attrs ||= {} @create_attrs.update(attrs) end end
def find_or(method, attrs = {}, &block)
-
(Document)
- The first or new document.
Parameters:
-
attrs
(Hash
) -- The attributes to query or set. -
method
(Symbol
) -- The method to invoke.
Other tags:
- Example: Find or perform an action. -
Other tags:
- Api: - private
def find_or(method, attrs = {}, &block) where(attrs).first || send(method, attrs, &block) end
def find_or_create_by(attrs = {}, &block)
-
(Document)
- A matching or newly created document.
Parameters:
-
attrs
(Hash
) -- The attributes to check.
Other tags:
- Example: Find or create the document. -
def find_or_create_by(attrs = {}, &block) find_or(:create, attrs, &block) end
def find_or_create_by!(attrs = {}, &block)
-
(Document)
- A matching or newly created document.
Raises:
-
(Errors::Validations)
- on validation error.
Parameters:
-
attrs
(Hash
) -- The attributes to check.
Other tags:
- Example: Find or create the document. -
def find_or_create_by!(attrs = {}, &block) find_or(:create!, attrs, &block) end
def find_or_initialize_by(attrs = {}, &block)
-
(Document)
- A matching or newly initialized document.
Parameters:
-
attrs
(Hash
) -- The attributes to check.
Other tags:
- Example: Find or initialize the document. -
def find_or_initialize_by(attrs = {}, &block) find_or(:new, attrs, &block) end
def first_or(method, attrs = {}, &block)
-
(Document)
- The first or new document.
Parameters:
-
attrs
(Hash
) -- The attributes to query or set. -
method
(Symbol
) -- The method to invoke.
Other tags:
- Example: First or perform an action. -
Other tags:
- Api: - private
def first_or(method, attrs = {}, &block) first || create_document(method, attrs, &block) end
def first_or_create(attrs = nil, &block)
-
(Document)
- A matching or newly created document.
Parameters:
-
attrs
(Hash
) -- The additional attributes to add.
Other tags:
- Example: First or create the document. -
def first_or_create(attrs = nil, &block) first_or(:create, attrs, &block) end
def first_or_create!(attrs = nil, &block)
-
(Document)
- A matching or newly created document.
Parameters:
-
attrs
(Hash
) -- The additional attributes to add.
Other tags:
- Example: First or create the document. -
def first_or_create!(attrs = nil, &block) first_or(:create!, attrs, &block) end
def first_or_initialize(attrs = nil, &block)
-
(Document)
- A matching or newly initialized document.
Parameters:
-
attrs
(Hash
) -- The additional attributes to add.
Other tags:
- Example: First or initialize the document. -
def first_or_initialize(attrs = nil, &block) first_or(:new, attrs, &block) end
def invalid_embedded_doc?(value)
def invalid_embedded_doc?(value) # @todo Change this to BSON::String::ILLEGAL_KEY when ruby driver 2.3.0 is # released and mongoid is updated to depend on driver >= 2.3.0 value.is_a?(Hash) && value.any? do |key, v| key.to_s =~ Mongoid::Document::ILLEGAL_KEY || invalid_embedded_doc?(v) end end
def invalid_key?(hash, key)
def invalid_key?(hash, key) # @todo Change this to BSON::String::ILLEGAL_KEY when ruby driver 2.3.0 is # released and mongoid is updated to depend on driver >= 2.3.0 key.to_s =~ Mongoid::Document::ILLEGAL_KEY || hash.key?(key.to_sym) || hash.key?(key) end