Class: Rootage::Scenario
- Inherits:
-
Object
- Object
- Rootage::Scenario
- Extended by:
- ScenarioInterface
- Includes:
- ScenarioInterface
- Defined in:
- lib/rootage/scenario.rb
Overview
Scenario is a class that controls flows and handles sequencial actions.
Direct Known Subclasses
Class Attribute Summary collapse
-
.__phase__ ⇒ Object
Returns the value of attribute phase.
-
.info ⇒ Object
Returns the value of attribute info.
-
.requirements ⇒ Object
Returns the value of attribute requirements.
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#current_phase ⇒ Object
readonly
Returns the value of attribute current_phase.
-
#exit_status ⇒ Object
Returns the value of attribute exit_status.
-
#info ⇒ Object
Returns the value of attribute info.
-
#model ⇒ Object
Returns the value of attribute model.
-
#running_thread ⇒ Object
readonly
Returns the value of attribute running_thread.
Class Method Summary collapse
-
.define_action(phase_name, action, &b) ⇒ Object
Register the action to the phase.
- .inherited(subclass) ⇒ Object
-
.make {|block| ... } ⇒ Object
Make a new scenario class.
-
.require(path) ⇒ void
Define a requirement of library.
-
.run(*args) ⇒ Object
Run the scenario with the arguments.
- .scenario_name ⇒ Object
Instance Method Summary collapse
-
#<<(object) ⇒ void
Push the phase action to the scenario.
-
#abort(msg_or_exception, option = {}) ⇒ Object
Exit the running command and return failure status.
-
#exit ⇒ void
Exit running scenario and return the status.
-
#initialize(*args) ⇒ Scenario
constructor
Initialize a scenario object.
-
#run ⇒ void
Run the lifecycle of command.
Methods included from ScenarioInterface
define, define_phase, desc, phase, phase_class, process_context_class, scenario_name
Constructor Details
#initialize(*args) ⇒ Scenario
Initialize a scenario object.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/rootage/scenario.rb', line 244 def initialize(*args) # hold scenario arguments @args = args # copy requirements, info, phases from the class @requirements = self.class.requirements.clone @info = self.class.instance_variable_get(:@info).clone @__phase__ = self.class.instance_variable_get(:@__phase__).clone # setup scenario model if self.class.info[:model] @model = self.class.info[:model].new else @model = Model.new end @model[:scenario_name] = @info.scenario_name @model[:scenario_desc] = @info.desc # init @current_phase = nil @exit_status = true @running_thread = nil end |
Class Attribute Details
.__phase__ ⇒ Object
Returns the value of attribute phase.
154 155 156 |
# File 'lib/rootage/scenario.rb', line 154 def __phase__ @__phase__ end |
.info ⇒ Object
Returns the value of attribute info.
153 154 155 |
# File 'lib/rootage/scenario.rb', line 153 def info @info end |
.requirements ⇒ Object
Returns the value of attribute requirements.
152 153 154 |
# File 'lib/rootage/scenario.rb', line 152 def requirements @requirements end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
233 234 235 |
# File 'lib/rootage/scenario.rb', line 233 def args @args end |
#current_phase ⇒ Object (readonly)
Returns the value of attribute current_phase.
236 237 238 |
# File 'lib/rootage/scenario.rb', line 236 def current_phase @current_phase end |
#exit_status ⇒ Object
Returns the value of attribute exit_status.
237 238 239 |
# File 'lib/rootage/scenario.rb', line 237 def exit_status @exit_status end |
#info ⇒ Object
Returns the value of attribute info.
235 236 237 |
# File 'lib/rootage/scenario.rb', line 235 def info @info end |
#model ⇒ Object
Returns the value of attribute model.
234 235 236 |
# File 'lib/rootage/scenario.rb', line 234 def model @model end |
#running_thread ⇒ Object (readonly)
Returns the value of attribute running_thread.
238 239 240 |
# File 'lib/rootage/scenario.rb', line 238 def running_thread @running_thread end |
Class Method Details
.define_action(phase_name, action, &b) ⇒ Object
Register the action to the phase.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/rootage/scenario.rb', line 208 def define_action(phase_name, action, &b) # setup action item case action when Symbol item = Action.new(name: action) when Action item = action.copy else raise ArgumentError.new("%p is invalid action" % action) end # action customization b.call(item) if b # append it to the phase @__phase__[phase_name].define(item) end |
.inherited(subclass) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/rootage/scenario.rb', line 156 def inherited(subclass) # subclass inherits superclass's requirements subclass.requirements = @requirements.clone # subclass inherits superclass's info subclass.info = @info.clone # subclass inherits superclass's phases subclass.__phase__ = Hash.new @__phase__.each do |key, val| subclass.__phase__[key] = val.copy end end |
.make {|block| ... } ⇒ Object
Make a new scenario class.
178 179 180 181 182 |
# File 'lib/rootage/scenario.rb', line 178 def make(&block) klass = Class.new(self) klass.instance_exec(&block) return klass end |
.require(path) ⇒ void
This method returns an undefined value.
Define a requirement of library. This is same as Kernel.require, but loads it when the scenario runs.
190 191 192 |
# File 'lib/rootage/scenario.rb', line 190 def require(path) @requirements << path end |
.run(*args) ⇒ Object
Run the scenario with the arguments.
198 199 200 |
# File 'lib/rootage/scenario.rb', line 198 def run(*args) self.new(*args).run end |
.scenario_name ⇒ Object
170 171 172 |
# File 'lib/rootage/scenario.rb', line 170 def scenario_name @info[:name] end |
Instance Method Details
#<<(object) ⇒ void
This method returns an undefined value.
Push the phase action to the scenario.
289 290 291 292 293 294 295 296 297 |
# File 'lib/rootage/scenario.rb', line 289 def <<(object) if @__phase__.empty? phase_name = :default define_phase(phase_name) else phase_name = @__phase__.keys.last end phase(phase_name) << object end |
#abort(msg_or_exception, option = {}) ⇒ Object
Exit the running command and return failure status. Note that this method enters termination phase before it exits.
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/rootage/scenario.rb', line 322 def abort(msg_or_exception, option={}) pos = option[:pos] || caller(1).first # show abortion message msg = msg_or_exception.is_a?(Exception) ? msg_or_exception. : msg_or_exception Log.fatal(msg, pos) # show optional exception message if option[:exception] errs = [option[:exception].] + option[:exception].backtrace Log.fatal(errs.join("\n"), pos) end # set exit status code @exit_status = false # do abort process if respond_to?(:abort_process, true) abort_process end # finally, exit the command exit end |
#exit ⇒ void
This method returns an undefined value.
Exit running scenario and return the status.
302 303 304 305 306 307 308 |
# File 'lib/rootage/scenario.rb', line 302 def exit Log.debug('"%{name}" exits with status "%{status}".' % {name: name, status: @exit_status}) if respond_to?(:exit_process, true) exit_process end end |
#run ⇒ void
This method returns an undefined value.
Run the lifecycle of command. This fires sequencial actions in each phase.
271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/rootage/scenario.rb', line 271 def run # raise an error when running thread exists already if @running_thread raise ScenarioError.new("Running thread exists already.") end @running_thread = Thread.current load_requirements execute_phases @running_thread = nil return self end |