Module: MongoModel::DocumentExtensions::Scopes::ClassMethods

Defined in:
lib/mongomodel/document/scopes.rb

Instance Method Summary collapse

Instance Method Details

#current_scopeObject


52
53
54
# File 'lib/mongomodel/document/scopes.rb', line 52

def current_scope
  current_scopes.last || unscoped
end

#default_scope(scope) ⇒ Object


46
47
48
49
50
# File 'lib/mongomodel/document/scopes.rb', line 46

def default_scope(scope)
  reset_current_scopes
  previous_scope = default_scoping.last || unscoped
  default_scoping << previous_scope.merge(scope)
end

#default_scopingObject


64
65
66
# File 'lib/mongomodel/document/scopes.rb', line 64

def default_scoping
  @_default_scoping ||= []
end

#default_scoping=(scoping) ⇒ Object


68
69
70
# File 'lib/mongomodel/document/scopes.rb', line 68

def default_scoping=(scoping)
  @_default_scoping = scoping
end

#inherited(subclass) ⇒ Object


72
73
74
75
76
# File 'lib/mongomodel/document/scopes.rb', line 72

def inherited(subclass)
  super
  subclass.scopes = scopes.dup
  subclass.default_scoping = default_scoping.dup
end

#scope(name, scope) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongomodel/document/scopes.rb', line 26

def scope(name, scope)
  name = name.to_sym

  if !scopes[name] && respond_to?(name, true)
    logger.warn "Creating scope :#{name}. " \
                "Overwriting existing method #{self.name}.#{name}."
  end

  scopes[name] = lambda do |*args|
    scope.is_a?(Proc) ? scope.call(*args) : scope
  end

  singleton_class.class_eval do
    define_method(name) do |*args|
      s = scopes[name].call(*args)
      scoped.merge(s)
    end
  end
end

#scopedObject


22
23
24
# File 'lib/mongomodel/document/scopes.rb', line 22

def scoped
  current_scope.clone
end

#scopesObject


56
57
58
# File 'lib/mongomodel/document/scopes.rb', line 56

def scopes
  @_scopes ||= {}
end

#scopes=(scopes) ⇒ Object


60
61
62
# File 'lib/mongomodel/document/scopes.rb', line 60

def scopes=(scopes)
  @_scopes = scopes
end

#unscopedObject


18
19
20
# File 'lib/mongomodel/document/scopes.rb', line 18

def unscoped
  @unscoped ||= MongoModel::Scope.new(self)
end