Class: Mongoid::Scope

Inherits:
Object show all
Defined in:
lib/mongoid/scope.rb

Overview

This module handles behaviour for defining scopes on classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conditions = {}, &block) ⇒ Scope

Create the new Scope. If a block is passed in, this Scope will store the block for future calls to #extend.

Examples:

Create a new scope.

Scope.new(:title => "Sir")

Parameters:

  • conditions (Hash) (defaults to: {})

    The scoping limitations.



14
15
16
17
# File 'lib/mongoid/scope.rb', line 14

def initialize(conditions = {}, &block)
  @conditions = conditions
  @extensions = Module.new(&block) if block_given?
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



5
6
7
# File 'lib/mongoid/scope.rb', line 5

def conditions
  @conditions
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



5
6
7
# File 'lib/mongoid/scope.rb', line 5

def extensions
  @extensions
end

Instance Method Details

#extend(criteria) ⇒ Criteria

Extend a supplied criteria.

Examples:

Extend the criteria.

scope.extend(criteria)

Parameters:

  • criteria (Criteria)

    A mongoid criteria to extend.

Returns:

  • (Criteria)

    The new criteria object.



27
28
29
# File 'lib/mongoid/scope.rb', line 27

def extend(criteria)
  extensions ? criteria.extend(extensions) : criteria
end