Module: ConvenientService::Support::FiniteLoop Private

Included in:
Utils::Module::GetOwnInstanceMethod
Defined in:
lib/convenient_service/support/finite_loop.rb

Overview

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

Since:

  • 1.0.0

Defined Under Namespace

Modules: Exceptions

Constant Summary collapse

MAX_ITERATION_COUNT =

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

Returns:

  • (Integer)

Since:

  • 1.0.0

1_000

Class Method Summary collapse

Class Method Details

.finite_loop(default: nil, max_iteration_count: MAX_ITERATION_COUNT, raise_on_exceedance: true, &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.

Returns Can be any type.

Examples:

class Person
  include ConvenientService::Support::FiniteLoop

  def foo
    finite_loop do |index|
      break if index > 3
    end
  end
end

Parameters:

  • max_iteration_count (Integer) (defaults to: MAX_ITERATION_COUNT)
  • raise_on_exceedance (Boolean) (defaults to: true)
  • block (Proc, nil)

Returns:

  • (Object)

    Can be any type.

Since:

  • 1.0.0



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/convenient_service/support/finite_loop.rb', line 74

def finite_loop(default: nil, max_iteration_count: MAX_ITERATION_COUNT, raise_on_exceedance: true, &block)
  ::ConvenientService.raise Exceptions::NoBlockGiven.new unless block

  loop.with_index do |_, index|
    if index >= max_iteration_count
      break default unless raise_on_exceedance

      ::ConvenientService.raise Exceptions::MaxIterationCountExceeded.new(limit: max_iteration_count)
    end

    yield(index)
  end
end