Class: BudgetBytesCli::ArrayPrompter

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

Overview

ArrayPrompter uses an ArraySelector for blocks of 20 (if needed) and an ArraySelector for the block of 20 chosen, returns the overall selection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt_text = "") ⇒ ArrayPrompter

Returns a new instance of ArrayPrompter.



6
7
8
9
10
# File 'lib/budget_bytes_cli/array-prompter.rb', line 6

def initialize(prompt_text = "")
    @block_selector = BudgetBytesCli::ArraySelector.new
    @item_selector = BudgetBytesCli::ArraySelector.new(true)
    @prompt_text = prompt_text
end

Instance Attribute Details

#array_to_selectObject

Returns the value of attribute array_to_select.



4
5
6
# File 'lib/budget_bytes_cli/array-prompter.rb', line 4

def array_to_select
  @array_to_select
end

#prompt_textObject

Returns the value of attribute prompt_text.



4
5
6
# File 'lib/budget_bytes_cli/array-prompter.rb', line 4

def prompt_text
  @prompt_text
end

Instance Method Details

#get_inputObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/budget_bytes_cli/array-prompter.rb', line 12

def get_input
    num_blocks = (self.array_to_select.length.to_f / 20.to_f).ceil
    
    #makes sure variable is outside if statement scope
    input_selected = 1

    @block_selector.prompt_text = self.prompt_text + "\nPlease select a range below"
        
    #create array for block selector
    @block_selector.array_to_select = []
    (1..num_blocks).each do |n|
        to_add = nil
        if ((n-1) * 20 + 1) == self.array_to_select.length
            to_add = "#{(n-1) * 20 + 1}"
        else
            to_add = "#{(n - 1) * 20 + 1}-#{[(n * 20), self.array_to_select.length].min}"
        end
            
        @block_selector.array_to_select << to_add
    end
    
    #fixes issue with back being displayed in item selector when only one block
    if @block_selector.array_to_select.length == 1
        @item_selector.back_allowed = false
    end
    
    input_selected = @block_selector.get_input
    
    if input_selected != 'Q'
        #Gets input using item_selector ArraySelector
        selected_value = input_selected.to_i
        @item_selector.prompt_text = self.prompt_text + "\nPlease select an item below."
        block_min = (selected_value - 1) * 20
        block_max = [(selected_value * 20) - 1, self.array_to_select.length].min
        @item_selector.array_to_select = self.array_to_select[block_min..block_max]
        second_selection = @item_selector.get_input
        if second_selection == 'Q'
            input_selected = second_selection
        elsif second_selection == 'B'
            #call function recursively to go back to prior input
            input_selected = self.get_input
        else
            input_selected = (second_selection.to_i + (selected_value - 1) * 20).to_s
        end
    end
    input_selected
end