Class: Mysh::ActionPool

Inherits:
Object show all
Defined in:
lib/mysh/internal/action_pool.rb

Overview

  • A managed hash of mysh actions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool_name, default_action = nil) ⇒ ActionPool

Create a new action pool



13
14
15
16
# File 'lib/mysh/internal/action_pool.rb', line 13

def initialize(pool_name, default_action = nil)
  @pool_name, @pool = pool_name, {}
  @pool.default = default_action
end

Instance Attribute Details

#pool_nameObject (readonly)

The name of this action pool.



10
11
12
# File 'lib/mysh/internal/action_pool.rb', line 10

def pool_name
  @pool_name
end

Instance Method Details

#[](index) ⇒ Object

Get a action.



19
20
21
# File 'lib/mysh/internal/action_pool.rb', line 19

def [](index)
  @pool[index]
end

#actions_infoObject

Get information on all actions.



40
41
42
43
44
45
# File 'lib/mysh/internal/action_pool.rb', line 40

def actions_info
  @pool
    .values
    .map  {|action| action.action_info}
    .sort {|first, second| first[0] <=> second[0]}
end

#add_action(action) ⇒ Object

Add an action to the pool.



29
30
31
32
33
34
35
36
37
# File 'lib/mysh/internal/action_pool.rb', line 29

def add_action(action)
  short_name = action.short_name

  if @pool.key?(short_name)
    fail "Add error: Action #{short_name.inspect} already exists in #{pool_name}."
  end

  @pool[short_name] = action
end

#exists?(index) ⇒ Boolean

Does this action exist?

Returns:

  • (Boolean)


24
25
26
# File 'lib/mysh/internal/action_pool.rb', line 24

def exists?(index)
  @pool.has_key?(index)
end