Module: Mongoid::Criterion::Builder

Included in:
Mongoid::Criteria
Defined in:
lib/mongoid/criterion/builder.rb

Overview

This module defines criteria behavior for building documents for specified conditions.

Instance Method Summary collapse

Instance Method Details

#build(attrs = {}) ⇒ Document

Build a document given the selector and return it. Complex criteria, such as $in and $or operations will get ignored.

Examples:

build the document.

Person.where(:title => "Sir").build

Build with selectors getting ignored.

Person.where(:age.gt => 5).build

Returns:

  • (Document)

    A non-persisted document.

Since:

  • 2.0.0



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/criterion/builder.rb', line 21

def build(attrs = {})
  klass.new(
    selector.inject(attrs) do |hash, (key, value)|
      hash.tap do |attrs|
        unless key.to_s =~ /\$/ || value.is_a?(Hash)
          attrs[key] = value
        end
      end
    end
  )
end