Module: MongoDoc::Scope

Defined in:
lib/mongo_doc/scope.rb

Defined Under Namespace

Classes: CriteriaProxy

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



4
5
6
# File 'lib/mongo_doc/scope.rb', line 4

def self.extended(klass)
  klass.class_attribute :_scopes
end

Instance Method Details

#scope(name, *args, &block) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mongo_doc/scope.rb', line 12

def scope(name, *args, &block)
  options = args.extract_options!
  raise ArgumentError if args.size != 1
  criteria = args.first
  name = name.to_sym
  scopes[name] = lambda do |parent_scope, *args|
    CriteriaProxy.new(parent_scope, Mongoid::Criteria === criteria ? criteria : criteria.call(*args), options, &block)
  end
  (class << self; self; end).class_eval <<-EOT
    def #{name}(*args)
      scopes[:#{name}].call(self, *args)
    end
  EOT
end

#scopesObject



8
9
10
# File 'lib/mongo_doc/scope.rb', line 8

def scopes
  self._scopes ||= {}
end