fuzzy-ruby 0.1.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 +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +10 -0
- data/fuzzy-ruby.gemspec +27 -0
- data/lib/fuzzy-ruby.rb +3 -0
- data/lib/fuzzy-ruby/api.rb +96 -0
- data/lib/fuzzy-ruby/version.rb +3 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 11666c5fe11d863034d4f1ad664411266ea89237
|
4
|
+
data.tar.gz: 82c71856bc61b0c711d6809874f4acdfadfbd1c8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d3e3f31a517595367d32dd456a50b72ed889aa082850872ccb993ee537a4d200635113142473391b951237638575f5e251e3c044716ec8f11fb98a97d9168a41
|
7
|
+
data.tar.gz: cf0cba0c205be51c54cab8a2a4ce96e607f7206c4cc70945c1183a2acd900a9d4edb691ad92a3af493f45c89a3b1c58c0179ca813a4b40fb1c2ef78c1bacac4d
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Ben Weissmann
|
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,112 @@
|
|
1
|
+
|
2
|
+
Fuzzy Ruby
|
3
|
+
==========
|
4
|
+
|
5
|
+
[](https://travis-ci.org/benweissmann/fuzzy-ruby)
|
6
|
+
|
7
|
+
One of the most difficult parts of programming is spelling everything correctly.
|
8
|
+
Particularly in a dynamic-typed language like Ruby, a single typo can cause
|
9
|
+
a critical runtime failure.
|
10
|
+
|
11
|
+
Traditional solutions to this problem include complex static analysis,
|
12
|
+
burdonsome code reviews, and onerous unit testing.
|
13
|
+
|
14
|
+
Fuzzy Ruby is a much easier solution to this problem: it modifies the Ruby
|
15
|
+
runtime environment so misspellings will automatically be corrected.
|
16
|
+
|
17
|
+
|
18
|
+
Example
|
19
|
+
=======
|
20
|
+
|
21
|
+
Without `fuzzy-ruby`:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
irb(main):001:0> "Hello".revers
|
25
|
+
NoMethodError: undefined method `revers' for "Hello":String
|
26
|
+
irb(main):002:0> my_variable = 123
|
27
|
+
=> 123
|
28
|
+
irb(main):003:0> my_vraiable
|
29
|
+
NameError: undefined local variable or method `my_vraiable' for main:Object
|
30
|
+
```
|
31
|
+
|
32
|
+
With `fuzzy-ruby`:
|
33
|
+
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
rirb(main):001:0> require 'fuzzy-ruby'
|
37
|
+
=> true
|
38
|
+
irb(main):002:0> "Hello".revers
|
39
|
+
=> "olleH"
|
40
|
+
irb(main):003:0> my_variable = 123
|
41
|
+
=> 123
|
42
|
+
irb(main):004:0> my_vraiable
|
43
|
+
=> 123
|
44
|
+
```
|
45
|
+
|
46
|
+
|
47
|
+
Installation
|
48
|
+
============
|
49
|
+
|
50
|
+
Add this line to your application's Gemfile:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
gem 'fuzzy-ruby'
|
54
|
+
```
|
55
|
+
|
56
|
+
And then execute:
|
57
|
+
|
58
|
+
$ bundle
|
59
|
+
|
60
|
+
Or install it yourself as:
|
61
|
+
|
62
|
+
$ gem install fuzzy-ruby
|
63
|
+
|
64
|
+
|
65
|
+
Advanced Usage
|
66
|
+
==============
|
67
|
+
|
68
|
+
A simple `require 'fuzzy-ruby'` will load Fuzzy Ruby and set it up to
|
69
|
+
automatically correct misspellings. However, if you'd like more fine-grained
|
70
|
+
control, you can use `require 'fuzzy-ruby/api'`, which just gives you access
|
71
|
+
to the `FuzzyRuby` api without modifying your runtime environment.
|
72
|
+
|
73
|
+
#### `FuzzyRuby.install`
|
74
|
+
|
75
|
+
Without a block, sets up Fuzzy Ruby to autocorrect misspellings until
|
76
|
+
`FuzzyRuby.uninstall` is called. With a block, runs the block with Fuzzy Ruby
|
77
|
+
installed, and then automatically uninstalls it when the block completes or
|
78
|
+
raises an error. The return value or error from the block is propagated up.
|
79
|
+
|
80
|
+
#### `FuzzyRuby.uninstall`
|
81
|
+
|
82
|
+
Undoes a `FuzzyRuby.install`.
|
83
|
+
|
84
|
+
#### `FuzzyRuby.mode = :autocorrect | :autocorrect_with_warning | :warn_only`
|
85
|
+
|
86
|
+
Sets the mode of Fuzzy Ruby. By default, Fuzzy Ruby uses `:autocorrect`, which
|
87
|
+
automatically corrects typos without warning. Other modes are
|
88
|
+
`:autocorrect_with_warning`, which autocorrect but prints a warning to stderr,
|
89
|
+
and `:warn_only`, which just prints the warning, but does not correct.
|
90
|
+
|
91
|
+
|
92
|
+
License
|
93
|
+
=======
|
94
|
+
|
95
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
96
|
+
|
97
|
+
|
98
|
+
How Does It Work?
|
99
|
+
=================
|
100
|
+
|
101
|
+
Fuzzy Ruby overrides `method_missing` on `Object`. This allows us to intercept
|
102
|
+
methods calls to any object, as well as local variable access. We then grab the
|
103
|
+
list of methods on the object, as well as the list of local variables
|
104
|
+
(using the `binding_of_caller` gem) and select the best match using the
|
105
|
+
Jaro-Winkler text distance algorithm (from the `amatch` gem). We then use
|
106
|
+
`eval` or `send` to run the method or access the local variable.
|
107
|
+
|
108
|
+
|
109
|
+
Notes
|
110
|
+
=====
|
111
|
+
|
112
|
+
Don't actually use this. Please.
|
data/Rakefile
ADDED
data/fuzzy-ruby.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fuzzy-ruby/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fuzzy-ruby"
|
8
|
+
spec.version = FuzzyRuby::VERSION
|
9
|
+
spec.authors = ["Ben Weissmann"]
|
10
|
+
spec.email = ["ben@benweissmann.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Auto-correct your Ruby code using fuzzy matching.}
|
13
|
+
spec.homepage = "https://github.com/benweissmann/fuzzy-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "amatch", "~> 0.3.0"
|
22
|
+
spec.add_runtime_dependency "binding_of_caller", "~> 0.7.2"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest"
|
27
|
+
end
|
data/lib/fuzzy-ruby.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'amatch'
|
2
|
+
require 'binding_of_caller'
|
3
|
+
require 'fuzzy-ruby/version'
|
4
|
+
|
5
|
+
module FuzzyRuby
|
6
|
+
class << self
|
7
|
+
VALID_MODES = [:autocorrect, :autocorrect_with_warning, :warn_only]
|
8
|
+
@mode = :autocorrect
|
9
|
+
@enabled = false
|
10
|
+
|
11
|
+
def mode= new_mode
|
12
|
+
unless VALID_MODES.include? new_mode
|
13
|
+
raise ArgumentError.new "Invalid mode: #{new_mode}"
|
14
|
+
end
|
15
|
+
|
16
|
+
@mode = new_mode
|
17
|
+
end
|
18
|
+
|
19
|
+
def install
|
20
|
+
# Enable automatic autocorrection
|
21
|
+
@enabled = true
|
22
|
+
|
23
|
+
if block_given?
|
24
|
+
begin
|
25
|
+
return yield
|
26
|
+
ensure
|
27
|
+
uninstall
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def uninstall
|
33
|
+
@enabled = false
|
34
|
+
end
|
35
|
+
|
36
|
+
def enabled?
|
37
|
+
@enabled
|
38
|
+
end
|
39
|
+
|
40
|
+
attr_reader :mode
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# Automatically corrects a method call or local variable access.
|
45
|
+
#
|
46
|
+
# May actually call the corrected version / return the value of the
|
47
|
+
# corrected variable name, or may just print a parming, or both, based
|
48
|
+
# on the value of @mode
|
49
|
+
#
|
50
|
+
# binding: The binding in which the method call or variable access occurred
|
51
|
+
# receiver: The object whose methods are being called
|
52
|
+
# name: Name of the method or variable, as a symbol
|
53
|
+
# args: Array of arguments passed to the method
|
54
|
+
# block: Block passed to the method
|
55
|
+
def autocorrect binding, receiver, name, args, block
|
56
|
+
# assemble possible methods or variables we could correct to
|
57
|
+
methods = receiver.methods.map(&:to_s)
|
58
|
+
vars = binding.eval("local_variables").map(&:to_s)
|
59
|
+
|
60
|
+
# match the mostly likely one
|
61
|
+
matcher = Amatch::JaroWinkler.new(name.to_s)
|
62
|
+
best = (vars + methods).sort_by{|m| matcher.match(m)}.last
|
63
|
+
|
64
|
+
# print a warning depending on mode
|
65
|
+
case @mode
|
66
|
+
when :autocorrect_with_warning
|
67
|
+
STDERR.puts "WARNING: Autocorrecting #{name} to #{best}"
|
68
|
+
when :warn_only
|
69
|
+
STDERR.puts "WARNING: by #{name}, you probably meant #{best}"
|
70
|
+
end
|
71
|
+
|
72
|
+
# execute the corrected version
|
73
|
+
if @mode != :warn_only
|
74
|
+
if methods.include? best
|
75
|
+
return receiver.send(best.to_sym, *args, &block)
|
76
|
+
else
|
77
|
+
return binding.eval(best)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
# hook into method_missing on Object
|
86
|
+
class Object
|
87
|
+
alias_method :__fuzzyruby_method_missing_old__, :method_missing
|
88
|
+
def method_missing name, *args, &block
|
89
|
+
if FuzzyRuby.enabled?
|
90
|
+
FuzzyRuby.send :autocorrect, binding.of_caller(1), self, name, args, block
|
91
|
+
else
|
92
|
+
__fuzzyruby_method_missing_old__ name, *args, &block
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fuzzy-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Weissmann
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: amatch
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.3.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.3.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: binding_of_caller
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.7.2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.7.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- ben@benweissmann.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- fuzzy-ruby.gemspec
|
97
|
+
- lib/fuzzy-ruby.rb
|
98
|
+
- lib/fuzzy-ruby/api.rb
|
99
|
+
- lib/fuzzy-ruby/version.rb
|
100
|
+
homepage: https://github.com/benweissmann/fuzzy-ruby
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.4.5.1
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Auto-correct your Ruby code using fuzzy matching.
|
124
|
+
test_files: []
|