strict_ivars 1.0.0 → 1.0.1
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 +4 -4
- data/README.md +16 -3
- data/lib/strict_ivars/name_error.rb +7 -42
- data/lib/strict_ivars/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a6e9c3b4af8b1558193b058b36ab883ecc8dbce111b13bafb0cedd54852faa9
|
4
|
+
data.tar.gz: 22858692a9849c8f1f3b33450fe78054a622216221d81edd7992868a530dfc3c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b52c5c582936f6905a00b381846109d673b6ff8b9a6c7f568d4c8bab82b60c8158afbf3924628d93ee133e5d98583169462ac42887a8f50e0c96068c23a450ea
|
7
|
+
data.tar.gz: 0b0d00db3c3daf0dd7c72ad2c3702e57240dcd93a5965d7b0e18b97ba56f5ec53dffc42cf4a7d845adc03ff0cd09c8f9c777d25d2c711be1c26de357583de977
|
data/README.md
CHANGED
@@ -4,11 +4,11 @@ If you reference an undefined method, constant or local varaible, Ruby will help
|
|
4
4
|
|
5
5
|
Strict Ivars solves this by making Ruby raise a `NameError` any time you read an undefined instance varaible. It’s enabled with two lines of code in your boot process, then it just works in the background and you’ll never have to think about it again. Strict Ivars has no known false-positives or false-negatives.
|
6
6
|
|
7
|
-
It
|
7
|
+
It’s especially good when used with [Literal](https://literal.fun) and [Phlex](https://www.phlex.fun), though it also works with regular Ruby objects and even ERB templates, which are actually pretty common spots for undefined instance variable reads to hide since that’s the main way of passing data to ERB.
|
8
8
|
|
9
|
-
When combined with Literal, you can
|
9
|
+
When combined with Literal, you can essentially remove all unexpected `nil`s. Literal validates your inputs and Strict Ivars ensures you’re reading the right instance variables.
|
10
10
|
|
11
|
-
> [!NOTE]
|
11
|
+
> [!NOTE]
|
12
12
|
> JRuby and TruffleRuby are not currently supported.
|
13
13
|
|
14
14
|
## Setup
|
@@ -35,6 +35,19 @@ StrictIvars.init(include: ["#{Dir.pwd}/**/*"], exclude: ["#{Dir.pwd}/vendor/**/*
|
|
35
35
|
|
36
36
|
This example include everything in the current directory apart from the `./vendor` folder (which is where GitHub Actions installs gems).
|
37
37
|
|
38
|
+
If you’re setting this up in Rails, your `boot.rb` file should look something like this.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
|
42
|
+
|
43
|
+
require "bundler/setup" # Set up gems listed in the Gemfile.
|
44
|
+
require "bootsnap/setup" # Speed up boot time by caching expensive operations.
|
45
|
+
|
46
|
+
require "strict_ivars"
|
47
|
+
|
48
|
+
StrictIvars.init(include: ["#{Dir.pwd}/**/*"], exclude: ["#{Dir.pwd}/vendor/**/*"])
|
49
|
+
```
|
50
|
+
|
38
51
|
If you’re using Bootsnap, you should clear your bootsnap cache by deleting the folder `tmp/cache/bootsnap`.
|
39
52
|
|
40
53
|
## How does it work?
|
@@ -1,12 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "did_you_mean/spell_checker"
|
4
|
+
|
3
5
|
class StrictIvars::NameError < ::NameError
|
4
6
|
INSTANCE_VARIABLE_METHOD = Kernel.instance_method(:instance_variables)
|
5
7
|
|
6
8
|
def initialize(object, name)
|
7
|
-
|
8
|
-
|
9
|
-
|
9
|
+
checker = DidYouMean::SpellChecker.new(
|
10
|
+
dictionary: INSTANCE_VARIABLE_METHOD.bind_call(object)
|
11
|
+
)
|
12
|
+
|
13
|
+
suggestion = checker.correct(name).first
|
10
14
|
|
11
15
|
message = [
|
12
16
|
"Undefined instance variable `#{name}`.",
|
@@ -15,43 +19,4 @@ class StrictIvars::NameError < ::NameError
|
|
15
19
|
|
16
20
|
super(message)
|
17
21
|
end
|
18
|
-
|
19
|
-
private def n_common_trigrams(left, right)
|
20
|
-
left = "\x03\x02#{left}"
|
21
|
-
right = "\x03\x02#{right}"
|
22
|
-
|
23
|
-
left_len = left.length
|
24
|
-
right_len = right.length
|
25
|
-
|
26
|
-
return 0 if left_len < 3 || right_len < 3
|
27
|
-
|
28
|
-
# Process shorter string first
|
29
|
-
if left_len > right_len
|
30
|
-
left, right = right, left
|
31
|
-
left_len, right_len = right_len, left_len
|
32
|
-
end
|
33
|
-
|
34
|
-
# Use a Set for lookup
|
35
|
-
trigrams = Set.new
|
36
|
-
count = 0
|
37
|
-
|
38
|
-
# Generate trigrams from shorter string
|
39
|
-
i = 0
|
40
|
-
left_max = left_len - 2
|
41
|
-
while i < left_max
|
42
|
-
trigram = left[i, 3]
|
43
|
-
trigrams.add(trigram)
|
44
|
-
i += 1
|
45
|
-
end
|
46
|
-
|
47
|
-
# Check trigrams from longer string
|
48
|
-
i = 0
|
49
|
-
right_max = right_len - 2
|
50
|
-
while i < right_max
|
51
|
-
count += 1 if trigrams.include?(right[i, 3])
|
52
|
-
i += 1
|
53
|
-
end
|
54
|
-
|
55
|
-
count
|
56
|
-
end
|
57
22
|
end
|
data/lib/strict_ivars/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strict_ivars
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Drapper
|
@@ -38,7 +38,8 @@ dependencies:
|
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '0'
|
40
40
|
description: Strict Ivars is a tiny pre-processor that guards you against undefined
|
41
|
-
instance variable reads.
|
41
|
+
instance variable reads. If you do read an undefined instance variable, it will
|
42
|
+
raise a NameError.
|
42
43
|
email:
|
43
44
|
- joel@drapper.me
|
44
45
|
executables: []
|