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

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

Instance Method Summary collapse

Instance Method Details

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

Finders.



61
62
63
64
65
66
67
# File 'lib/mongo/model/scope.rb', line 61

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



6
7
8
9
10
11
12
13
14
15
# File 'lib/mongo/model/scope.rb', line 6

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



42
43
44
45
46
47
48
49
50
# File 'lib/mongo/model/scope.rb', line 42

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



77
78
79
80
81
82
83
# File 'lib/mongo/model/scope.rb', line 77

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



69
70
71
72
73
74
75
# File 'lib/mongo/model/scope.rb', line 69

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.



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

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

#paginate(*args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mongo/model/scope.rb', line 96

def paginate *args
  args.size.must.be_in 1..2
  if args.size == 2
    page, per_page = *args
  else
    options = args.first
    page, per_page = options[:page].try(:to_i), options[:per_page].try(:to_i)
  end
  page ||= 1
  per_page ||= PER_PAGE
  per_page = MAX_PER_PAGE if per_page > MAX_PER_PAGE
  skip((page - 1) * per_page).limit(per_page)
end

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



52
53
54
55
56
57
# File 'lib/mongo/model/scope.rb', line 52

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

#skip(n) ⇒ Object



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

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

#snapshotObject



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

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

#sort(*list) ⇒ Object Also known as: sort_by



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

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

#with_exclusive_scope(*args, &block) ⇒ Object



17
18
19
# File 'lib/mongo/model/scope.rb', line 17

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

#with_scope(*args, &block) ⇒ Object



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

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