class OCG::Options

OCG::Options class.

Constants

VARIABLES_TO_COPY

Current variables to be copied.

Public Class Methods

new(options) click to toggle source

Initializes options using options values. options is a hash with values convertable to array.

# File lib/ocg/options.rb, line 17
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 = length.zero?
end

Public Instance Methods

finished?() click to toggle source

Is option combinations generation finished?

# File lib/ocg/options.rb, line 84
def finished?
  @is_finished
end
last() click to toggle source

Get last option combination.

# File lib/ocg/options.rb, line 74
def last
  @last_options
end
length() click to toggle source

Get option combinations length.

# File lib/ocg/options.rb, line 89
def length
  @options.reduce(0) do |length, (_name, values)|
    if length.zero?
      values.length
    else
      length * values.length
    end
  end
end
next() click to toggle source

Get next option combination.

# File lib/ocg/options.rb, line 51
def next
  return nil if @is_finished

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

  @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

Resets current option combinations state.

# File lib/ocg/options.rb, line 37
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 = length.zero?

  nil
end
started?() click to toggle source

Is option combinations generation started?

# File lib/ocg/options.rb, line 79
def started?
  !@last_options.nil?
end

Protected Instance Methods

reset_value_indexes() click to toggle source

Resets internal value indexes.

# File lib/ocg/options.rb, line 32
          def reset_value_indexes
  @value_indexes = @options.transform_values { |_values| 0 }
end