Module: MongoDoc::Scope

Defined in:
lib/mongo_doc/scope.rb

Defined Under Namespace

Classes: CriteriaProxy

Instance Method Summary collapse

Instance Method Details

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

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mongo_doc/scope.rb', line 8

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



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

def scopes
  read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
end