Class Index [+]

Quicksearch

DataMapper::Model::Scope

Module with query scoping functionality.

Scopes are implemented using simple array based stack that is thread local. Default scope can be set on a per repository basis.

Scopes are merged as new queries are nested. It is also possible to get exclusive scope access using with_exclusive_scope

Public Instance Methods

current_scope() click to toggle source

@api private

    # File lib/dm-core/model/scope.rb, line 34
34:       def current_scope
35:         scope_stack.last || default_scope(repository.name)
36:       end
default_scope(repository_name = default_repository_name) click to toggle source

@api private

    # File lib/dm-core/model/scope.rb, line 14
14:       def default_scope(repository_name = default_repository_name)
15:         @default_scope ||= {}
16: 
17:         default_repository_name = self.default_repository_name
18: 
19:         @default_scope[repository_name] ||= if repository_name == default_repository_name
20:           {}
21:         else
22:           default_scope(default_repository_name).dup
23:         end
24:       end
query() click to toggle source

Returns query on top of scope stack

@api private

    # File lib/dm-core/model/scope.rb, line 29
29:       def query
30:         repository.new_query(self, current_scope).freeze
31:       end

Protected Instance Methods

scope_stack() click to toggle source

Initializes (if necessary) and returns current scope stack @api private

    # File lib/dm-core/model/scope.rb, line 81
81:       def scope_stack
82:         scope_stack_for = Thread.current[:dm_scope_stack] ||= {}
83:         scope_stack_for[object_id] ||= []
84:       end
with_exclusive_scope(query) click to toggle source

Pushes given query on top of scope stack and yields given block, then pops the stack. During block execution queries previously pushed onto the stack have no effect.

@api private

    # File lib/dm-core/model/scope.rb, line 62
62:       def with_exclusive_scope(query)
63:         query = if query.kind_of?(Hash)
64:           repository.new_query(self, query)
65:         else
66:           query.dup
67:         end
68: 
69:         scope_stack = self.scope_stack
70:         scope_stack << query.options
71: 
72:         begin
73:           yield query.freeze
74:         ensure
75:           scope_stack.pop
76:         end
77:       end
with_scope(query) click to toggle source

Pushes given query on top of the stack

@param [Hash, Query] Query to add to current scope nesting

@api private

    # File lib/dm-core/model/scope.rb, line 45
45:       def with_scope(query)
46:         options = if query.kind_of?(Hash)
47:           query
48:         else
49:           query.options
50:         end
51: 
52:         # merge the current scope with the passed in query
53:         with_exclusive_scope(self.query.merge(options)) { |*block_args| yield(*block_args) }
54:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.