class Rootage::Sequence
‘Sequence` is a series of items.
Public Class Methods
Set a process context. This context is used as default context for items.
@param process_context_class [Class]
process context
@return [void]
# File lib/rootage/core.rb, line 243 def set_process_context_class(klass) @process_context_class = klass end
Public Instance Methods
Append the item.
@param item [Item]
the action item
@return [void]
# File lib/rootage/core.rb, line 286 def append(item) list.push(item) end
Clear list.
# File lib/rootage/core.rb, line 271 def clear list.clear end
Configure the sequence option.
@param option [Hash]
options
@return [void]
# File lib/rootage/core.rb, line 305 def configure(option) config.merge(option) end
Copy the object. This is shallow copy, but arrays are cloned.
@return [Item]
copied object
# File lib/rootage/core.rb, line 262 def copy self.class.new.tap do |obj| self.each_pair do |key, val| obj.set(key => (val.is_a?(Array) or val.is_a?(Hash)) ? val.clone : val) end end end
Define an exception handler.
@param exceptions [Array<Class>]
exception classes that handler should handle, that is assumed `StandardError` if empty
@yieldparam e [Exception]
raised exception object
@yieldparam args [Array]
arbitrary objects
# File lib/rootage/core.rb, line 343 def exception(*exceptions, &block) if exceptions.empty? exceptions = [StandardError] end self.exception_handlers << ExceptionHandler.new(exceptions, block) end
# File lib/rootage/core.rb, line 361 def execute(scenario, &block) catch(:rootage_sequence_quit) do execute_pre(scenario, &block) execute_main(scenario, &block) execute_post(scenario, &block) end rescue Exception => e catch(:rootage_item_stop) do if exception_handlers.find do |handler| catch(:rootage_process_failure) do handler.try_to_handle(get_process_context_class(scenario).new(scenario), e, args) end end else raise end end end
Return the context class. If this object has no context class, return class’s context.
# File lib/rootage/core.rb, line 277 def get_process_context_class(scenario) self.process_context_class || self.class.instance_variable_get(:@process_context_class) || scenario.process_context_class end
Define a post-action item.
@param name [Symbol]
item name
@yieldparam item [Item]
defined postprocess item
# File lib/rootage/core.rb, line 328 def post(name, &block) self.posts << item_class.new.tap do |item| item.name = name block.call(item) end end
Define a pre-action item.
@param name [Symbol]
item name
@yieldparam item [Item]
defined preprocess item
# File lib/rootage/core.rb, line 315 def pre(name, &block) self.pres << item_class.new.tap do |item| item.name = name block.call(item) end end
Preppend the item.
@param item [Item]
the action item
@return [void]
# File lib/rootage/core.rb, line 296 def preppend(item) list.unshift(item) end
Use the item. This defines an item and pushs it to the sequence.
@param item [Symbol or Item]
item
@yield [item]
the cloned item
# File lib/rootage/core.rb, line 356 def use(item, &block) _item = define(item, &block) list << _item end
Private Instance Methods
# File lib/rootage/core.rb, line 403 def execute_item(scenario, item, &block) if item.is_a?(Symbol) if table.has_key?(item) item = table[item] else raise NoSuchItem.new(scenario.name, name, item) end end # set context class if item.process_context_class.nil? item.process_context_class = get_process_context_class(scenario) end if block_given? yield item end item.execute(scenario) # log args = {scenario: scenario.name, phase: name, action: item.name} Log.debug('"%{scenario}" has executed an item "%{action}" at the phase "%{phase}".' % args) end
Execute all actions in this sequence.
@param cmd [Scenario]
a scenario owned this sequence
@return [void]
# File lib/rootage/core.rb, line 399 def execute_items(items, scenario, &block) items.each {|item| execute_item(scenario, item, &block)} end
# File lib/rootage/core.rb, line 386 def execute_main(scenario, &block) execute_items(list, scenario, &block) end
# File lib/rootage/core.rb, line 390 def execute_post(scenario, &block) execute_items(posts, scenario, &block) end
# File lib/rootage/core.rb, line 382 def execute_pre(scenario, &block) execute_items(pres, scenario, &block) end