class Pione::Command::BasicItem

Public Instance Methods

assign(name=(self.model_name || self.name), &block) click to toggle source

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

@param name [Symbol]

attribute name in model

@yieldparam val [Object]

normalized option value
# 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(&block) click to toggle source

Define a condition. This is a process simply, but goes to quit the whale action halfway if it fails.

@yieldparam val [Object]

arbitrary value, this is passed from #execute
# 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() click to toggle source

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

@return [Command::BasicItem]

copied object
# 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, &block) click to toggle source

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/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) click to toggle source

Execute the action.

@param cmd [Command::PlainCommand]

command

@param args [Array]

arbitrary objects, this is passed to process's block

@return [void]

# 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(&block) click to toggle source

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

@yieldparam val [Object]

arbitrary value, this is passed from #execute
# 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

Private Instance Methods

create_context(cmd) click to toggle source

Create a context object for processing.

@param cmd [Command::PlainCommand] @return [ProcessContext]

a context object for processing
# File lib/pione/command/common.rb, line 122
def create_context(cmd)
  (self.context || ProcessContext).new(cmd)
end