Class: PropCheck::Hooks Private

Inherits:
Object
  • Object
show all
Defined in:
lib/prop_check/hooks.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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.

API:

  • private

Defined Under Namespace

Classes: Enumerable

Instance Method Summary collapse

Constructor Details

#initialize(before: proc {}, after: proc {}, around: proc { |*args, &block| block.call(*args) }) ⇒ Hooks

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

attr_reader :before, :after, :around

API:

  • private



22
23
24
25
26
27
# 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

Instance Method Details

#add_after(&hook) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds hook to the after proc. It is called before earlier-added after procs.

API:

  • private



75
76
77
78
79
80
81
82
83
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds hook to the around proc. It is called inside earlier-added around procs.

API:

  • private



88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds hook to the before proc. It is called after earlier-added before procs.

API:

  • private



62
63
64
65
66
67
68
69
70
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wraps a block with all hooks that were configured this far, and immediately calls it using the given ‘*args`.

See also #wrap_block

API:

  • private



48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

API:

  • private



39
40
41
# File 'lib/prop_check/hooks.rb', line 39

def wrap_block(&block)
  proc { |*args| call(*args, &block) }
end

#wrap_enum(enumerable) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



29
30
31
# File 'lib/prop_check/hooks.rb', line 29

def wrap_enum(enumerable)
  PropCheck::Hooks::Enumerable.new(enumerable, self)
end