class BudgetBytesCli::ArraySelector
ArraySelector
selects from an array (helper class for ArrayPrompter)
Attributes
array_to_select[RW]
prompt_text[RW]
Public Class Methods
new(back_allowed = false)
click to toggle source
# File lib/budget_bytes_cli/array-prompter.rb, line 74 def initialize(back_allowed = false) @back_allowed = back_allowed @allowed_chars = ['Q'] if @back_allowed @allowed_chars << 'B' end end
Public Instance Methods
back_allowed=(value)
click to toggle source
# File lib/budget_bytes_cli/array-prompter.rb, line 65 def back_allowed= (value) @back_allowed = value if value @allowed_chars = ['Q', 'B'] else @allowed_chars = ['Q'] end end
get_input()
click to toggle source
# File lib/budget_bytes_cli/array-prompter.rb, line 102 def get_input input = "" valid_input = false #for corner case where only one item in array if array_to_select.length == 1 input = "1" valid_input = true end while !valid_input self.prompt input = gets.strip.upcase if @allowed_chars.include?(input) valid_input = true elsif input.to_i.to_s == input && input.to_i >= 1 && input.to_i <= self.last_item valid_input = true else puts "Invalid input, please try again." end end input end
last_item()
click to toggle source
# File lib/budget_bytes_cli/array-prompter.rb, line 82 def last_item self.array_to_select.length end
prompt(text_prompt = nil)
click to toggle source
# File lib/budget_bytes_cli/array-prompter.rb, line 86 def prompt(text_prompt = nil) if text_prompt self.prompt_text = text_prompt end puts self.prompt_text self.array_to_select.each_with_index do |menu_item, idx| puts "#{idx + 1}. #{menu_item}" end if @back_allowed puts "Or enter 'B' to go back to the previous menu" end puts "Or enter 'Q' to quit" end