Class: Menu::SubselectMenu

Inherits:
Base
  • Object
show all
Includes:
Listenable
Defined in:
lib/menu/subselect_menu.rb

Overview

menu class that inherits Base to abstract a menu which is used to query a subselect on an index

Instance Attribute Summary collapse

Attributes inherited from Base

#menu_description, #menu_items

Instance Method Summary collapse

Methods included from Listenable

#add_listener, #listeners, #notify_listeners, #remove_listener

Methods inherited from Base

#add_menu_item, #get_entry, #handle_wrong_option, #print_menu

Constructor Details

#initialize(ranking) ⇒ SubselectMenu

initialization as (attribute => occurrence)

Parameters:

  • ranking (Array)

    the sorted n result with the highest ranking mapped



16
17
18
19
20
# File 'lib/menu/subselect_menu.rb', line 16

def initialize(ranking)
  @menu_description = "Overview of the top n index results:"
  @ranking = ranking
  super
end

Instance Attribute Details

#criteriaSymbol (readonly, private)

Returns criteria the ranking criteria.

Returns:

  • (Symbol)

    criteria the ranking criteria



33
34
35
# File 'lib/menu/subselect_menu.rb', line 33

def criteria
  @criteria
end

#rankingArray (readonly, private)

Returns an array that holds arrays of size two with [value, rank].

Returns:

  • (Array)

    an array that holds arrays of size two with [value, rank]



31
32
33
# File 'lib/menu/subselect_menu.rb', line 31

def ranking
  @ranking
end

Instance Method Details

#change_index(criteria) ⇒ Object

method to replace the current criteria

Parameters:

  • criteria (Symbol)

    the ranking criteria



24
25
26
# File 'lib/menu/subselect_menu.rb', line 24

def change_index(criteria)
  @criteria = criteria
end

#define_menu_itemsObject (private)

implementation to define the items of the menu



36
37
38
39
40
41
42
# File 'lib/menu/subselect_menu.rb', line 36

def define_menu_items
  @ranking.each { |entry|
    text = "#{entry[0]} [#{entry[1]}]"
    add_menu_item(text)
  }
  add_menu_item("Return to previous menu.")
end

#determine_action(input) ⇒ Object (private)

method to specify the actions to a given input

Parameters:

  • input (String)

    the user input



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/menu/subselect_menu.rb', line 46

def determine_action(input)
  input_number = input.to_i
  case
    when (input_number == @menu_items.size) then return false
    when (input.to_i > @menu_items.size) || (input.to_i < 1)
      puts "Invalid input. Please try again."
      return false
    else
      index_menu = Menu::IndexSelectionMenu.new()
      index_menu.add_listener(:subselect_menu, self)
      index_menu.print_menu
      notify_listeners(:generate_subselect, @ranking[input.to_i - 1][0],
                                            @criteria)
  end
end