Class: Menu::Base

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

Overview

Menu main class

Direct Known Subclasses

Config, Main, Scheduled

Instance Method Summary collapse

Constructor Details

#initialize(menu, input_map = {}, procs_map = {}) ⇒ Base

Initialize a new menu

Parameters:

  • menu (Slop:Result)

    A slop menu instance.

  • input_map (Hash) (defaults to: {})

    A hash that includes the command line arguments.

  • procs_map (Hash) (defaults to: {})

    A hash that includes procs that will be executed for each command.



26
27
28
29
30
31
# File 'lib/menu/base.rb', line 26

def initialize(menu, input_map = {}, procs_map = {})
  self.menu = menu

  run_selected_command(create_input_map.merge(input_map),
                       create_procs_map.merge(procs_map))
end

Instance Method Details

#create_input_mapObject



35
# File 'lib/menu/base.rb', line 35

def create_input_map; end

#create_procs_mapObject



33
# File 'lib/menu/base.rb', line 33

def create_procs_map; end

#run_defaultObject

Print help



49
50
51
# File 'lib/menu/base.rb', line 49

def run_default
  puts menu
end

#run_selected_command(input, procs) ⇒ Object

Run user's selected command



38
39
40
41
42
43
44
45
46
# File 'lib/menu/base.rb', line 38

def run_selected_command(input, procs)
  selected_key = input.keys.select { |key| input[key] }.first
  if selected_key.nil?
    run_default
    return
  end

  procs[selected_key].call
end