summaryrefslogtreecommitdiff
path: root/lib/did_you_mean/spell_checkers/key_error_checker.rb
diff options
context:
space:
mode:
authorKevin Deisz <kevin.deisz@gmail.com>2019-10-29 10:08:37 -0400
committerYuki Nishijima <yk.nishijima@gmail.com>2019-11-30 21:08:19 -0500
commit171803d5d34feb1b4244ca81b9db0a7bc2171c85 (patch)
tree664ee644da144f28152097fbe5ea43329bfc0576 /lib/did_you_mean/spell_checkers/key_error_checker.rb
parenta2fc6a51dd2e1a153559038795e1e2509f9c6a94 (diff)
Promote did_you_mean to default gem
At the moment, there are some problems with regard to bundler + did_you_mean because of did_you_mean being a bundled gem. Since the vendored version of thor inside bundler and ruby itself explicitly requires did_you_mean, it can become difficult to load it when using Bundler.setup. See this issue: https://github.com/yuki24/did_you_mean/issues/117#issuecomment-482733159 for more details.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2689
Diffstat (limited to 'lib/did_you_mean/spell_checkers/key_error_checker.rb')
-rw-r--r--lib/did_you_mean/spell_checkers/key_error_checker.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/did_you_mean/spell_checkers/key_error_checker.rb b/lib/did_you_mean/spell_checkers/key_error_checker.rb
new file mode 100644
index 0000000000..be4bea7789
--- /dev/null
+++ b/lib/did_you_mean/spell_checkers/key_error_checker.rb
@@ -0,0 +1,20 @@
+require_relative "../spell_checker"
+
+module DidYouMean
+ class KeyErrorChecker
+ def initialize(key_error)
+ @key = key_error.key
+ @keys = key_error.receiver.keys
+ end
+
+ def corrections
+ @corrections ||= exact_matches.empty? ? SpellChecker.new(dictionary: @keys).correct(@key).map(&:inspect) : exact_matches
+ end
+
+ private
+
+ def exact_matches
+ @exact_matches ||= @keys.select { |word| @key == word.to_s }.map(&:inspect)
+ end
+ end
+end