Class: Menuboy::Menu

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/menuboy.rb

Constant Summary collapse

CTRL_C =
"\u0003"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL

#mainmenu, #option, #submenu

Constructor Details

#initialize(name) ⇒ Menu

Returns a new instance of Menu.



110
111
112
113
# File 'lib/menuboy.rb', line 110

def initialize name
  self.name = name
  self.options = []
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



106
107
108
# File 'lib/menuboy.rb', line 106

def name
  @name
end

#optionsObject

Returns the value of attribute options.



106
107
108
# File 'lib/menuboy.rb', line 106

def options
  @options
end

Instance Method Details

#handle_input(k) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/menuboy.rb', line 129

def handle_input k
  if k == "h"
    print_help
  elsif k == "p" && defined? Pry
    Menuboy.fix_stdin do
      binding.pry
    end
  elsif option = option_match?(k)
    option.proc.call
  else
    puts "Unassigned input"
  end
end

#option_match?(input) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
# File 'lib/menuboy.rb', line 124

def option_match? input
  matches = self.options.select{|o| o.key == input}
  matches.length == 1 ? matches.last : false
end


115
116
117
118
119
120
121
122
# File 'lib/menuboy.rb', line 115

def print_help
  self.options.each do |opt|
    puts "#{opt.key} - #{opt.name}"
  end
  puts "p - jump into a Pry repl" if defined? Pry
  puts "h - print help"
  puts "q - quit / go back"
end

#promptObject



143
144
145
# File 'lib/menuboy.rb', line 143

def prompt
  print "(#{@name})> "
end