Class: Musa::Variatio::Variatio

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

Defined Under Namespace

Classes: B

Instance Method Summary collapse

Constructor Details

#initialize(instance_name, &block) ⇒ Variatio

Returns a new instance of Variatio.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/musa-dsl/generative/variatio.rb', line 13

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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/musa-dsl/generative/variatio.rb', line 26

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

#runObject



63
64
65
# File 'lib/musa-dsl/generative/variatio.rb', line 63

def run
  on
end