Class: BudgetBytesCli::ArraySelector

Inherits:
Object
  • Object
show all
Defined in:
lib/budget_bytes_cli/array-prompter.rb

Overview

ArraySelector selects from an array (helper class for ArrayPrompter)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(back_allowed = false) ⇒ ArraySelector

Returns a new instance of ArraySelector.



74
75
76
77
78
79
80
# 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

Instance Attribute Details

#array_to_selectObject

Returns the value of attribute array_to_select.



63
64
65
# File 'lib/budget_bytes_cli/array-prompter.rb', line 63

def array_to_select
  @array_to_select
end

#prompt_textObject

Returns the value of attribute prompt_text.



63
64
65
# File 'lib/budget_bytes_cli/array-prompter.rb', line 63

def prompt_text
  @prompt_text
end

Instance Method Details

#back_allowed=(value) ⇒ Object



65
66
67
68
69
70
71
72
# 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_inputObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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_itemObject



82
83
84
# File 'lib/budget_bytes_cli/array-prompter.rb', line 82

def last_item
    self.array_to_select.length
end

#prompt(text_prompt = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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