lib/did_you_mean/spell_checkers/key_error_checker.rb
raw| 1 | require_relative "../spell_checker" |
| 2 | |
| 3 | module DidYouMean |
| 4 | class KeyErrorChecker |
| 5 | def initialize(key_error) |
| 6 | @key = key_error.key |
| 7 | @keys = key_error.receiver.keys |
| 8 | end |
| 9 | |
| 10 | def corrections |
| 11 | @corrections ||= exact_matches.empty? ? SpellChecker.new(dictionary: @keys).correct(@key).map(&:inspect) : exact_matches |
| 12 | end |
| 13 | |
| 14 | private |
| 15 | |
| 16 | def exact_matches |
| 17 | @exact_matches ||= @keys.select { |word| @key == word.to_s }.map(&:inspect) |
| 18 | end |
| 19 | end |
| 20 | end |