Class: OCG::Options
Constant Summary collapse
- VARIABLES_TO_COPY =
i[ keys value_indexes].freeze
Instance Method Summary collapse
- #finished? ⇒ Boolean
-
#initialize(options) ⇒ Options
constructor
A new instance of Options.
- #last ⇒ Object
- #length ⇒ Object
- #next ⇒ Object
- #reset ⇒ Object
- #started? ⇒ Boolean
Methods included from Copyable
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() Validation. = .transform_values(&:to_a) # End to start is more traditional way of making combinations. @keys = .keys.reverse = nil reset_value_indexes @is_finished = length.zero? end |
Instance Method Details
#finished? ⇒ Boolean
74 75 76 |
# File 'lib/ocg/options.rb', line 74 def finished? @is_finished end |
#last ⇒ Object
66 67 68 |
# File 'lib/ocg/options.rb', line 66 def last end |
#length ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/ocg/options.rb', line 78 def length .reduce(0) do |length, (_name, values)| if length.zero? values.length else length * values.length end end end |
#next ⇒ Object
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 = @value_indexes.map { |name, value_index| [name, [name][value_index]] }.to_h @is_finished = @keys.all? do |name| values = [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 end |
#reset ⇒ Object
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? = 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
70 71 72 |
# File 'lib/ocg/options.rb', line 70 def started? !.nil? end |