Class: OCG::Options
Overview
OCG::Options class.
Constant Summary collapse
- VARIABLES_TO_COPY =
Current variables to be copied.
%i[options keys last_options value_indexes].freeze
Instance Method Summary collapse
-
#finished? ⇒ Boolean
Is option combinations generation finished?.
-
#initialize(options) ⇒ Options
constructor
Initializes options using
options
values. -
#last ⇒ Object
Get last option combination.
-
#length ⇒ Object
Get option combinations length.
-
#next ⇒ Object
Get next option combination.
-
#reset ⇒ Object
Resets current option combinations state.
-
#started? ⇒ Boolean
Is option combinations generation started?.
Methods included from Copyable
Constructor Details
#initialize(options) ⇒ Options
Initializes options using options
values. options
is a hash with values convertable to array.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ocg/options.rb', line 17 def initialize() Validation. @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
Is option combinations generation finished?
84 85 86 |
# File 'lib/ocg/options.rb', line 84 def finished? @is_finished end |
#last ⇒ Object
Get last option combination.
74 75 76 |
# File 'lib/ocg/options.rb', line 74 def last @last_options end |
#length ⇒ Object
Get option combinations length.
89 90 91 92 93 94 95 96 97 |
# 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 ⇒ Object
Get next option combination.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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 ⇒ Object
Resets current option combinations state.
37 38 39 40 41 42 43 44 45 46 47 48 |
# 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? ⇒ Boolean
Is option combinations generation started?
79 80 81 |
# File 'lib/ocg/options.rb', line 79 def started? !@last_options.nil? end |