Class: Rhymera::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/rhymera/menu.rb

Overview

menu handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMenu

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

#functionObject

Returns the value of attribute function.



7
8
9
# File 'lib/rhymera/menu.rb', line 7

def function
  @function
end

#listObject

Returns the value of attribute list.



7
8
9
# File 'lib/rhymera/menu.rb', line 7

def list
  @list
end

#promptObject (readonly)

Returns the value of attribute prompt.



8
9
10
# File 'lib/rhymera/menu.rb', line 8

def prompt
  @prompt
end

#resultObject

Returns the value of attribute result.



7
8
9
# File 'lib/rhymera/menu.rb', line 7

def result
  @result
end

#wordObject

Returns the value of attribute word.



7
8
9
# File 'lib/rhymera/menu.rb', line 7

def word
  @word
end

Instance Method Details

#callObject



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)
  menu = extra_menu_entries
  # 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)
    menu << { "#{arg.capitalize}: #{word}" => word }
  end
  opts = prompt.select("More details on #{@word}:", menu)

  @word = opts
  prompt.select("More details on #{@word}:", extra_menu_entries) while @word == opts
end

#display_resultsObject



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 = extra_menu_entries
  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_entriesObject



52
53
54
55
56
57
58
59
# File 'lib/rhymera/menu.rb', line 52

def extra_menu_entries
  # 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_searchesObject



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_typeObject



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