class VirtFS::ContextManager
Central/Global VirtFS
context manager. Provides a singleton context management interface for use in VirtFS
.
Contexts are recorded here on a per-thread-group basis
Attributes
thread_group[R]
Public Class Methods
context()
click to toggle source
# File lib/virtfs/context_manager.rb, line 28 def self.context current.current_context end
context!()
click to toggle source
# File lib/virtfs/context_manager.rb, line 32 def self.context! current!.current_context end
current()
click to toggle source
# File lib/virtfs/context_manager.rb, line 16 def self.current @context_manager_mutex.synchronize do @context_managers[my_thread_group] ||= ContextManager.new(my_thread_group) end end
current!()
click to toggle source
# File lib/virtfs/context_manager.rb, line 22 def self.current! @context_manager_mutex.synchronize do @context_managers[my_thread_group] || raise(VirtFS::NoContextError.new) end end
manager_for(tgroup)
click to toggle source
# File lib/virtfs/context_manager.rb, line 42 def self.manager_for(tgroup) raise ArgumentError, "value must be a ThreadGroup object" unless tgroup.is_a?(ThreadGroup) @context_manager_mutex.synchronize do @context_managers[tgroup] end end
managers()
click to toggle source
# File lib/virtfs/context_manager.rb, line 36 def self.managers @context_manager_mutex.synchronize do @context_managers.dup end end
my_thread_group()
click to toggle source
# File lib/virtfs/context_manager.rb, line 12 def self.my_thread_group Thread.current.group || ThreadGroup::Default end
new(thread_group)
click to toggle source
# File lib/virtfs/context_manager.rb, line 69 def initialize(thread_group) @thread_group = thread_group @context_mutex = Mutex.new reset end
new_manager_for(tgroup)
click to toggle source
# File lib/virtfs/context_manager.rb, line 49 def self.new_manager_for(tgroup) raise ArgumentError, "value must be a ThreadGroup object" unless tgroup.is_a?(ThreadGroup) @context_manager_mutex.synchronize do @context_managers[tgroup] = ContextManager.new(tgroup) end end
remove_manager_for(tgroup)
click to toggle source
# File lib/virtfs/context_manager.rb, line 56 def self.remove_manager_for(tgroup) raise ArgumentError, "value must be a ThreadGroup object" unless tgroup.is_a?(ThreadGroup) @context_manager_mutex.synchronize do @context_managers.delete(tgroup) end end
reset_all()
click to toggle source
# File lib/virtfs/context_manager.rb, line 63 def self.reset_all @context_manager_mutex.synchronize do @context_managers = {} end end
Public Instance Methods
[](key)
click to toggle source
# File lib/virtfs/context_manager.rb, line 75 def [](key) @context_mutex.synchronize do @contexts[key] end end
[]=(key, context)
click to toggle source
Change context without saving current context state.
# File lib/virtfs/context_manager.rb, line 84 def []=(key, context) raise ArgumentError, "Context must be a VirtFS::Context object" if context && !context.is_a?(Context) raise ArgumentError, "Cannot change the default context" if key == :default @context_mutex.synchronize do if context.nil? ctx = @contexts.delete(key) ctx.key = nil return ctx end raise "Context for given key already exists" if @contexts[key] context.key = key @contexts[key] = context end end
activate!(key)
click to toggle source
Save the current context state and change context.
# File lib/virtfs/context_manager.rb, line 108 def activate!(key) @context_mutex.synchronize do raise "Context already activated" if @saved_context raise "Context for given key doesn't exist" unless (ctx = @contexts[key]) @saved_context = @current_context @current_context = ctx @saved_context # returns the pre-activation context. end end
activated?()
click to toggle source
# File lib/virtfs/context_manager.rb, line 99 def activated? @context_mutex.synchronize do !@saved_context.nil? end end
current_context()
click to toggle source
# File lib/virtfs/context_manager.rb, line 131 def current_context @context_mutex.synchronize do @current_context end end
current_context=(key)
click to toggle source
# File lib/virtfs/context_manager.rb, line 137 def current_context=(key) @context_mutex.synchronize do raise "Context for given key doesn't exist" unless (ctx = @contexts[key]) @current_context = ctx end end
deactivate!()
click to toggle source
Restore the context state saved by activate!
# File lib/virtfs/context_manager.rb, line 121 def deactivate! @context_mutex.synchronize do raise "Context not activated" unless @saved_context ret = @current_context @current_context = @saved_context @saved_context = nil ret # returns the pre-deactivated context. end end
reset()
click to toggle source
# File lib/virtfs/context_manager.rb, line 144 def reset @context_mutex.synchronize do @contexts = {} @saved_context = nil @contexts[:default] = Context.new @current_context = @contexts[:default] end end
with(key) { || ... }
click to toggle source
# File lib/virtfs/context_manager.rb, line 153 def with(key) activate!(key) begin yield ensure deactivate! end end
without() { || ... }
click to toggle source
# File lib/virtfs/context_manager.rb, line 162 def without if !activated? yield else begin saved_context = deactivate! yield ensure activate!(saved_context.key) end end end