Class: Pione::Command::BasicItem
- Inherits:
-
StructX
- Object
- StructX
- Pione::Command::BasicItem
- Defined in:
- lib/pione/command/common.rb
Instance Method Summary collapse
-
#assign(name = (self.model_name || self.name)) {|val| ... } ⇒ Object
Define an assignment process.
-
#condition {|val| ... } ⇒ Object
Define a condition.
-
#copy ⇒ Command::BasicItem
Copy the object.
-
#exception(*exceptions) {|e, args| ... } ⇒ Object
Define an exception handler.
-
#execute(cmd, *args) ⇒ void
Execute the action.
-
#process {|val| ... } ⇒ Object
Define a process that is executed just after pre-process.
Instance Method Details
#assign(name = (self.model_name || self.name)) {|val| ... } ⇒ Object
Define an assignment process. This assigns the result value of the block to model. This is same as #action excluding model assignment.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pione/command/common.rb', line 44 def assign(name=(self.model_name || self.name), &block) self.processes << Proc.new do |cmd, args| catch(:failure) do res = create_context(cmd).instance_exec(cmd, *args, &block) if name cmd.model[name] = res end end end end |
#condition {|val| ... } ⇒ Object
Define a condition. This is a process simply, but goes to quit the whale action halfway if it fails.
60 61 62 63 64 65 66 67 68 |
# File 'lib/pione/command/common.rb', line 60 def condition(&block) self.processes << Proc.new do |cmd, args| res = catch(:failure) do create_context(cmd).instance_exec(*args, &block) true end throw :quit unless res end end |
#copy ⇒ Command::BasicItem
Copy the object. This is shallow copy, but arrays are cloned.
89 90 91 92 93 94 95 |
# File 'lib/pione/command/common.rb', line 89 def copy self.class.new.tap do |obj| self.each_pair do |key, val| obj.set(key => val.is_a?(Array) ? val.clone : val) end end end |
#exception(*exceptions) {|e, args| ... } ⇒ Object
Define an exception handler.
78 79 80 81 82 83 |
# File 'lib/pione/command/common.rb', line 78 def exception(*exceptions, &block) if exceptions.empty? exceptions = [StandardError] end self.exception_handlers << ExceptionHandler.new(exceptions, block) end |
#execute(cmd, *args) ⇒ void
This method returns an undefined value.
Execute the action.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/pione/command/common.rb', line 104 def execute(cmd, *args) # catch "quit" message here catch(:quit) do processes.each {|block| block.call(cmd, args)} end rescue Exception => e exception_handlers.find do |handler| catch(:failure) { handler.try_to_handle(create_context(cmd), e, args) } end 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.
29 30 31 32 33 34 35 |
# File 'lib/pione/command/common.rb', line 29 def process(&block) self.processes << Proc.new do |cmd, args| catch(:failure) do create_context(cmd).instance_exec(*args, &block) end end end |