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

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

Instance Method Summary collapse

Instance Method Details

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

Finders.



58
59
60
61
62
63
64
# File 'lib/mongo/model/scope.rb', line 58

def count selector = {}, options = {}
  if current = current_scope
    super current.selector.merge(selector), current.options.merge(options)
  else
    super selector, options
  end
end

#current_scopeObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/mongo/model/scope.rb', line 3

def current_scope
  scope, exclusive = Thread.current[scope_identifier]
  current = if exclusive
    scope
  elsif scope
    default_scope ? default_scope.merge(scope) : scope
  else
    default_scope
  end
end

#default_scope(*args, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/mongo/model/scope.rb', line 39

def default_scope *args, &block
  if block
    self._default_scope = -> {query block.call}
  elsif !args.empty?
    self._default_scope = -> {query *args}
  else
    _default_scope && _default_scope.call
  end
end

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



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

def each  selector = {}, options = {}, &block
  if current = current_scope
    super current.selector.merge(selector), current.options.merge(options), &block
  else
    super selector, options, &block
  end
end

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



66
67
68
69
70
71
72
# File 'lib/mongo/model/scope.rb', line 66

def first  selector = {}, options = {}
  if current = current_scope
    super current.selector.merge(selector), current.options.merge(options)
  else
    super selector, options
  end
end

#limit(n) ⇒ Object

Shortcuts for frequently used scopes.



84
# File 'lib/mongo/model/scope.rb', line 84

def limit n; query({}, limit: n) end

#paginate(page, per_page) ⇒ Object



92
93
94
# File 'lib/mongo/model/scope.rb', line 92

def paginate page, per_page
  skip((page - 1) * per_page).limit(per_page)
end

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



49
50
51
52
53
54
# File 'lib/mongo/model/scope.rb', line 49

def scope name, *args, &block
  model = self
  metaclass.define_method name do
    query (block && instance_eval(&block)) || args
  end
end

#skip(n) ⇒ Object



85
# File 'lib/mongo/model/scope.rb', line 85

def skip n; query({}, skip: n) end

#snapshotObject



90
# File 'lib/mongo/model/scope.rb', line 90

def snapshot; query({}, snapshot: true) end

#sort(*list) ⇒ Object



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

def sort *list
  list = list.collect{|item| item.is_a?(Array) ? item : [item, 1]}
  query({}, sort: list)
end

#with_exclusive_scope(*args, &block) ⇒ Object



14
15
16
# File 'lib/mongo/model/scope.rb', line 14

def with_exclusive_scope *args, &block
  with_scope *(args << true), &block
end

#with_scope(*args, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mongo/model/scope.rb', line 18

def with_scope *args, &block
  if args.last.is_a?(TrueClass) or args.last.is_a?(FalseClass)
    exclusive = args.pop
  else
    exclusive = false
  end

  scope = query *args
  previous_scope, previous_exclusive = Thread.current[scope_identifier]
  raise "exclusive scope already applied!" if previous_exclusive

  begin
    scope = previous_scope.merge scope if !exclusive and previous_scope
    Thread.current[scope_identifier] = [scope, exclusive]
    return block.call
  ensure
    Thread.current[scope_identifier] = [previous_scope, false]
  end
end