Class: Musa::Series::Composer::Composer

Inherits:
Object
  • Object
show all
Defined in:
lib/musa-dsl/series/series-composer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input: nil, inputs: [:input], outputs: [:output], auto_commit: nil, &block) ⇒ Composer

Returns a new instance of Composer.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/musa-dsl/series/series-composer.rb', line 44

def initialize(input: nil, inputs: [:input], outputs: [:output], auto_commit: nil, &block)
  auto_commit = true if auto_commit.nil?

  inputs = case inputs
           when Array
             inputs.collect { |_| [_, nil] }.to_h
           when nil
             {}
           when Hash
             inputs
           else
             raise ArgumentError, "inputs: expected a Hash with input names and source series { name: serie, ... } or an Array with names [name, ...] but received #{inputs}"
           end

  inputs[:input] = input if input

  @pipelines = {}

  def @pipelines.[]=(name, pipeline)
    pipeline_to_add = @commited ? pipeline.commit! : pipeline
    super(name, pipeline_to_add)
  end

  @dsl = DSLContext.new(@pipelines)
  @inputs = {}
  @outputs = {}

  inputs.keys&.each do |input|
    p = Musa::Series::Constructors.PROXY(inputs[input])

    @inputs[input] = @pipelines[input] = Pipeline.new(input, input: p, output: p.buffered, pipelines: @pipelines)

    @dsl.define_singleton_method(input) { input }
  end

  outputs&.each do |output|
    p = Musa::Series::Constructors.PROXY()
    @outputs[output] = @pipelines[output] = Pipeline.new(output, is_output: true, input: p, output: p, pipelines: @pipelines)

    @dsl.define_singleton_method(output) { output }
  end

  @dsl.with &block if block
  commit! if auto_commit
end

Class Method Details

.[]=(name, pipeline) ⇒ Object



62
63
64
65
# File 'lib/musa-dsl/series/series-composer.rb', line 62

def @pipelines.[]=(name, pipeline)
  pipeline_to_add = @commited ? pipeline.commit! : pipeline
  super(name, pipeline_to_add)
end

Instance Method Details

#commit!Object



114
115
116
117
118
119
120
121
122
# File 'lib/musa-dsl/series/series-composer.rb', line 114

def commit!
  raise 'Already commited' if @commited

  @outputs.each_value do |pipeline|
    pipeline.commit!
  end

  @commited = true
end

#input(name = nil) ⇒ Object



90
91
92
93
# File 'lib/musa-dsl/series/series-composer.rb', line 90

def input(name = nil)
  name ||= :input
  @inputs[name].input
end

#output(name = nil) ⇒ Object



95
96
97
98
99
100
# File 'lib/musa-dsl/series/series-composer.rb', line 95

def output(name = nil)
  raise "Can't access output if the Composer is uncommited. Call '.commit' first." unless @commited

  name ||= :output
  @outputs[name].output
end

#pipeline(name, *elements) ⇒ Object



106
107
108
# File 'lib/musa-dsl/series/series-composer.rb', line 106

def pipeline(name, *elements)
  @dsl.pipeline(name, elements)
end

#route(from, to:, on: nil, as: nil) ⇒ Object



102
103
104
# File 'lib/musa-dsl/series/series-composer.rb', line 102

def route(from, to:, on: nil, as: nil)
  @dsl.route(from, to: to, on: on, as: as)
end

#update(&block) ⇒ Object



110
111
112
# File 'lib/musa-dsl/series/series-composer.rb', line 110

def update(&block)
  @dsl.with &block
end