Class: OCG::Options

Inherits:
Object
  • Object
show all
Includes:
Copyable
Defined in:
lib/ocg/options.rb

Constant Summary collapse

VARIABLES_TO_COPY =
i[options keys last_options value_indexes].freeze

Instance Method Summary collapse

Methods included from Copyable

#initialize_copy

Constructor Details

#initialize(options) ⇒ Options

Returns a new instance of Options.



13
14
15
16
17
18
19
20
21
22
23
24
25
# 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 = length.zero?
end

Instance Method Details

#finished?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/ocg/options.rb', line 74

def finished?
  @is_finished
end

#lastObject



66
67
68
# File 'lib/ocg/options.rb', line 66

def last
  @last_options
end

#lengthObject



78
79
80
81
82
83
84
85
86
# File 'lib/ocg/options.rb', line 78

def length
  @options.reduce(0) do |length, (_name, values)|
    if length.zero?
      values.length
    else
      length * values.length
    end
  end
end

#nextObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

#resetObject



31
32
33
34
35
36
37
38
39
40
41
42
# 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 = length.zero?

  nil
end

#started?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/ocg/options.rb', line 70

def started?
  !@last_options.nil?
end