class BudgetBytesCli::ArrayPrompter

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

Attributes

array_to_select[RW]
prompt_text[RW]

Public Class Methods

new(prompt_text = "") click to toggle source
# 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

Public Instance Methods

get_input() click to toggle source
# 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