Class: Ukiryu::Action::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/ukiryu/action/base.rb

Overview

This class is abstract.

Abstract base class for action classes

Action classes represent executable commands and provide a fluent interface for building and executing commands.

This is an alternative pattern to using options objects directly.

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool_instance = nil) ⇒ Base

Create a new action

Parameters:

  • tool_instance (Tools::Base) (defaults to: nil)

    the tool instance to use



34
35
36
# File 'lib/ukiryu/action/base.rb', line 34

def initialize(tool_instance = nil)
  @tool = tool_instance || self.class.tool_class.new
end

Class Attribute Details

.command_defHash (readonly)

Get the command definition

Returns:

  • (Hash)

    the command definition



23
24
25
# File 'lib/ukiryu/action/base.rb', line 23

def command_def
  @command_def
end

.command_nameSymbol (readonly)

Get the command name

Returns:

  • (Symbol)

    the command name



18
19
20
# File 'lib/ukiryu/action/base.rb', line 18

def command_name
  @command_name
end

.tool_classClass (readonly)

Get the associated tool class

Returns:

  • (Class)

    the tool class



28
29
30
# File 'lib/ukiryu/action/base.rb', line 28

def tool_class
  @tool_class
end

Instance Method Details

#command_defHash

Get the command definition

Returns:

  • (Hash)

    the command definition



73
74
75
# File 'lib/ukiryu/action/base.rb', line 73

def command_def
  self.class.command_def
end

#command_nameSymbol

Get the command name

Returns:

  • (Symbol)

    the command name



66
67
68
# File 'lib/ukiryu/action/base.rb', line 66

def command_name
  self.class.command_name
end

#optionsOptions::Base

Create an options object for this action

Returns:



59
60
61
# File 'lib/ukiryu/action/base.rb', line 59

def options
  @tool.options_class_for(self.class.command_name).new
end

#run(options = {}, timeout:) ⇒ Response::Base

Execute this action with options

Parameters:

  • options (Hash, Options::Base) (defaults to: {})

    the options to use

  • timeout (Integer)

    timeout in seconds (required)

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ukiryu/action/base.rb', line 43

def run(options = {}, timeout:)
  # If options is a hash, create an options object
  if options.is_a?(Hash)
    options_class = @tool.options_class_for(self.class.command_name)
    options_obj = options_class.new
    options_obj.set(options)
    options = options_obj
  end

  # Execute with the tool
  @tool.execute(self.class.command_name, options, timeout: timeout)
end