Class: OCG::Options

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Options

Returns a new instance of Options.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ocg/options.rb', line 8

def initialize(options)
  Validation.validate_options options
  @options = Hash[options.map { |name, values| [name, 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

Instance Method Details

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  @is_finished
end

#lastObject



61
62
63
# File 'lib/ocg/options.rb', line 61

def last
  @last_options
end

#lengthObject



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

def length
  @options.reduce(1) { |length, (_name, values)| length * values.length }
end

#nextObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ocg/options.rb', line 39

def next
  return nil if @is_finished

  @last_options = Hash[@value_indexes.map { |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

#resetObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ocg/options.rb', line 26

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?Boolean

Returns:

  • (Boolean)


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

def started?
  !@last_options.nil?
end