forked from mongodb/mongoid
/
scope.rb
31 lines (29 loc) · 902 Bytes
/
scope.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# encoding: utf-8
module Mongoid #:nodoc:
# This module handles behaviour for defining scopes on classes.
class Scope
attr_reader :conditions, :extensions
# Create the new +Scope+. If a block is passed in, this Scope will store
# the block for future calls to #extend.
#
# @example Create a new scope.
# Scope.new(:title => "Sir")
#
# @param [ Hash ] conditions The scoping limitations.
def initialize(conditions = {}, &block)
@conditions = conditions
@extensions = Module.new(&block) if block_given?
end
# Extend a supplied criteria.
#
# @example Extend the criteria.
# scope.extend(criteria)
#
# @param [ Criteria } criteria A mongoid criteria to extend.
#
# @return [ Criteria ] The new criteria object.
def extend(criteria)
extensions ? criteria.extend(extensions) : criteria
end
end
end