class BlockRepeater::Repeater

Public Class Methods

new(manual_repeat: true, **kwargs, &block) click to toggle source

Prepare the Repeater to take the initial block to be repeated

@param manual_repeat - Determines whether the repeat method needs to called manually, used by the Repeatable module @param **kwargs - Capture other arguments in the situation where repeat isn’t being called manually @param &block - The block of code to be repeated

# File lib/block_repeater/repeater.rb, line 19
def initialize(manual_repeat: true, **kwargs, &block)
  @manual_repeat = manual_repeat
  @repeater_arguments = kwargs
  @repeat_block = block
end

Public Instance Methods

repeat(times: 25, delay: 0.2, **_) click to toggle source

Repeat a block until either the defined timeout is met or the condition block returns true

@param times - How many times to attempt to execute the main block @param delay - The amount of time to wait between each attempt @param **_ - Capture any extra keyword arguments and discard them @return The result of calling the main block the final time

# File lib/block_repeater/repeater.rb, line 32
def repeat(times: 25, delay: 0.2, **_)
  result, @condition_met, exception = nil
  times.times do
    result = @repeat_block.call
    begin
      @condition_met = @condition_block.call(result) if @condition_block
      exception = nil
    rescue RSpec::Expectations::ExpectationNotMetError => e
      exception = e
    end
    break if @condition_met

    sleep delay
  end

  raise exception if exception

  result
end
until(&block) click to toggle source

Set the block which determines if the main block should stop being executed

@param &block - The block of code which determines the target condition of the main block @return Either return the repeater object if the repeat method is being called manually

or return the result of calling the repeat method
# File lib/block_repeater/repeater.rb, line 58
def until(&block)
  @condition_block = block
  if @manual_repeat
    self
  else
    repeat **@repeater_arguments
  end
end