pry-byetypo 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d29897c99a83e87e7e440a1c4d70152362c59b56909271441ad8ee64cdfc0dda
4
+ data.tar.gz: e56b51093055e7374b10b83c4ab5694f51a46e6fa7d7bc49db4f883ee1e5238a
5
+ SHA512:
6
+ metadata.gz: e0196f077acc833eb924f2d112bb88e052a2b8386ac1166d3f155b40db286a2afd99edfd2d81c5dce720d0dbb494902fb7ab0da185463b7926cd3aba0e446a0d
7
+ data.tar.gz: e3c93cf18c7e5af20557d60d4760d2c7305b3b986c073ec1fc643c5be991e7d54ec8cc36ee9ee9f3a393865f15207bce3292e726a4496f1972cfe71357efe077
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-07-17
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at clement.morisset@potloc.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in pry-byetypo.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "standard", "~> 1.3"
13
+
14
+ gem "byebug"
data/Gemfile.lock ADDED
@@ -0,0 +1,261 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pry-byetypo (1.0.0)
5
+ colorize (~> 1.1.0)
6
+ pry (>= 0.13, < 0.15)
7
+ rails (~> 7.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actioncable (7.1.3)
13
+ actionpack (= 7.1.3)
14
+ activesupport (= 7.1.3)
15
+ nio4r (~> 2.0)
16
+ websocket-driver (>= 0.6.1)
17
+ zeitwerk (~> 2.6)
18
+ actionmailbox (7.1.3)
19
+ actionpack (= 7.1.3)
20
+ activejob (= 7.1.3)
21
+ activerecord (= 7.1.3)
22
+ activestorage (= 7.1.3)
23
+ activesupport (= 7.1.3)
24
+ mail (>= 2.7.1)
25
+ net-imap
26
+ net-pop
27
+ net-smtp
28
+ actionmailer (7.1.3)
29
+ actionpack (= 7.1.3)
30
+ actionview (= 7.1.3)
31
+ activejob (= 7.1.3)
32
+ activesupport (= 7.1.3)
33
+ mail (~> 2.5, >= 2.5.4)
34
+ net-imap
35
+ net-pop
36
+ net-smtp
37
+ rails-dom-testing (~> 2.2)
38
+ actionpack (7.1.3)
39
+ actionview (= 7.1.3)
40
+ activesupport (= 7.1.3)
41
+ nokogiri (>= 1.8.5)
42
+ racc
43
+ rack (>= 2.2.4)
44
+ rack-session (>= 1.0.1)
45
+ rack-test (>= 0.6.3)
46
+ rails-dom-testing (~> 2.2)
47
+ rails-html-sanitizer (~> 1.6)
48
+ actiontext (7.1.3)
49
+ actionpack (= 7.1.3)
50
+ activerecord (= 7.1.3)
51
+ activestorage (= 7.1.3)
52
+ activesupport (= 7.1.3)
53
+ globalid (>= 0.6.0)
54
+ nokogiri (>= 1.8.5)
55
+ actionview (7.1.3)
56
+ activesupport (= 7.1.3)
57
+ builder (~> 3.1)
58
+ erubi (~> 1.11)
59
+ rails-dom-testing (~> 2.2)
60
+ rails-html-sanitizer (~> 1.6)
61
+ activejob (7.1.3)
62
+ activesupport (= 7.1.3)
63
+ globalid (>= 0.3.6)
64
+ activemodel (7.1.3)
65
+ activesupport (= 7.1.3)
66
+ activerecord (7.1.3)
67
+ activemodel (= 7.1.3)
68
+ activesupport (= 7.1.3)
69
+ timeout (>= 0.4.0)
70
+ activestorage (7.1.3)
71
+ actionpack (= 7.1.3)
72
+ activejob (= 7.1.3)
73
+ activerecord (= 7.1.3)
74
+ activesupport (= 7.1.3)
75
+ marcel (~> 1.0)
76
+ activesupport (7.1.3)
77
+ base64
78
+ bigdecimal
79
+ concurrent-ruby (~> 1.0, >= 1.0.2)
80
+ connection_pool (>= 2.2.5)
81
+ drb
82
+ i18n (>= 1.6, < 2)
83
+ minitest (>= 5.1)
84
+ mutex_m
85
+ tzinfo (~> 2.0)
86
+ ast (2.4.2)
87
+ base64 (0.2.0)
88
+ bigdecimal (3.1.6)
89
+ builder (3.2.4)
90
+ byebug (11.1.3)
91
+ coderay (1.1.3)
92
+ colorize (1.1.0)
93
+ concurrent-ruby (1.2.3)
94
+ connection_pool (2.4.1)
95
+ crass (1.0.6)
96
+ date (3.3.4)
97
+ diff-lcs (1.5.0)
98
+ drb (2.2.0)
99
+ ruby2_keywords
100
+ erubi (1.12.0)
101
+ globalid (1.2.1)
102
+ activesupport (>= 6.1)
103
+ i18n (1.14.1)
104
+ concurrent-ruby (~> 1.0)
105
+ io-console (0.7.2)
106
+ irb (1.11.1)
107
+ rdoc
108
+ reline (>= 0.4.2)
109
+ json (2.7.1)
110
+ language_server-protocol (3.17.0.3)
111
+ lint_roller (1.1.0)
112
+ loofah (2.22.0)
113
+ crass (~> 1.0.2)
114
+ nokogiri (>= 1.12.0)
115
+ mail (2.8.1)
116
+ mini_mime (>= 0.1.1)
117
+ net-imap
118
+ net-pop
119
+ net-smtp
120
+ marcel (1.0.2)
121
+ method_source (1.0.0)
122
+ mini_mime (1.1.5)
123
+ minitest (5.21.2)
124
+ mutex_m (0.2.0)
125
+ net-imap (0.4.9.1)
126
+ date
127
+ net-protocol
128
+ net-pop (0.1.2)
129
+ net-protocol
130
+ net-protocol (0.2.2)
131
+ timeout
132
+ net-smtp (0.4.0.1)
133
+ net-protocol
134
+ nio4r (2.7.0)
135
+ nokogiri (1.16.0-arm64-darwin)
136
+ racc (~> 1.4)
137
+ nokogiri (1.16.0-x86_64-linux)
138
+ racc (~> 1.4)
139
+ parallel (1.23.0)
140
+ parser (3.2.2.4)
141
+ ast (~> 2.4.1)
142
+ racc
143
+ pry (0.14.2)
144
+ coderay (~> 1.1)
145
+ method_source (~> 1.0)
146
+ psych (5.1.2)
147
+ stringio
148
+ racc (1.7.3)
149
+ rack (3.0.8)
150
+ rack-session (2.0.0)
151
+ rack (>= 3.0.0)
152
+ rack-test (2.1.0)
153
+ rack (>= 1.3)
154
+ rackup (2.1.0)
155
+ rack (>= 3)
156
+ webrick (~> 1.8)
157
+ rails (7.1.3)
158
+ actioncable (= 7.1.3)
159
+ actionmailbox (= 7.1.3)
160
+ actionmailer (= 7.1.3)
161
+ actionpack (= 7.1.3)
162
+ actiontext (= 7.1.3)
163
+ actionview (= 7.1.3)
164
+ activejob (= 7.1.3)
165
+ activemodel (= 7.1.3)
166
+ activerecord (= 7.1.3)
167
+ activestorage (= 7.1.3)
168
+ activesupport (= 7.1.3)
169
+ bundler (>= 1.15.0)
170
+ railties (= 7.1.3)
171
+ rails-dom-testing (2.2.0)
172
+ activesupport (>= 5.0.0)
173
+ minitest
174
+ nokogiri (>= 1.6)
175
+ rails-html-sanitizer (1.6.0)
176
+ loofah (~> 2.21)
177
+ nokogiri (~> 1.14)
178
+ railties (7.1.3)
179
+ actionpack (= 7.1.3)
180
+ activesupport (= 7.1.3)
181
+ irb
182
+ rackup (>= 1.0.0)
183
+ rake (>= 12.2)
184
+ thor (~> 1.0, >= 1.2.2)
185
+ zeitwerk (~> 2.6)
186
+ rainbow (3.1.1)
187
+ rake (13.1.0)
188
+ rdoc (6.6.2)
189
+ psych (>= 4.0.0)
190
+ regexp_parser (2.8.3)
191
+ reline (0.4.2)
192
+ io-console (~> 0.5)
193
+ rexml (3.2.6)
194
+ rspec (3.12.0)
195
+ rspec-core (~> 3.12.0)
196
+ rspec-expectations (~> 3.12.0)
197
+ rspec-mocks (~> 3.12.0)
198
+ rspec-core (3.12.2)
199
+ rspec-support (~> 3.12.0)
200
+ rspec-expectations (3.12.3)
201
+ diff-lcs (>= 1.2.0, < 2.0)
202
+ rspec-support (~> 3.12.0)
203
+ rspec-mocks (3.12.6)
204
+ diff-lcs (>= 1.2.0, < 2.0)
205
+ rspec-support (~> 3.12.0)
206
+ rspec-support (3.12.1)
207
+ rubocop (1.57.2)
208
+ json (~> 2.3)
209
+ language_server-protocol (>= 3.17.0)
210
+ parallel (~> 1.10)
211
+ parser (>= 3.2.2.4)
212
+ rainbow (>= 2.2.2, < 4.0)
213
+ regexp_parser (>= 1.8, < 3.0)
214
+ rexml (>= 3.2.5, < 4.0)
215
+ rubocop-ast (>= 1.28.1, < 2.0)
216
+ ruby-progressbar (~> 1.7)
217
+ unicode-display_width (>= 2.4.0, < 3.0)
218
+ rubocop-ast (1.30.0)
219
+ parser (>= 3.2.1.0)
220
+ rubocop-performance (1.19.1)
221
+ rubocop (>= 1.7.0, < 2.0)
222
+ rubocop-ast (>= 0.4.0)
223
+ ruby-progressbar (1.13.0)
224
+ ruby2_keywords (0.0.5)
225
+ standard (1.32.1)
226
+ language_server-protocol (~> 3.17.0.2)
227
+ lint_roller (~> 1.0)
228
+ rubocop (~> 1.57.2)
229
+ standard-custom (~> 1.0.0)
230
+ standard-performance (~> 1.2)
231
+ standard-custom (1.0.2)
232
+ lint_roller (~> 1.0)
233
+ rubocop (~> 1.50)
234
+ standard-performance (1.2.1)
235
+ lint_roller (~> 1.1)
236
+ rubocop-performance (~> 1.19.1)
237
+ stringio (3.1.0)
238
+ thor (1.3.0)
239
+ timeout (0.4.1)
240
+ tzinfo (2.0.6)
241
+ concurrent-ruby (~> 1.0)
242
+ unicode-display_width (2.5.0)
243
+ webrick (1.8.1)
244
+ websocket-driver (0.7.6)
245
+ websocket-extensions (>= 0.1.0)
246
+ websocket-extensions (0.1.5)
247
+ zeitwerk (2.6.12)
248
+
249
+ PLATFORMS
250
+ arm64-darwin-22
251
+ x86_64-linux
252
+
253
+ DEPENDENCIES
254
+ byebug
255
+ pry-byetypo!
256
+ rake (~> 13.0)
257
+ rspec (~> 3.0)
258
+ standard (~> 1.3)
259
+
260
+ BUNDLED WITH
261
+ 2.4.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 morissetcl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,145 @@
1
+ # PRY-BYETYPO
2
+
3
+ Autocorrects typos in your Pry console.
4
+
5
+ This small Pry plugin captures exceptions that could be due to typos and deduces the correct command based on your database information.
6
+
7
+ > [!NOTE]
8
+ > So far, this plugin is not framework agnostic, and it requires Rails 7 or later.
9
+
10
+
11
+ #### Before
12
+
13
+ ```ruby
14
+ [1] pry(main)> Usert.last
15
+ NameError: uninitialized constant Usert
16
+ from (pry):3:in `__pry__'
17
+ ```
18
+
19
+ #### After
20
+
21
+ ```ruby
22
+ [1] pry(main)> Usert.last
23
+ I, [2024-01-13T20:00:16.280710 #694] INFO -- : `Usert` not working, running:
24
+ I, [2024-01-13T20:00:16.281237 #694] INFO -- : User.last
25
+ 2024-01-13 20:00:16.345175 D [694:9200 log_subscriber.rb:130] ActiveRecord::Base -- User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL ORDER BY "users"."id" DESC LIMIT $1 [["LIMIT", 1]]
26
+ => #<User id: 1, email: "yo@email.com">
27
+ ```
28
+
29
+ ## Installation
30
+
31
+
32
+ Install the gem and add to the application's Gemfile, under the `development` group, by executing:
33
+
34
+ ```
35
+ bundle add pry-byetypo
36
+ ```
37
+
38
+ If bundler is not being used to manage dependencies, install the gem by executing:
39
+
40
+ ```
41
+ gem install pry-byetypo
42
+ ```
43
+
44
+ ## Usage
45
+
46
+ 1. Open a PRY console.
47
+ 2. Start your daily work.
48
+ 3. Let `pry-byetypo` remove frictions. 🚀
49
+
50
+ ## Under the hood
51
+
52
+ ### 1. Byetypo dictionary
53
+
54
+ When you open a new Pry console, the gem will generate a `byetypo_dictionary.pstore` file containing three pieces of information:
55
+
56
+ - A list of the ActiveRecord models in your application (e.g. `User`, `Account`).
57
+ - A list of the ActiveRecord associations in your application (e.g. `user`, `users`, `account`, `accounts`).
58
+ - A timestamp representing the last time the `byetypo_dictionary` was updated. (by default updated every week).
59
+
60
+ This file is generated at the root of your application by default. If you want to update its location, you can configure the path by adding a `BYETYPO_STORE_PATH` entry in your `.env` file.
61
+
62
+ ### 2. Captured exceptions
63
+
64
+ #### NameError
65
+
66
+ This error occurs when you mispelled a model in your REPL. The gem will catch that exception and will try find the closest matches. If so, it will run the command with the (potential) corrected model.
67
+
68
+ ```ruby
69
+ [1] pry(main)> Usert.last
70
+ I, [2024-01-13T20:00:16.280710 #694] INFO -- : `Usert` not working, running:
71
+ I, [2024-01-13T20:00:16.281237 #694] INFO -- : User.last
72
+ 2024-01-13 20:00:16.345175 D [694:9200 log_subscriber.rb:130] ActiveRecord::Base -- User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL ORDER BY "users"."id" DESC LIMIT $1 [["LIMIT", 1]]
73
+ => #<User id: 1, email: "yo@email.com">
74
+ ```
75
+
76
+ #### ActiveRecord::ConfigurationError
77
+
78
+ Raised when association is being configured improperly or user tries to use offset and limit together with `ActiveRecord::Base.has_many` or `ActiveRecord::Base.has_and_belongs_to_many` associations.
79
+
80
+ eg:
81
+
82
+ ```ruby
83
+ [6] pry(main)> User.joins(:group).where(groups: { name: "Landlord" }).last
84
+ ActiveRecord::ConfigurationError: Can't join 'User' to association named 'group'; perhaps you misspelled it?
85
+ ```
86
+
87
+ This plugin will look into the `byetypo_dictionary` file to find the closest match and run the correct query.
88
+
89
+
90
+ ```ruby
91
+ [1] pry(main)> User.joins(:group).where(groups: { name: "Landlord" })
92
+ I, [2024-01-13T22:45:16.297811 #1079] INFO -- : `group` not working, running.
93
+ I, [2024-01-13T22:45:16.297972 #1079] INFO -- : User.joins(:groups).where(groups: { name: "Landlord" })
94
+ 2024-01-13 22:45:16.319544 D [1079:9200 log_subscriber.rb:130] ActiveRecord::Base -- User Load (1.6ms) SELECT "users".* FROM "users" INNER JOIN "user_groups" ON "user_groups"."user_id" = "users"."id" INNER JOIN "groups" ON "groups"."id" = "user_groups"."group_id" WHERE "users"."deleted_at" IS NULL AND "groups"."name" = $1 [["name", "Landlord"]]
95
+ => []
96
+ ```
97
+
98
+ #### ActiveRecord::StatementInvalid
99
+
100
+ The query attempts to reference columns or conditions related to a table, but the table is not properly included in the FROM clause.
101
+
102
+ ```ruby
103
+ [1] pry(main)> User.joins(:groups).where(grous: { name: "Landlord" }).last
104
+ ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "grous"
105
+ LINE 1: ..."group_id" WHERE "users"."deleted_at" IS NULL AND "grous"."n...
106
+ ```
107
+
108
+ This plugin will look into the `byetypo_dictionary` file to find the closest match and run the correct query.
109
+
110
+ ```ruby
111
+ 1] pry(main)> User.joins(:groups).where(grous: { name: "Landlord" }).last
112
+ I, [2024-01-14T23:50:49.273043 #1248] INFO -- : `grous` not working, running:
113
+ I, [2024-01-14T23:50:49.273177 #1248] INFO -- : User.joins(:groups).where(groups: { name: "Landlord" }).last
114
+ 2024-01-14 23:50:49.281956 D [1248:9200 log_subscriber.rb:130] ActiveRecord::Base -- User Load (2.1ms) SELECT "users".* FROM "users" INNER JOIN "user_groups" ON "user_groups"."user_id" = "users"."id" INNER JOIN "groups" ON "groups"."id" = "user_groups"."group_id" WHERE "users"."deleted_at" IS NULL AND "groups"."name" = $1 ORDER BY "users"."id" DESC LIMIT $2 [["name", "Landlord"], ["LIMIT", 1]]
115
+ ```
116
+
117
+ ## Troubleshooting
118
+
119
+ Pry-byetypo is linked to your development database. During initialization, it will attempt to establish a connection to retrieve the tables available in your project. It will fetch the information for the development environment from the `database.yml` file.
120
+
121
+ ### Unreadable database URL (URI::InvalidURIError)
122
+
123
+ If the database connection string is not readable, the gem will be unable to establish a connection. If you encounter such an issue, make sure to add a `DATABASE_URL` variable to your `.env` file with the easily readable URL of your database.
124
+
125
+ ### Unreadable connection pool (ActiveRecord::ConnectionTimeoutError)
126
+
127
+ If the number of connections in your pool is not readable, you may encounter an `ActiveRecord::ConnectionTimeoutError`. If you experience this issue, make sure to add a `DATABASE_POOL` variable to your `.env` file.
128
+
129
+ ## Development
130
+
131
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
132
+
133
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
134
+
135
+ ## Contributing
136
+
137
+ Bug reports and pull requests are welcome on GitHub at https://github.com/morissetcl/pry-byetypo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/morissetcl/pry-byetypo/blob/master/CODE_OF_CONDUCT.md).
138
+
139
+ ## License
140
+
141
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
142
+
143
+ ## Code of Conduct
144
+
145
+ Everyone interacting in the Pry::Byetypo project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/morissetcl/pry-byetypo/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Base
4
+ class << self
5
+ def call(...)
6
+ new(...).call
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../exceptions_base"
4
+
5
+ module Exceptions
6
+ module ActiveRecord
7
+ class Base < ExceptionsBase
8
+ private
9
+
10
+ def corrected_word
11
+ @corrected_word ||= spell_checker(associations_dictionary).correct(unknown_from_exception).first
12
+ end
13
+
14
+ def corrected_cmd
15
+ @corrected_cmd ||= last_cmd.gsub(/\b#{unknown_from_exception}\b/, corrected_word)
16
+ end
17
+
18
+ def unknown_from_exception
19
+ exception.to_s.match(exception_regexp)[1]
20
+ end
21
+
22
+ def associations_dictionary
23
+ @associations_dictionary ||= store.transaction { |s| s["associations"] }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module Exceptions
6
+ module ActiveRecord
7
+ class ConfigurationError < Base
8
+ private
9
+
10
+ def exception_regexp
11
+ /association named '(.*?)'/
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module Exceptions
6
+ module ActiveRecord
7
+ class StatementInvalid < Base
8
+ private
9
+
10
+ def exception_regexp
11
+ /PG::UndefinedTable: ERROR: missing FROM-clause entry for table "(.*?)"\nLINE/
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../base"
4
+ require_relative "../setup/store"
5
+ require "colorize"
6
+
7
+ class ExceptionsBase < Base
8
+ include Setup::Store
9
+
10
+ def call
11
+ logger.error(exception.to_s.colorize(color: :light_red, mode: :bold))
12
+ logger.info("Running: #{corrected_cmd}".colorize(color: :green, mode: :bold))
13
+
14
+ pry.eval(corrected_cmd)
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :exception, :output, :pry
20
+
21
+ def initialize(output, exception, pry)
22
+ @output = output
23
+ @exception = exception
24
+ @pry = pry
25
+ end
26
+
27
+ def spell_checker(dictionary)
28
+ DidYouMean::SpellChecker.new(dictionary: dictionary)
29
+ end
30
+
31
+ def logger
32
+ @logger = Logger.new($stdout)
33
+ end
34
+
35
+ def last_cmd
36
+ @last_cmd ||= Pry.line_buffer.last.strip
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "exceptions_base"
4
+
5
+ module Exceptions
6
+ class NameError < ExceptionsBase
7
+ private
8
+
9
+ def unknown_from_exception
10
+ exception.to_s.split.last
11
+ end
12
+
13
+ def corrected_word
14
+ @corrected_word ||= spell_checker(ar_models_dictionary).correct(unknown_from_exception).first
15
+ end
16
+
17
+ def corrected_cmd
18
+ @corrected_cmd ||= last_cmd.gsub(unknown_from_exception, corrected_word)
19
+ end
20
+
21
+ def ar_models_dictionary
22
+ @ar_models_dictionary ||= store.transaction { |s| s["active_record_models"] }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+ require_relative "exceptions/active_record/statement_invalid"
5
+ require_relative "exceptions/active_record/configuration_error"
6
+ require_relative "exceptions/name_error"
7
+
8
+ class ExceptionsHandler < Base
9
+ attr_reader :exception, :output, :pry
10
+
11
+ UNINITIALIZED_CONSTANT = "uninitialized constant"
12
+ UNDEFINED_TABLE = "UndefinedTable"
13
+
14
+ def initialize(output, exception, pry)
15
+ @output = output
16
+ @exception = exception
17
+ @pry = pry
18
+ end
19
+
20
+ def call
21
+ case exception
22
+ in NameError => error
23
+ # eg: Usert.last
24
+ # NameError is a Superclass for all undefined statement.
25
+ # In this context We only need to one including an `uninitialized constant` error.
26
+ return pry_exception_handler unless error.message.include?(UNINITIALIZED_CONSTANT)
27
+ Exceptions::NameError.call(output, exception, pry)
28
+ in ActiveRecord::StatementInvalid => error
29
+ # eg: User.joins(:groups).where(grous: { name: "Landlord" }).last
30
+ # ActiveRecord::StatementInvalid is a Superclass for all database execution errors.
31
+ # We only need to one including an `UndefinedTable` error.
32
+ return pry_exception_handler unless error.message.include?(UNDEFINED_TABLE)
33
+ Exceptions::ActiveRecord::StatementInvalid.call(output, exception, pry)
34
+ in ActiveRecord::ConfigurationError
35
+ # eg: User.joins(:group).where(groups: { name: "Landlord" }).last
36
+ Exceptions::ActiveRecord::ConfigurationError.call(output, exception, pry)
37
+ else
38
+ pry_exception_handler
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def pry_exception_handler
45
+ Pry::ExceptionHandler.handle_exception(output, exception, pry)
46
+ end
47
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "checks/database_url"
4
+ require_relative "checks/database_pool"
5
+ require_relative "store"
6
+
7
+ require "pstore"
8
+
9
+ module Setup
10
+ class ApplicationDictionary
11
+ include Store
12
+
13
+ def initialize
14
+ establish_db_connection
15
+ populate_store
16
+ end
17
+
18
+ private
19
+
20
+ SEVEN_DAYS = 604800
21
+
22
+ def populate_store
23
+ store.transaction do
24
+ store.abort unless staled_store?
25
+
26
+ store["active_record_models"] = populate_active_record_models_dictionary
27
+ store["associations"] = populate_associations
28
+ store["synced_at"] = Time.now
29
+ end
30
+ end
31
+
32
+ def establish_db_connection
33
+ configuration_checks
34
+ ActiveRecord::Base.establish_connection(development_database_config)
35
+ end
36
+
37
+ def populate_active_record_models_dictionary
38
+ Zeitwerk::Loader.eager_load_all
39
+ ActiveRecord::Base.descendants.map { |model| model.name }
40
+ end
41
+
42
+ 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
46
+ end
47
+
48
+ def configuration_checks
49
+ Setup::Checks::DatabaseUrl.check(development_database_config, logger)
50
+ Setup::Checks::DatabasePool.check(development_database_config, logger)
51
+ end
52
+
53
+ def database_config
54
+ YAML.safe_load(File.read("./config/database.yml"), aliases: true)
55
+ end
56
+
57
+ def development_database_config
58
+ @development_database_config ||= database_config["development"]
59
+ end
60
+
61
+ # By default we update the store every week.
62
+ # TODO: Make it configurable
63
+ def staled_store?
64
+ return true if store["synced_at"].nil?
65
+
66
+ (store["synced_at"] + SEVEN_DAYS) <= Time.now
67
+ end
68
+
69
+ def logger
70
+ @logger = Logger.new($stdout)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+
5
+ class Base
6
+ class << self
7
+ def check(database_config, logger)
8
+ URI.parse(database_config[name])
9
+ rescue URI::InvalidURIError
10
+ # Try to connect using the ENV variables.
11
+ if ENV[variable]
12
+ database_config[name] = ENV[variable]
13
+ else
14
+ logger.warn(missing_variable_msg)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module Setup
6
+ module Checks
7
+ class DatabasePool < Base
8
+ class << self
9
+ def name
10
+ "pool"
11
+ end
12
+
13
+ def variable
14
+ "DATABASE_POOL"
15
+ end
16
+
17
+ def missing_variable_msg
18
+ "[PRY-BYETYPO] ENV[\"DATABASE_POOL\"] is empty. Please assign a value to it to enable the functionality of pry-byetypo."
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module Setup
6
+ module Checks
7
+ class DatabaseUrl < Base
8
+ class << self
9
+ def name
10
+ "url"
11
+ end
12
+
13
+ def variable
14
+ "DATABASE_URL"
15
+ end
16
+
17
+ def missing_variable_msg
18
+ "[PRY-BYETYPO] ENV[\"DATABASE_URL\"] is empty. Please assign a value to it to enable the functionality of pry-byetypo."
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module Setup
2
+ module Store
3
+ DEFAULT_STORE_PATH = "byetypo_dictionary.pstore"
4
+
5
+ def store
6
+ @store ||= PStore.new(store_path)
7
+ end
8
+
9
+ def store_path
10
+ ENV["BYETYPO_STORE_PATH"] || DEFAULT_STORE_PATH
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ module Byetypo
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pry"
4
+ require "zeitwerk"
5
+
6
+ require_relative "pry-byetypo/version"
7
+ require_relative "pry-byetypo/setup/application_dictionary"
8
+ require_relative "pry-byetypo/exceptions_handler"
9
+
10
+ module Pry::Byetypo
11
+ Pry.config.hooks.add_hook(:before_session, :eager_loading) do |output, exception, pry|
12
+ Setup::ApplicationDictionary.new
13
+ end
14
+
15
+ # TODO: Adds max_attempts
16
+ # TODO: If max_attempt reached clean the last entries (eg: max entry 3 has been reached, we remove the last 3 history entries)
17
+ Pry.config.exception_handler = proc do |output, exception, pry|
18
+ ExceptionsHandler.call(output, exception, pry)
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/pry-byetypo/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "pry-byetypo"
7
+ gem.version = Pry::Byetypo::VERSION
8
+ gem.authors = ["morissetcl"]
9
+ gem.email = ["morissetcl87@gmail.com"]
10
+
11
+ gem.summary = "Autocorrects typos in your Pry console"
12
+ gem.description = "This small Pry plugin captures exceptions that could be due to typos and deduces the correct command based on your database information."
13
+ gem.homepage = "https://github.com/morissetcl/pry-byetypo"
14
+ gem.license = "MIT"
15
+ gem.required_ruby_version = ">= 2.6.0"
16
+
17
+ gem.metadata["homepage_uri"] = gem.homepage
18
+ gem.metadata["source_code_uri"] = "https://github.com/morissetcl/pry-byetypo"
19
+ gem.metadata["changelog_uri"] = "https://github.com/morissetcl/pry-byetypo/blob/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ gem.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
26
+ end
27
+ end
28
+ gem.bindir = "exe"
29
+ gem.executables = gem.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ gem.require_paths = ["lib"]
31
+ gem.add_runtime_dependency "colorize", "~> 1.1.0"
32
+ gem.add_runtime_dependency "pry", ">= 0.13", "< 0.15"
33
+ gem.add_runtime_dependency "rails", "~> 7.0"
34
+ end
@@ -0,0 +1,6 @@
1
+ module Pry
2
+ module Byetypo
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pry-byetypo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - morissetcl
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.13'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '0.15'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0.13'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.15'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rails
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '7.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '7.0'
61
+ description: This small Pry plugin captures exceptions that could be due to typos
62
+ and deduces the correct command based on your database information.
63
+ email:
64
+ - morissetcl87@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - ".rspec"
70
+ - ".standard.yml"
71
+ - CHANGELOG.md
72
+ - CODE_OF_CONDUCT.md
73
+ - Gemfile
74
+ - Gemfile.lock
75
+ - LICENSE.txt
76
+ - README.md
77
+ - Rakefile
78
+ - lib/pry-byetypo.rb
79
+ - lib/pry-byetypo/base.rb
80
+ - lib/pry-byetypo/exceptions/active_record/base.rb
81
+ - lib/pry-byetypo/exceptions/active_record/configuration_error.rb
82
+ - lib/pry-byetypo/exceptions/active_record/statement_invalid.rb
83
+ - lib/pry-byetypo/exceptions/exceptions_base.rb
84
+ - lib/pry-byetypo/exceptions/name_error.rb
85
+ - lib/pry-byetypo/exceptions_handler.rb
86
+ - lib/pry-byetypo/setup/application_dictionary.rb
87
+ - lib/pry-byetypo/setup/checks/base.rb
88
+ - lib/pry-byetypo/setup/checks/database_pool.rb
89
+ - lib/pry-byetypo/setup/checks/database_url.rb
90
+ - lib/pry-byetypo/setup/store.rb
91
+ - lib/pry-byetypo/version.rb
92
+ - pry-byetypo.gemspec
93
+ - sig/pry/byetypo.rbs
94
+ homepage: https://github.com/morissetcl/pry-byetypo
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ homepage_uri: https://github.com/morissetcl/pry-byetypo
99
+ source_code_uri: https://github.com/morissetcl/pry-byetypo
100
+ changelog_uri: https://github.com/morissetcl/pry-byetypo/blob/master/CHANGELOG.md
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 2.6.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.4.22
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Autocorrects typos in your Pry console
120
+ test_files: []