Class: Eskimo::ASCII::DidYouMean

Inherits:
Component show all
Defined in:
gems/eskimo-ascii/lib/eskimo/ascii/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



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

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



14
15
16
# File 'gems/eskimo-ascii/lib/eskimo/ascii/components/did_you_mean.rb', line 14

def corrections
  @corrections
end

#separatorObject (readonly)

Returns the value of attribute separator



14
15
16
# File 'gems/eskimo-ascii/lib/eskimo/ascii/components/did_you_mean.rb', line 14

def separator
  @separator
end

Instance Method Details

#renderObject



26
27
28
29
30
# File 'gems/eskimo-ascii/lib/eskimo/ascii/components/did_you_mean.rb', line 26

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