Class: Pione::Command::BasicItem

Inherits:
StructX
  • Object
show all
Defined in:
lib/pione/command/common.rb

Instance Method Summary collapse

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.

Parameters:

  • name (Symbol) (defaults to: (self.model_name || self.name))

    attribute name in model

Yield Parameters:

  • val (Object)

    normalized option value



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.

Yield Parameters:

  • val (Object)

    arbitrary value, this is passed from #execute



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

#copyCommand::BasicItem

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

Returns:



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.

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



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.

Parameters:

  • cmd (Command::PlainCommand)

    command

  • args (Array)

    arbitrary objects, this is passed to process's block



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.

Yield Parameters:

  • val (Object)

    arbitrary value, this is passed from #execute



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