module RepeaterMethods

Additional methods for the Repeater class

Constants

UNTIL_METHOD_REGEX

Public Instance Methods

call_if_method_responsive(value, method) click to toggle source
# File lib/block_repeater/repeater_methods.rb, line 51
def call_if_method_responsive(value, method)
  method = method.to_sym
  if value.respond_to?(method)
    value.send(method)
  else
    @unresponsive_errors << "#{value.class.name} does not respond to method #{method}"
  end
end
method_missing(method_name, *args, &block) click to toggle source

If a method in the correct format is provided it will be converted into the conditional block for the Repeater class This expects either one or two method names which will be called against the result of repeating the main block. The correct format is ‘until_<method name>’ or ‘until_<method name>becomes<method name>’.

examples:

until_positive? Attempts to call the :positive? method on the result of the block repeat

until_count_becomes_positive? Attempts to call :count on the result of the block repeat, then :positive? on that result

Calls superclass method
# File lib/block_repeater/repeater_methods.rb, line 22
def method_missing(method_name, *args, &block)
  if method_name.match UNTIL_METHOD_REGEX
    @manual_repeat = false
    @unresponsive_errors = []
    first_match = Regexp.last_match(1)
    second_match = Regexp.last_match(2)
    third_match = Regexp.last_match(3)

    if second_match && third_match
      final_result = self.until do |result|
        call_output = call_if_method_responsive(result, second_match)
        call_if_method_responsive(call_output, third_match) if @unresponsive_errors.empty?
      end
    else
      final_result = self.until do |result|
        call_if_method_responsive(result, first_match)
      end
    end
    raise MethodUnresponsiveError, "Methods were not compatible: #{@unresponsive_errors.uniq.join(', ')}" unless @unresponsive_errors.empty?
    final_result
  else
    super
  end
end
respond_to_missing?(method_name, *) click to toggle source
Calls superclass method
# File lib/block_repeater/repeater_methods.rb, line 47
def respond_to_missing?(method_name, *)
  method_name.match UNTIL_METHOD_REGEX || super
end