Class: Diddy::Steps

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/diddy/steps.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#try, #try_with_exception

Constructor Details

#initialize(shared_scope) ⇒ Steps

Returns a new instance of Steps.



8
9
10
# File 'lib/diddy/steps.rb', line 8

def initialize(shared_scope)
  @shared_scope = shared_scope
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



6
7
8
# File 'lib/diddy/steps.rb', line 6

def context
  @context
end

#current_stepObject

Returns the value of attribute current_step.



6
7
8
# File 'lib/diddy/steps.rb', line 6

def current_step
  @current_step
end

#sub_stepsObject

Returns the value of attribute sub_steps.



6
7
8
# File 'lib/diddy/steps.rb', line 6

def sub_steps
  @sub_steps
end

Class Method Details

.definition(description) ⇒ Object

Returns proc for step



59
60
61
# File 'lib/diddy/steps.rb', line 59

def self.definition(description)
  @steps[description]
end

.has_step?(description) ⇒ Boolean

Checks if a step is defined

Returns:

  • (Boolean)


52
53
54
# File 'lib/diddy/steps.rb', line 52

def self.has_step?(description)
  @steps && @steps.has_key?(description)
end

.step(description, &block) ⇒ Object

Describes a step

step('Do something crazy') do
  ...
  a == true
end

Steps must return TRUE to keep the script running. When a step returns a FALSE, it will stop the script.

It is also to use sub_steps (not to confuse with dubstep).

step('Do something crazy') do
  sub_step('Eat brains') do
    a == true
  end

  sub_step('Go insane') do
    b == true
  end
end

In the case above, every sub_step needs to return true.



37
38
39
40
# File 'lib/diddy/steps.rb', line 37

def self.step(description, &block)
  @steps ||= {}
  @steps[description] = block
end

.stepsObject

Returns the defined steps



45
46
47
# File 'lib/diddy/steps.rb', line 45

def self.steps
  @steps
end

Instance Method Details

#reset!Object

Reset me



100
101
102
103
104
105
106
# File 'lib/diddy/steps.rb', line 100

def reset!
  instance_variables.each do |var|
    unless %w(@shared_scope).include?(var.to_s)
      remove_instance_variable(var)
    end
  end
end

#sharedObject

Returns shared scope



66
67
68
# File 'lib/diddy/steps.rb', line 66

def shared
  @shared_scope
end

#sub_step(description, &block) ⇒ Object

Adds a sub_step



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/diddy/steps.rb', line 73

def sub_step(description, &block)
  # print pending to screen
  print("\n")
  print(blue("  . #{description}"))

  result_hash = { description: description, valid: false }
  self.sub_steps << result_hash

  # yield the block
  valid = yield(block)

  # jump back to beginning of line
  print("\r")

  # print out success or failure
  if valid
    print(green("#{description}"))
  else
    print(red(bold("#{description}")))
  end

  result_hash[:valid] = valid
end