Class: MySQLNoIo::WrapStack
- Inherits:
-
Object
- Object
- MySQLNoIo::WrapStack
- Defined in:
- lib/mysqlnoio/wrap_stack.rb
Overview
A WrapStack collects a series of Wraps, and runs their #execute commands inside one another.
Example: stack = MySQLNoIo::WrapStack.new()a
wrapFoo = Wrap::Foo.new() wrapBar = Wrap::Bar.new()
stack.add_stack wrapFoo stack.add_stack wrapBar stack.execute do puts "hi" end
Is identical to: wrapFoo.execute do wrapBar.execute do puts "hi" end end
Instance Method Summary collapse
-
#add_wrap(wrap) ⇒ TrueClass
Add a layer to wrap around the execution of the command.
-
#execute(&block) ⇒ Object
Execute your bit of code from within the safety and comfort of the wrappers appended to the stack.
-
#initialize ⇒ WrapStack
constructor
A new instance of WrapStack.
Constructor Details
#initialize ⇒ WrapStack
Returns a new instance of WrapStack.
26 27 28 |
# File 'lib/mysqlnoio/wrap_stack.rb', line 26 def initialize @wraps = [] end |
Instance Method Details
#add_wrap(wrap) ⇒ TrueClass
Add a layer to wrap around the execution of the command
36 37 38 39 40 41 42 43 44 |
# File 'lib/mysqlnoio/wrap_stack.rb', line 36 def add_wrap wrap unless wrap.respond_to?('execute') raise MySQLNoIo::Errors::ArgumentError.new('Wrap must respond to #execute and accept a block.') end @wraps << wrap true end |
#execute(&block) ⇒ Object
Execute your bit of code from within the safety and comfort of the wrappers appended to the stack.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mysqlnoio/wrap_stack.rb', line 48 def execute &block # Brace yourself. # # This next bit of code creates a nested lamda like the WrapStack # describes. deep_proc = @wraps.reverse.inject(block) do |deep_proc, layer| Proc.new do layer.execute do deep_proc.call end end end deep_proc.call end |