Class: Musa::Variatio::Variatio

Inherits:
Object
  • Object
show all
Defined in:
lib/musa-dsl/generative/variatio.rb

Overview

Combinatorial variation generator.

Generates all combinations of field values using Cartesian product, constructs objects, applies attributes, and optionally finalizes.

Defined Under Namespace

Classes: B

Instance Method Summary collapse

Constructor Details

#initialize(instance_name) { ... } ⇒ void

Creates variation generator with field definitions.

Examples:

variatio = Variatio.new :obj do
  field :x, [1, 2, 3]
  field :y, [:a, :b]
  constructor { |x:, y:| { x: x, y: y } }
end

Parameters:

  • instance_name (Symbol)

    name for object parameter in blocks

Yields:

  • DSL block defining fields, constructor, attributes, finalize

Yield Returns:

  • (void)

Raises:

  • (ArgumentError)

    if instance_name not a symbol

  • (ArgumentError)

    if no block given



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/musa-dsl/generative/variatio.rb', line 148

def initialize(instance_name, &block)
  raise ArgumentError, 'instance_name should be a symbol' unless instance_name.is_a?(Symbol)
  raise ArgumentError, 'block is needed' unless block

  @instance_name = instance_name

  main_context = MainContext.new &block

  @constructor = main_context._constructor
  @fieldset = main_context._fieldset
  @finalize = main_context._finalize
end

Instance Method Details

#on(**values) ⇒ Array

Generates variations with runtime field value overrides.

Allows overriding field options at generation time, useful for limiting variation space or parameterizing generation.

Examples:

Override field values

variatio = Variatio.new :obj do
  field :x, 1..10
  field :y, [:a, :b, :c]
  constructor { |x:, y:| { x: x, y: y } }
end

# Default: 10 × 3 = 30 variations
variatio.run.size  # => 30

# Override :x to limit variations
variatio.on(x: 1..3).size  # => 3 × 3 = 9
variatio.on(x: [5], y: [:a]).size  # => 1 × 1 = 1

Parameters:

  • values (Hash{Symbol => Array, Range})

    field overrides Keys are field names, values are option arrays or ranges

Returns:

  • (Array)

    all generated variation objects



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/musa-dsl/generative/variatio.rb', line 184

def on(**values)
  constructor_binder = Musa::Extension::SmartProcBinder::SmartProcBinder.new @constructor
  finalize_binder = Musa::Extension::SmartProcBinder::SmartProcBinder.new @finalize if @finalize

  run_fieldset = @fieldset.clone # TODO: verificar que esto no da problemas

  run_fieldset.components.each do |component|
    if values.key? component.name
      component.options = values[component.name].arrayfy.explode_ranges
    end
  end

  tree_A = generate_eval_tree_A run_fieldset
  tree_B = generate_eval_tree_B run_fieldset

  parameters_set = tree_A.calc_parameters

  combinations = []

  parameters_set.each do |parameters_with_depth|
    instance = @constructor.call **(constructor_binder._apply(nil, parameters_with_depth).last)

    tree_B.run parameters_with_depth, @instance_name => instance

    if @finalize
      finalize_parameters = finalize_binder._apply(nil, parameters_with_depth).last
      finalize_parameters[@instance_name] = instance

      @finalize.call **finalize_parameters
    end

    combinations << instance
  end

  combinations
end

#runArray

Generates all variations with default field values.

Equivalent to calling #on with no overrides.

Examples:

variatio = Variatio.new :obj do
  field :x, [1, 2, 3]
  field :y, [:a, :b]
  constructor { |x:, y:| { x: x, y: y } }
end

variations = variatio.run
# => [
#   { x: 1, y: :a }, { x: 1, y: :b },
#   { x: 2, y: :a }, { x: 2, y: :b },
#   { x: 3, y: :a }, { x: 3, y: :b }
# ]

Returns:

  • (Array)

    all generated variation objects



240
241
242
# File 'lib/musa-dsl/generative/variatio.rb', line 240

def run
  on
end