Class: Foreplay::Engine::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/foreplay/engine/step.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, i) ⇒ Step

Returns a new instance of Step.



4
5
6
7
# File 'lib/foreplay/engine/step.rb', line 4

def initialize(s, i)
  @step = s
  @instructions = i
end

Instance Attribute Details

#instructionsObject (readonly)

Returns the value of attribute instructions.



2
3
4
# File 'lib/foreplay/engine/step.rb', line 2

def instructions
  @instructions
end

#stepObject (readonly)

Returns the value of attribute step.



2
3
4
# File 'lib/foreplay/engine/step.rb', line 2

def step
  @step
end

Instance Method Details

#buildObject



9
10
11
12
13
14
15
16
17
# File 'lib/foreplay/engine/step.rb', line 9

def build
  # Each step can be (1) a command or (2) a series of values to add to a file
  if step.key?('key')
    instructions.key?(step['key']) ? build_commands(step, instructions) : []
  else
    # ...or just execute the command specified
    [step['command']]
  end
end

#build_commands(step, instructions) ⇒ Object



19
20
21
22
23
24
25
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
# File 'lib/foreplay/engine/step.rb', line 19

def build_commands(step, instructions)
  # Add values from the config file to a file on the remote machine
  key       = step['key']
  prefix    = step['prefix'] || ''
  suffix    = step['suffix'] || ''
  path      = step['path'] || ''
  before    = step['before'] || ''
  delimiter = step['delimiter'] || ''
  after     = step['after'] || ''

  step['silent'] = true
  filename = "#{path}#{prefix}#{key}#{suffix}"

  if step.key?('header')
    commands  = ["echo \"#{step['header']}\" > #{filename}"]
    redirect  = '>>'
  else
    commands  = []
    redirect  = '>'
  end

  if instructions[key].is_a? Hash
    instructions[key].each do |k, v|
      commands << "echo \"#{before}#{k}#{delimiter}#{v}#{after}\" #{redirect} #{filename}"
      redirect = '>>'
    end
  else
    commands << "echo \"#{before}#{delimiter}#{instructions[key]}#{after}\" #{redirect} #{filename}"
    redirect = '>>'
  end

  commands
end