Class: Rootage::Item
- Inherits:
-
StructX
- Object
- StructX
- Rootage::Item
- Defined in:
- lib/rootage/core.rb
Overview
Item
is an atomic unit of Rootage, for holding infomations and processes.
Instance Method Summary collapse
-
#assign(name = self.key) {|val| ... } ⇒ Object
Define an assignment process.
-
#condition {|val| ... } ⇒ Object
Define a condition.
-
#copy ⇒ Item
Copy the object.
-
#exception(*exceptions) {|e, args| ... } ⇒ Object
Define an exception handler.
-
#execute(scenario, *args) ⇒ void
Execute the action.
-
#key ⇒ Object
Return the key of model.
-
#process {|val| ... } ⇒ Object
Define a process that is executed just after pre-process.
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.
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.
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 |
#copy ⇒ Item
Copy the object. This is shallow copy, but arrays are cloned.
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.
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.
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 |
#key ⇒ Object
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.
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 |