Class: Leash::Procedure

Inherits:
Object
  • Object
show all
Defined in:
lib/leash/procedure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, options = {}, &block) ⇒ Procedure

Returns a new instance of Procedure.



7
8
9
10
11
12
13
# File 'lib/leash/procedure.rb', line 7

def initialize(title, options = {}, &block)
  @title = title
  @options = options
  @steps = []

  instance_eval(&block) if block
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/leash/procedure.rb', line 5

def options
  @options
end

#stepsObject (readonly)

Returns the value of attribute steps.



5
6
7
# File 'lib/leash/procedure.rb', line 5

def steps
  @steps
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/leash/procedure.rb', line 5

def title
  @title
end

Instance Method Details

#lengthObject



43
44
45
# File 'lib/leash/procedure.rb', line 43

def length
  steps.length
end

#renderObject



33
34
35
36
37
38
39
40
41
# File 'lib/leash/procedure.rb', line 33

def render
  text = +"#{title}\n"

  steps.each_with_index do |step, index|
    text << "(#{index.next}/#{length}): #{step.description}\n"
  end

  text
end

#runObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/leash/procedure.rb', line 22

def run
  puts "Running: #{title}"

  steps.each_with_index do |step, index|
    puts("(#{index.next}/#{length}): #{step.description}")
    puts "> Press Enter to continue ..."
    $stdin.gets
    step.run
  end
end

#step(description, action = nil, &block) ⇒ Object



15
16
17
18
19
20
# File 'lib/leash/procedure.rb', line 15

def step(description, action = nil, &block)
  steps << Step.new(
    description: description,
    action: action || block
  )
end