Class: Eskimo::Components::DidYouMean

Inherits:
Eskimo::Component show all
Defined in:
lib/eskimo/components/did_you_mean.rb

Overview

Present the user with the closest possible correction, if any.

DidYouMean.new(dictionary: [ 'abc', 'bca' ], item: 'abd')
# => "hint: Did you mean? abc"

DidYouMean.new(dictionary: [ 'abc', 'bca' ], item: 'asdfasdf')
# => ""

See github.com/yuki24/did_you_mean#using-the-didyoumeanspellchecker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary:, item:, separator: " or ", &children) ⇒ DidYouMean

Returns a new instance of DidYouMean



17
18
19
20
21
22
23
24
25
# File 'lib/eskimo/components/did_you_mean.rb', line 17

def initialize(dictionary:, item:, separator: " or ", &children)
  @corrections = ::DidYouMean::SpellChecker.new(
    dictionary: dictionary
  ).correct(item)

  @separator = separator

  super
end

Instance Attribute Details

#correctionsObject (readonly)

Returns the value of attribute corrections



15
16
17
# File 'lib/eskimo/components/did_you_mean.rb', line 15

def corrections
  @corrections
end

#separatorObject (readonly)

Returns the value of attribute separator



15
16
17
# File 'lib/eskimo/components/did_you_mean.rb', line 15

def separator
  @separator
end

Instance Method Details

#renderObject



27
28
29
30
31
# File 'lib/eskimo/components/did_you_mean.rb', line 27

def render(**)
  if corrections.any?
    "Did you mean? #{corrections.join(separator)}"
  end
end