Class: Diddy::Script
- Inherits:
-
Object
- Object
- Diddy::Script
- 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
-
#context ⇒ Object
Returns the value of attribute context.
-
#description ⇒ Object
Returns the value of attribute description.
-
#run_result ⇒ Object
Returns the value of attribute run_result.
-
#scenarios ⇒ Object
Returns the value of attribute scenarios.
Class Method Summary collapse
-
.define(description, &block) ⇒ Object
Defines a new script.
-
.run(run_result, description, options = {}) ⇒ Object
Runs a given script.
-
.scripts ⇒ Object
Returns all the defined scripts.
Instance Method Summary collapse
-
#initialize(description) ⇒ Script
constructor
A new instance of Script.
-
#run ⇒ Object
Run the script.
-
#scenario(description, &block) ⇒ Object
Defines a new scenario.
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
#context ⇒ Object
Returns the value of attribute context.
7 8 9 |
# File 'lib/diddy/script.rb', line 7 def context @context end |
#description ⇒ Object
Returns the value of attribute description.
7 8 9 |
# File 'lib/diddy/script.rb', line 7 def description @description end |
#run_result ⇒ Object
Returns the value of attribute run_result.
7 8 9 |
# File 'lib/diddy/script.rb', line 7 def run_result @run_result end |
#scenarios ⇒ Object
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
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, = {}) 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 .any? vars = " (" + .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() script.run_result = run_result # run the entire script script.run end |
.scripts ⇒ Object
Returns all the defined scripts
82 83 84 |
# File 'lib/diddy/script.rb', line 82 def self.scripts @scripts end |
Instance Method Details
#run ⇒ Object
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 |