Class: PropCheck::Hooks::Enumerable Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
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.

Wraps enumerable `inner` with a `PropCheck::Hooks` object such that the before/after/around hooks are called before/after/around each element that is fetched from `inner`.

This is very helpful if you need to perform cleanup logic before/after/around e.g. data is generated or fetched.

Note that whatever is after a `yield` in an `around` hook is not guaranteed to be called (for instance when a StopIteration is raised). Thus: make sure you use `ensure` to clean up resources.

Instance Method Summary collapse

Constructor Details

#initialize(inner, hooks) ⇒ Enumerable

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.

Returns a new instance of Enumerable.


114
115
116
117
# File 'lib/prop_check/hooks.rb', line 114

def initialize(inner, hooks)
  @inner = inner
  @hooks = hooks
end

Instance Method Details

#each(&task) ⇒ 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.


119
120
121
122
123
124
125
126
127
128
129
# File 'lib/prop_check/hooks.rb', line 119

def each(&task)
  return to_enum(:each) unless block_given?

  enum = @inner.to_enum

  wrapped_yielder = @hooks.wrap_block do
    yield enum.next(&task)
  end

  loop(&wrapped_yielder)
end