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:

Alternatively, check out `PropCheck::Hooks::Enumerable` which allows wrapping the elements of an enumerable with hooks.

Public Class Methods

new(before: proc {}, after: proc {}, around: proc { |*args, &block| block.call(*args) }) click to toggle source

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

add_after(&hook) click to toggle source

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
add_around(&hook) click to toggle source

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
add_before(&hook) click to toggle source

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
call(*args, &block) click to toggle source

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
wrap_block(&block) click to toggle source

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
wrap_enum(enumerable) click to toggle source
# File lib/prop_check/hooks.rb, line 29
def wrap_enum(enumerable)
  PropCheck::Hooks::Enumerable.new(enumerable, self)
end