Class: Rhymera::Menu
- Inherits:
-
Object
- Object
- Rhymera::Menu
- Defined in:
- lib/rhymera/menu.rb
Overview
menu handler
Instance Attribute Summary collapse
-
#function ⇒ Object
Returns the value of attribute function.
-
#list ⇒ Object
Returns the value of attribute list.
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
-
#result ⇒ Object
Returns the value of attribute result.
-
#word ⇒ Object
Returns the value of attribute word.
Instance Method Summary collapse
- #call ⇒ Object
- #detail_view(object) ⇒ Object
- #display_results ⇒ Object
- #extra_menu_entries ⇒ Object
-
#initialize ⇒ Menu
constructor
A new instance of Menu.
- #old_searches ⇒ Object
- #search(term) ⇒ Object
- #search_type ⇒ Object
Constructor Details
#initialize ⇒ Menu
Returns a new instance of Menu.
10 11 12 13 14 |
# File 'lib/rhymera/menu.rb', line 10 def initialize @prompt = TTY::Prompt.new # print once on intialization puts 'Welcome to Rhymera.' end |
Instance Attribute Details
#function ⇒ Object
Returns the value of attribute function.
7 8 9 |
# File 'lib/rhymera/menu.rb', line 7 def function @function end |
#list ⇒ Object
Returns the value of attribute list.
7 8 9 |
# File 'lib/rhymera/menu.rb', line 7 def list @list end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
8 9 10 |
# File 'lib/rhymera/menu.rb', line 8 def prompt @prompt end |
#result ⇒ Object
Returns the value of attribute result.
7 8 9 |
# File 'lib/rhymera/menu.rb', line 7 def result @result end |
#word ⇒ Object
Returns the value of attribute word.
7 8 9 |
# File 'lib/rhymera/menu.rb', line 7 def word @word end |
Instance Method Details
#call ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/rhymera/menu.rb', line 16 def call @word = prompt.ask("Please enter a search term or :q to quit.\n>") call if @word.nil? # vim-style quit, seems best exit(0) if @word == ':q' search(@word) end |
#detail_view(object) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rhymera/menu.rb', line 61 def detail_view(object) = # iterate through attrs of a given Rhyme or Portmanteau for display object.instance_variables.each do |var| arg = var.to_s.gsub('@', '') word = object.send(arg) << { "#{arg.capitalize}: #{word}" => word } end opts = prompt.select("More details on #{@word}:", ) @word = opts prompt.select("More details on #{@word}:", ) while @word == opts end |
#display_results ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rhymera/menu.rb', line 37 def display_results # can be called both by search and previous entries results = results << @list.entries.map { |ent| { ent.word.to_s => ent } } result = prompt.select("#{@function} for '#{@word}' and other options:", results) @word = if result.instance_of?(Rhymera::Rhyme) || result.instance_of?(Rhymera::Portmanteau) result.word else result end detail_view(result) end |
#extra_menu_entries ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/rhymera/menu.rb', line 52 def # options leading to lambas! we love lambdas, don't we folks [{ "Copy '#{@word}' to Clipboard" => -> { Clipboard.copy(@word) } }, { "Search '#{@word}'" => -> { search(@word) } }, { 'New Search' => -> { call } }, { 'Previous Searches' => -> { old_searches } }, { 'Quit Program' => -> { exit(0) } }] end |
#old_searches ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rhymera/menu.rb', line 75 def old_searches # here's one of the ol' class methods lists = Rhymera::List.all.map do |list| { "#{list.type[3..]} for #{list.query}" => list } end selection = prompt.select('Previous searches:', lists) @list = selection @function = selection.type[3..] @word = selection.query display_results end |
#search(term) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/rhymera/menu.rb', line 24 def search(term) # can be called again and again! @word = term @function = search_type @list = Rhymera::List.new(word: @word, function: "get#{@function}") display_results end |
#search_type ⇒ Object
32 33 34 35 |
# File 'lib/rhymera/menu.rb', line 32 def search_type prompt.select('Which type of result are you looking for?', %w[Rhymes Portmanteaus]) end |