Class: PhaseShift::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/phase_shift/builder.rb

Overview

Create a pipeline by building up stages

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_stage = nil, &block) ⇒ Builder

Returns a new instance of Builder.



12
13
14
15
16
# File 'lib/phase_shift/builder.rb', line 12

def initialize(default_stage = nil, &block)
  @default_stage = default_stage || proc { [] }
  @stages = []
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#stagesObject (readonly)

Returns the value of attribute stages.



4
5
6
# File 'lib/phase_shift/builder.rb', line 4

def stages
  @stages
end

Class Method Details

.parse_file(config, file = '(phase_shift)') ⇒ Object



6
7
8
9
10
# File 'lib/phase_shift/builder.rb', line 6

def self.parse_file(config, file = '(phase_shift)')
  builder_script = ::File.read(config)
  code = "PhaseShift::Builder.new {\n" + builder_script + "\n}.pipeline"
  eval code, TOPLEVEL_BINDING, file, 0
end

Instance Method Details

#pipelineObject



22
23
24
# File 'lib/phase_shift/builder.rb', line 22

def pipeline
  stages.inject(@default_stage) { |a, e| e[a] }
end

#runObject



26
27
28
# File 'lib/phase_shift/builder.rb', line 26

def run
  pipeline.call
end

#use(stage, *args) ⇒ Object



18
19
20
# File 'lib/phase_shift/builder.rb', line 18

def use(stage, *args)
  @stages << proc { |pipeline| stage.new(pipeline, *args) }
end