class OCG::Options

Constants

VARIABLES_TO_COPY

Public Class Methods

new(options) click to toggle source
# File lib/ocg/options.rb, line 13
def initialize(options)
  Validation.validate_options options
  @options = options.transform_values(&:to_a)

  # End to start is more traditional way of making combinations.
  @keys = @options.keys.reverse

  @last_options = nil

  reset_value_indexes

  @is_finished = false
end

Public Instance Methods

finished?() click to toggle source
# File lib/ocg/options.rb, line 74
def finished?
  @is_finished
end
last() click to toggle source
# File lib/ocg/options.rb, line 66
def last
  @last_options
end
length() click to toggle source
# File lib/ocg/options.rb, line 78
def length
  @options.reduce(1) { |length, (_name, values)| length * values.length }
end
next() click to toggle source
# File lib/ocg/options.rb, line 44
def next
  return nil if @is_finished

  @last_options = @value_indexes.map { |name, value_index| [name, @options[name][value_index]] }.to_h

  @is_finished = @keys.all? do |name|
    values          = @options[name]
    new_value_index = @value_indexes[name] + 1

    if new_value_index < values.length
      @value_indexes[name] = new_value_index
      next false
    end

    # Reset value index to zero.
    @value_indexes[name] = 0
    true
  end

  @last_options
end
reset() click to toggle source
# File lib/ocg/options.rb, line 31
def reset
  return nil unless started?

  @last_options = nil

  # If state is finished than all value indexes are already zero.
  reset_value_indexes unless @is_finished

  @is_finished = false

  nil
end
started?() click to toggle source
# File lib/ocg/options.rb, line 70
def started?
  !@last_options.nil?
end

Protected Instance Methods

reset_value_indexes() click to toggle source
# File lib/ocg/options.rb, line 27
          def reset_value_indexes
  @value_indexes = @options.transform_values { |_values| 0 }
end