class PropCheck::Hooks
@api private Contains the logic to combine potentially many before/after/around hooks into a single pair of procedures called `before` and `after`.
_Note: This module is an implementation detail of PropCheck
._
These can be invoked by manually calling `#before` and `#after`. Important:
-
Always call first `#before` and then `#after`. This is required to make sure that `around` callbacks will work properly.
-
Make sure that if you call `#before`, to also call `#after`. It is thus highly recommended to call `#after` inside an `ensure`. This is to make sure that `around` callbacks indeed perform their proper cleanup.
Alternatively, check out `PropCheck::Hooks::Enumerable` which allows wrapping the elements of an enumerable with hooks.
Public Class Methods
attr_reader :before, :after, :around
# File lib/prop_check/hooks.rb, line 22 def initialize(before: proc {}, after: proc {}, around: proc { |*args, &block| block.call(*args) }) @before = before @after = after @around = around freeze end
Public Instance Methods
Adds `hook` to the `after` proc. It is called before earlier-added `after` procs.
# File lib/prop_check/hooks.rb, line 75 def add_after(&hook) # old_after = @after new_after = proc { hook.call @after.call } # self self.class.new(before: @before, after: new_after, around: @around) end
Adds `hook` to the `around` proc. It is called inside earlier-added `around` procs.
# File lib/prop_check/hooks.rb, line 88 def add_around(&hook) # old_around = @around new_around = proc do |&block| @around.call do |*args| hook.call(*args, &block) end end # self self.class.new(before: @before, after: @after, around: new_around) end
Adds `hook` to the `before` proc. It is called after earlier-added `before` procs.
# File lib/prop_check/hooks.rb, line 62 def add_before(&hook) # old_before = @before new_before = proc { @before.call hook.call } # self self.class.new(before: new_before, after: @after, around: @around) end
Wraps a block with all hooks that were configured this far, and immediately calls it using the given `*args`.
See also wrap_block
# File lib/prop_check/hooks.rb, line 48 def call(*args, &block) begin @before.call() @around.call do block.call(*args) end ensure @after.call() end end
Wraps a block with all hooks that were configured this far.
This means that whenever the block is called, the before/around/after hooks are called before/around/after it.
# File lib/prop_check/hooks.rb, line 39 def wrap_block(&block) proc { |*args| call(*args, &block) } end
# File lib/prop_check/hooks.rb, line 29 def wrap_enum(enumerable) PropCheck::Hooks::Enumerable.new(enumerable, self) end