Module: Tins::Scope
- Included in:
- DynamicScope
- Defined in:
- lib/tins/dslkit.rb
Overview
A module that provides thread-local stack-based scoping functionality.
This module implements a context management system where each thread maintains its own isolated scope stacks. It's particularly useful for tracking nested contexts in multi-threaded applications.
Instance Method Summary collapse
-
#scope(name = :default) ⇒ Array
Returns a copy of the specified scope stack.
-
#scope_block(scope_frame, name = :default) {|void| ... } ⇒ Object
Executes a block within the context of a scope frame.
-
#scope_get(name = :default) ⇒ Array
Retrieves or initializes the specified scope stack.
-
#scope_pop(name = :default) ⇒ self
Pops a scope frame from the top of the specified scope stack.
-
#scope_push(scope_frame, name = :default) ⇒ self
Pushes a scope frame onto the top of the specified scope stack.
-
#scope_reverse(name = :default) {|frame| ... } ⇒ Enumerator
Iterates through the specified scope stack in reverse order.
-
#scope_top(name = :default) ⇒ Object?
Returns the top element of the specified scope stack without removing it.
Instance Method Details
#scope(name = :default) ⇒ Array
Returns a copy of the specified scope stack.
1053 1054 1055 |
# File 'lib/tins/dslkit.rb', line 1053 def scope(name = :default) scope_get(name).dup end |
#scope_block(scope_frame, name = :default) {|void| ... } ⇒ Object
Executes a block within the context of a scope frame.
Automatically pushes the scope frame before yielding and pops it after the block completes, even if an exception occurs.
1034 1035 1036 1037 1038 1039 |
# File 'lib/tins/dslkit.rb', line 1034 def scope_block(scope_frame, name = :default) scope_push(scope_frame, name) yield ensure scope_pop(name) end |
#scope_get(name = :default) ⇒ Array
Retrieves or initializes the specified scope stack.
1045 1046 1047 |
# File 'lib/tins/dslkit.rb', line 1045 def scope_get(name = :default) Thread.current[name] ||= [] end |
#scope_pop(name = :default) ⇒ self
Pops a scope frame from the top of the specified scope stack.
If the scope becomes empty after popping, it is automatically removed from Thread.current to prevent memory leaks.
1002 1003 1004 1005 1006 |
# File 'lib/tins/dslkit.rb', line 1002 def scope_pop(name = :default) scope_get(name).pop scope_get(name).empty? and Thread.current[name] = nil self end |
#scope_push(scope_frame, name = :default) ⇒ self
Pushes a scope frame onto the top of the specified scope stack.
990 991 992 993 |
# File 'lib/tins/dslkit.rb', line 990 def scope_push(scope_frame, name = :default) scope_get(name).push scope_frame self end |
#scope_reverse(name = :default) {|frame| ... } ⇒ Enumerator
Iterates through the specified scope stack in reverse order.
1021 1022 1023 |
# File 'lib/tins/dslkit.rb', line 1021 def scope_reverse(name = :default, &block) scope_get(name).reverse_each(&block) end |
#scope_top(name = :default) ⇒ Object?
Returns the top element of the specified scope stack without removing it.
1012 1013 1014 |
# File 'lib/tins/dslkit.rb', line 1012 def scope_top(name = :default) scope_get(name).last end |