Class: Rootage::Item

Inherits:
StructX
  • Object
show all
Defined in:
lib/rootage/core.rb

Overview

Item is an atomic unit of Rootage, for holding infomations and processes.

Direct Known Subclasses

Action, Argument, Option

Instance Method Summary collapse

Instance Method Details

#assign(name = self.key) {|val| ... } ⇒ Object

Define an assignment process. This assigns the result value of the block to model. This is same as #action excluding model assignment.

Parameters:

  • name (Symbol) (defaults to: self.key)

    key name for model

Yield Parameters:

  • val (Object)

    normalized option value



47
48
49
50
51
52
53
54
55
56
# File 'lib/rootage/core.rb', line 47

def assign(name=self.key, &block)
  self.processes << Proc.new do |scenario, args|
    catch(:rootage_process_failure) do
      res = get_process_context_class(scenario).new(scenario).instance_exec(*args, &block)
      if name
        scenario.model[name] = res
      end
    end
  end
end

#condition {|val| ... } ⇒ Object

Define a condition. This is a process simply, but quits the action halfway if it fails.

Yield Parameters:

  • val (Object)

    arbitrary value, this is passed from #execute



63
64
65
66
67
68
69
70
71
# File 'lib/rootage/core.rb', line 63

def condition(&block)
  self.processes << Proc.new do |scenario, args|
    res = catch(:rootage_process_failure) do
      get_process_context_class(scenario).new(scenario).instance_exec(*args, &block)
      true
    end
    throw :rootage_item_stop unless res
  end
end

#copyItem

Copy the object. This is shallow copy, but arrays are cloned.

Returns:

  • (Item)

    copied object



92
93
94
95
96
97
98
# File 'lib/rootage/core.rb', line 92

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

#exception(*exceptions) {|e, args| ... } ⇒ Object

Define an exception handler.

Parameters:

  • exceptions (Array<Class>)

    exception classes that handler should handle, that is assumed StandardError if empty

Yield Parameters:

  • e (Exception)

    raised exception object

  • args (Array)

    arbitrary objects



81
82
83
84
85
86
# File 'lib/rootage/core.rb', line 81

def exception(*exceptions, &block)
  if exceptions.empty?
    exceptions = [StandardError]
  end
  self.exception_handlers << ExceptionHandler.new(exceptions, block)
end

#execute(scenario, *args) ⇒ void

This method returns an undefined value.

Execute the action.

Parameters:

  • scenario (Scenario)

    scenario

  • args (Array)

    arbitrary objects, this is passed to process's block



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rootage/core.rb', line 107

def execute(scenario, *args)
  catch(:rootage_item_stop) do
    processes.each {|block| self.instance_exec(scenario, args, &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

#keyObject

Return the key of model.



125
126
127
# File 'lib/rootage/core.rb', line 125

def key
  (key_name || name).to_sym
end

#process {|val| ... } ⇒ Object

Define a process that is executed just after pre-process. This is used for setting values excluding command model, for example global values.

Yield Parameters:

  • val (Object)

    arbitrary value, this is passed from #execute



32
33
34
35
36
37
38
# File 'lib/rootage/core.rb', line 32

def process(&block)
  self.processes << Proc.new do |scenario, args|
    catch(:rootage_process_failure) do
      get_process_context_class(scenario).new(scenario).instance_exec(*args, &block)
    end
  end
end