pry-byetypo 1.3.5 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f15529f2e08377a72e5f53a9760054290f2d02c503fcb083bf4a302fc4431f5
4
- data.tar.gz: 3c79cf73afb8e8854828136769681f3f668ad5464da41c9da4659fbf3cb87493
3
+ metadata.gz: a33e074d9f5da4f065da3f5cdbf37807b1de2a0565d81a3bf89f0fd751286ee5
4
+ data.tar.gz: 59b098fd8d154d4315c931a9869b0feedcbed19e022ecb71f828b61913759d13
5
5
  SHA512:
6
- metadata.gz: 8054993c420a8c032d70f786d87564e2d9ea81a1a1393e35d68d4f2b64afdee3180b4661a8fb4b4f56fb9cc0c5d95e135321022a3b656978d1380e80ad66fe4f
7
- data.tar.gz: e75803de9ae6d6f644cc98551dddae9281d77219044882408e99a05c0bb3f14c45d6f69478e009907d231b0ad23614e561352c47057b3fb609acdfd958906b92
6
+ metadata.gz: 158849f7db42283374d60ed2cc3fee8c48443101870cbb03ba5e032ee5a0a467c07b0fa584afba5428cecc98cd4528113d66447a760c77ee539fe136e6f008ee
7
+ data.tar.gz: 5cf13d244cef3a3f8538ad21817bd179a67fbd2fb6170ff5c85e80f959f179c4a0ffa8eb38408006b862f727b381811d622a010cb7daed799cca4939c5d85a8f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pry-byetypo (1.3.5)
4
+ pry-byetypo (1.3.6)
5
5
  pry (~> 0.15)
6
6
 
7
7
  GEM
@@ -8,7 +8,7 @@ module Exceptions
8
8
  private
9
9
 
10
10
  def corrected_cmd
11
- @corrected_cmd ||= last_cmd.gsub(/\b#{unknown_from_exception}\b/, corrected_word)
11
+ @corrected_cmd ||= last_cmd.gsub(/\b#{unknown_from_exception}\b/, corrected_word.to_s)
12
12
  end
13
13
 
14
14
  def unknown_from_exception
@@ -8,7 +8,8 @@ module Exceptions
8
8
  private
9
9
 
10
10
  def unknown_from_exception
11
- exception.to_s.split.last
11
+ # Returns the first word after Unitialized constant error message.
12
+ exception.to_s.split[2]
12
13
  end
13
14
 
14
15
  def corrected_cmd
@@ -29,6 +29,8 @@ class ExceptionsBase < Base
29
29
  end
30
30
 
31
31
  def can_correct?
32
+ return true if did_you_mean_output
33
+
32
34
  error_from_typo? && dictionary && corrected_word
33
35
  end
34
36
 
@@ -37,7 +39,15 @@ class ExceptionsBase < Base
37
39
  end
38
40
 
39
41
  def corrected_word
40
- @corrected_word ||= spell_checker.correct(unknown_from_exception).first
42
+ @corrected_word ||= did_you_mean_output ||
43
+ spell_checker.correct(unknown_from_exception).first
44
+ end
45
+
46
+ def did_you_mean_output
47
+ match_data = exception.message.match(/Did you mean\?\s+(\w+)/)
48
+ return unless match_data
49
+
50
+ match_data[1]
41
51
  end
42
52
 
43
53
  def spell_checker
@@ -15,21 +15,45 @@ module Exceptions
15
15
  end
16
16
 
17
17
  def dictionary
18
- klass_methods = eval(infer_klass).methods # rubocop:disable Security/Eval
19
- # Early return because built_in class does not have access to `instance_methods`.
20
- return klass_methods.map(&:to_s) if built_in_klass
18
+ @dictionary ||= begin
19
+ klass_eval = eval(infer_klass) # rubocop:disable Security/Eval
20
+ klass_methods = klass_eval.methods
21
+ # Conditions below exist because built_in class does not have access to `instance_methods` or `column_names`. eg: []
22
+ instance_methods = klass_eval.respond_to?(:instance_methods) ? klass_eval.instance_methods(false) : []
23
+ column_names = klass_eval.respond_to?(:column_names) ? klass_eval.column_names : []
21
24
 
22
- instance_methods = eval(infer_klass).instance_methods(false) # rubocop:disable Security/Eval
23
- instance_methods.push(klass_methods).flatten.map(&:to_s)
25
+ [instance_methods, klass_methods, model_associations, column_names, active_records_methods].flatten.map(&:to_s)
26
+ end
27
+ end
28
+
29
+ def model_associations
30
+ store.transaction { |s| [s["associations"]] }
31
+ end
32
+
33
+ def active_records_methods
34
+ store.transaction { |s| [s["ar_methods"]] }
24
35
  end
25
36
 
26
37
  def exception_regexp
27
38
  /`([^']+)' for/
28
39
  end
29
40
 
41
+ def klass_regexp
42
+ return /for an instance of (\w+)/ if instance_exception?
43
+ return /for class (\w+)/ if exception.to_s.include?("class")
44
+
45
+ /for #<(\w+)/
46
+ end
47
+
30
48
  def klass
31
- exception_without_class_module = exception.to_s.gsub(":Class", "")
32
- exception_without_class_module.split.last
49
+ @klass ||= begin
50
+ exception_without_class_module = exception.to_s.match(klass_regexp)
51
+ exception_without_class_module[1]
52
+ end
53
+ end
54
+
55
+ def instance_exception?
56
+ exception.to_s.include?("for an instance of")
33
57
  end
34
58
 
35
59
  # [].methods and Array.methods have a different output.
@@ -5,6 +5,7 @@ require_relative "../checks/database_pool"
5
5
  require_relative "../store"
6
6
 
7
7
  require "pstore"
8
+ require "zeitwerk"
8
9
 
9
10
  module Setup
10
11
  module Dictionary
@@ -25,6 +26,7 @@ module Setup
25
26
 
26
27
  store["active_record_models"] = populate_active_record_models_dictionary
27
28
  store["associations"] = populate_associations
29
+ store["ar_methods"] = ::ActiveRecord::Base.methods
28
30
  store["synced_at"] = Time.now
29
31
  end
30
32
  end
@@ -36,13 +38,15 @@ module Setup
36
38
 
37
39
  def populate_active_record_models_dictionary
38
40
  Zeitwerk::Loader.eager_load_all
39
- ::ActiveRecord::Base.descendants.map { |model| format_active_record_model(model) }.flatten
41
+ models.map { |model| format_active_record_model(model) }.flatten
40
42
  end
41
43
 
42
44
  def populate_associations
43
- table_names = ::ActiveRecord::Base.connection.tables
44
- singularize_table_names = table_names.map { |a| a.chop }
45
- table_names.zip(singularize_table_names).flatten
45
+ models.map { |model| model.reflect_on_all_associations.map(&:name) }.flatten.uniq
46
+ end
47
+
48
+ def models
49
+ @models ||= ::ActiveRecord::Base.descendants
46
50
  end
47
51
 
48
52
  # This method takes an ActiveRecord model as an argument and formats its name and module information.
@@ -2,6 +2,6 @@
2
2
 
3
3
  class Pry
4
4
  module Byetypo
5
- VERSION = "1.3.5"
5
+ VERSION = "1.3.6"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry-byetypo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.5
4
+ version: 1.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clément Morisset
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-28 00:00:00.000000000 Z
11
+ date: 2025-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  requirements: []
105
- rubygems_version: 3.5.3
105
+ rubygems_version: 3.4.1
106
106
  signing_key:
107
107
  specification_version: 4
108
108
  summary: Autocorrects typos in your Pry console