Module: Mongo::Model::Scope::ClassMethods

Defined in:
lib/mongo_db/model/scope.rb

Instance Method Summary collapse

Instance Method Details

#count(selector = {}, opts = {}) ⇒ Object

finders



87
88
89
# File 'lib/mongo_db/model/scope.rb', line 87

def count selector = {}, opts = {}
  super current_scope.merge(selector), opts
end

#current_scopeObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/mongo_db/model/scope.rb', line 33

def current_scope
  scope, exclusive = Thread.current[:mongo_model_scope]
  if exclusive
    scope
  elsif scope
    default_scope.merge scope
  else
    default_scope
  end
end

#default_scope(*args, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mongo_db/model/scope.rb', line 62

def default_scope *args, &block
  if block
    self._default_scope = block
  elsif !args.empty?
    args.size.must == 1
    args.first.must_be.a Hash
    scope = args.first
    self._default_scope = -> {args.first}
  else
    _default_scope.call
  end
end

#each(selector = {}, opts = {}, &block) ⇒ Object



95
96
97
# File 'lib/mongo_db/model/scope.rb', line 95

def each selector = {}, opts = {}, &block
  super current_scope.merge(selector), opts, &block
end

#first(selector = {}, opts = {}) ⇒ Object



91
92
93
# File 'lib/mongo_db/model/scope.rb', line 91

def first selector = {}, opts = {}
  super current_scope.merge(selector), opts
end

#scope(name, options = nil, &block) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/mongo_db/model/scope.rb', line 75

def scope name, options = nil, &block
  model = self
  metaclass.define_method name do
    scope = (block && block.call) || options
    ScopeProxy.new model, scope
  end
end

#with_exclusive_scope(options = {}, &block) ⇒ Object



44
45
46
# File 'lib/mongo_db/model/scope.rb', line 44

def with_exclusive_scope options = {}, &block
  with_scope options, true, &block
end

#with_scope(options = {}, exclusive = false, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mongo_db/model/scope.rb', line 48

def with_scope options = {}, exclusive = false, &block
  previous_options, previous_exclusive = Thread.current[:mongo_model_scope]
  raise "exclusive scope already applied!" if previous_exclusive

  begin
    options = previous_options.merge options if previous_options and !exclusive
    Thread.current[:mongo_model_scope] = [options, exclusive]
    return block.call
  ensure
    Thread.current[:mongo_model_scope] = [previous_options, false]
  end
end