Class: Tomo::Error::Suggestions

Inherits:
Object
  • Object
show all
Defined in:
lib/tomo/error/suggestions.rb

Instance Method Summary collapse

Constructor Details

#initialize(dictionary:, word:) ⇒ Suggestions

Returns a new instance of Suggestions.



6
7
8
9
# File 'lib/tomo/error/suggestions.rb', line 6

def initialize(dictionary:, word:)
  @dictionary = dictionary
  @word = word
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/tomo/error/suggestions.rb', line 11

def any?
  to_a.any?
end

#to_aObject



15
16
17
18
19
20
21
22
23
# File 'lib/tomo/error/suggestions.rb', line 15

def to_a
  @_suggestions ||= if defined?(DidYouMean::SpellChecker)
                      checker = DidYouMean::SpellChecker.new(dictionary:)
                      suggestions = checker.correct(word)
                      suggestions || []
                    else
                      []
                    end
end

#to_consoleObject



25
26
27
28
29
30
# File 'lib/tomo/error/suggestions.rb', line 25

def to_console
  return unless any?

  sentence = to_sentence(to_a.map { |word| Colors.blue(word) })
  "\nDid you mean #{sentence}?\n"
end