Class: TTY::MenuTree::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/menu_tree/action.rb

Overview

Base class for anything that can appear in a menu.

An action is any object that responds to the four methods below. You do not have to subclass Action; duck typing is enough. Subclassing simply gives you sensible defaults (#submenu => nil, #exit? => false) and NotImplementedError guards for the parts you must define.

Examples:

A leaf action

class SayHello < TTY::MenuTree::Action
  def name = "Say hello"
  def call = puts("Hello!")
end

Direct Known Subclasses

ExitAction, Group

Instance Method Summary collapse

Instance Method Details

#call ⇒ void

This method returns an undefined value.

Invoked when a leaf action is selected.

Not called for actions that return a #submenu; those are descended into instead.

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/tty/menu_tree/action.rb', line 29

def call
  raise NotImplementedError, "#{self.class}#call must be implemented"
end

#exit? ⇒ Boolean

Returns when true, selecting this action ends the top-level menu loop.

Returns:

  • (Boolean) —

    when true, selecting this action ends the top-level menu loop.



41
42
43
# File 'lib/tty/menu_tree/action.rb', line 41

def exit?
  false
end

#name ⇒ String

Returns the label shown for this action in a menu.

Returns:

  • (String) —

    the label shown for this action in a menu.

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/tty/menu_tree/action.rb', line 19

def name
  raise NotImplementedError, "#{self.class}#name must be implemented"
end

Returns child actions to descend into, or nil for a leaf action.

Returns:

  • (Array<#name>, nil) —

    child actions to descend into, or nil for a leaf action.



35
36
37
# File 'lib/tty/menu_tree/action.rb', line 35

def submenu
  nil
end