Class: Rootage::Scenario

Inherits:
Object
  • Object
show all
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

Command

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

.infoObject

Returns the value of attribute info.



153
154
155
# File 'lib/rootage/scenario.rb', line 153

def info
  @info
end

.requirementsObject

Returns the value of attribute requirements.



152
153
154
# File 'lib/rootage/scenario.rb', line 152

def requirements
  @requirements
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



233
234
235
# File 'lib/rootage/scenario.rb', line 233

def args
  @args
end

#current_phaseObject (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_statusObject

Returns the value of attribute exit_status.



237
238
239
# File 'lib/rootage/scenario.rb', line 237

def exit_status
  @exit_status
end

#infoObject

Returns the value of attribute info.



235
236
237
# File 'lib/rootage/scenario.rb', line 235

def info
  @info
end

#modelObject

Returns the value of attribute model.



234
235
236
# File 'lib/rootage/scenario.rb', line 234

def model
  @model
end

#running_threadObject (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.

Parameters:

  • phase_name (Symbol)

    phase name

  • action (Symbol or Action)

    action name or item



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.

Yield Parameters:

  • block (Class)

    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.

Parameters:

  • path (String)

    path that is required 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.

Parameters:

  • args (Array<Object>)

    scenario arguments



198
199
200
# File 'lib/rootage/scenario.rb', line 198

def run(*args)
  self.new(*args).run
end

.scenario_nameObject



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.

Parameters:



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.

Parameters:

  • msg_or_exception (String, Exception)

    a message or exception

  • pos (String)

    error position

  • option (Hash) (defaults to: {})

Options Hash (option):

  • :pos (String)

    error position

  • :exception (Exception)

    caused exception



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.message : msg_or_exception
  Log.fatal(msg, pos)

  # show optional exception message
  if option[:exception]
    errs = [option[:exception].message] + 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

#exitvoid

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

#runvoid

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