Class: Diddy::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/diddy/script.rb

Overview

A script contains several scenarios. A script can be a recipe, an application etc.

Defined Under Namespace

Classes: CannotFindScriptError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description) ⇒ Script

Returns a new instance of Script.



9
10
11
# File 'lib/diddy/script.rb', line 9

def initialize(description)
  self.description = description
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



7
8
9
# File 'lib/diddy/script.rb', line 7

def context
  @context
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/diddy/script.rb', line 7

def description
  @description
end

#run_resultObject

Returns the value of attribute run_result.



7
8
9
# File 'lib/diddy/script.rb', line 7

def run_result
  @run_result
end

#scenariosObject

Returns the value of attribute scenarios.



7
8
9
# File 'lib/diddy/script.rb', line 7

def scenarios
  @scenarios
end

Class Method Details

.define(description, &block) ⇒ Object

Defines a new script

Diddy::Script.define('Some recipe') do
  scenario('Putting stuff in a bowl') do
    step 'Put milk in it'
    step 'Drink from bowl'
  end
end


68
69
70
71
72
73
74
75
76
77
# File 'lib/diddy/script.rb', line 68

def self.define(description, &block)
  @scripts ||= []

  script = self.new(description)
  script.instance_eval(&block)

  @scripts << script

  script
end

.run(run_result, description, options = {}) ⇒ Object

Runs a given script



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/diddy/script.rb', line 89

def self.run(run_result, description, options = {})
  script = nil

  # find script
  if description.is_a?(String)
    script = scripts.find { |script| script.description == description }

  elsif description.is_a?(Diddy::Script)
    script = description

  end

  raise CannotFindScriptError.new("Cannot find script #{description}") unless script

  # output and run
  if options.any?
    vars = " (" + options.map { |k, v| "#{k}=#{v}" }.join(', ') + ")"
  else
    vars = ""
  end

  # print to screen what we are doing
  puts(bold("Running #{script.description}#{vars}"))

  # also log to result logger
  run_result.run_script("Running #{script.description}#{vars}")

  # apply the context and run result
  script.context = Context.new(options)
  script.run_result = run_result

  # run the entire script
  script.run
end

.scriptsObject

Returns all the defined scripts



82
83
84
# File 'lib/diddy/script.rb', line 82

def self.scripts
  @scripts
end

Instance Method Details

#runObject

Run the script



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/diddy/script.rb', line 44

def run
  scenarios.each_with_index do |scenario, index|
    # reset scenario
    scenario.reset!

    # run all the steps within the scenario
    scenario.run_result = run_result
    scenario.context = context
    scenario.run

    puts("\n")
  end
end

#scenario(description, &block) ⇒ Object

Defines a new scenario

scenario('Do something') do
   uses SomeSteps

   step 'Do this'
   step 'Do that'
 end


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/diddy/script.rb', line 23

def scenario(description, &block)
  @scenarios ||= []

  # instantiate scenario
  scenario = Scenario.new(
    script: self,
    context: context,
    description: description,
    run_result: run_result
  )

  # run the DSL
  scenario.instance_eval(&block)

  # add to our collection of scenarios
  @scenarios << scenario
end