did-you-mean 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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +39 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/did-you-mean.gemspec +58 -0
- data/lib/did-you-mean.rb +37 -0
- data/spec/did-you-mean_spec.rb +45 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- metadata +101 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Leonardo Bessa
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
= did-you-mean
|
2
|
+
|
3
|
+
Did-you-mean compares the misspelled method name with all other method names and suggests the one with closest textual match.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
sudo gem install did-you-mean
|
8
|
+
|
9
|
+
== Basic Usage
|
10
|
+
|
11
|
+
# First include the module in the Object class
|
12
|
+
require 'did-you-mean'
|
13
|
+
class Object
|
14
|
+
include DidYouMean
|
15
|
+
end
|
16
|
+
|
17
|
+
# Search method names by textual proximity
|
18
|
+
"FOO".methods.search('down_case')[0..5]
|
19
|
+
["downcase", "downcase!", "swapcase", "upcase", "concat", "swapcase!"]
|
20
|
+
|
21
|
+
# Avoiding NoMethodError on method_missing
|
22
|
+
%w(a o u i e).sot
|
23
|
+
Did you mean Array.sort (yes/no)?
|
24
|
+
yes
|
25
|
+
["a", "e", "i", "o", "u"]
|
26
|
+
|
27
|
+
== Note on Patches/Pull Requests
|
28
|
+
|
29
|
+
* Fork the project.
|
30
|
+
* Make your feature addition or bug fix.
|
31
|
+
* Add tests for it. This is important so I don't break it in a
|
32
|
+
future version unintentionally.
|
33
|
+
* Commit, do not mess with rakefile, version, or history.
|
34
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
35
|
+
* Send me a pull request. Bonus points for topic branches.
|
36
|
+
|
37
|
+
== Copyright
|
38
|
+
|
39
|
+
Copyright (c) 2010 Leonardo Bessa. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "did-you-mean"
|
8
|
+
gem.summary = %Q{Runtime corrector for misspelled method calls}
|
9
|
+
gem.description = %Q{Did-you-mean compares the misspelled method name with all other method names and suggests the one with closest textual match.}
|
10
|
+
gem.email = "leobessa@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/leobessa/did-you-mean"
|
12
|
+
gem.authors = ["Leonardo Bessa"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_dependency('levenshtein', '>= 0.2.0')
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "did-you-mean #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{did-you-mean}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Leonardo Bessa"]
|
12
|
+
s.date = %q{2010-04-12}
|
13
|
+
s.description = %q{Did-you-mean compares the misspelled method name with all other method names and suggests the one with closest textual match.}
|
14
|
+
s.email = %q{leobessa@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"did-you-mean.gemspec",
|
27
|
+
"lib/did-you-mean.rb",
|
28
|
+
"spec/did-you-mean_spec.rb",
|
29
|
+
"spec/spec.opts",
|
30
|
+
"spec/spec_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/leobessa/did-you-mean}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.6}
|
36
|
+
s.summary = %q{Runtime corrector for misspelled method calls}
|
37
|
+
s.test_files = [
|
38
|
+
"spec/did-you-mean_spec.rb",
|
39
|
+
"spec/spec_helper.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
48
|
+
s.add_runtime_dependency(%q<levenshtein>, [">= 0.2.0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
51
|
+
s.add_dependency(%q<levenshtein>, [">= 0.2.0"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
55
|
+
s.add_dependency(%q<levenshtein>, [">= 0.2.0"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
data/lib/did-you-mean.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'levenshtein'
|
3
|
+
|
4
|
+
module DidYouMean
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
def methods_with_a_search
|
8
|
+
methods_array = methods_with_no_search
|
9
|
+
class << methods_array
|
10
|
+
def search(query)
|
11
|
+
self.sort_by {|name| Levenshtein.normalized_distance(name.to_s, query.to_s) }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
methods_array
|
15
|
+
end
|
16
|
+
alias_method :methods_with_no_search, :methods
|
17
|
+
alias_method :methods, :methods_with_a_search
|
18
|
+
|
19
|
+
unless method_defined? :method_missing
|
20
|
+
def method_missing(meth, *args, &block); super; end
|
21
|
+
end
|
22
|
+
alias_method :method_missing_without_method_search, :method_missing
|
23
|
+
alias_method :method_missing, :method_missing_with_method_search
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
def method_missing_with_method_search(name,*args,&block)
|
29
|
+
unless (self.respond_to? name)
|
30
|
+
best_match = self.methods.search(name).first
|
31
|
+
$stdout.print "Did you mean #{self.class}.#{best_match} (yes/no)?\n"
|
32
|
+
response = $stdin.gets
|
33
|
+
return self.send(best_match,*args,&block) if /yes/ =~ response
|
34
|
+
end
|
35
|
+
method_missing_without_method_search name,*args,&block
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "DidYouMean" do
|
4
|
+
|
5
|
+
context "methods search" do
|
6
|
+
subject do
|
7
|
+
Class.new{include(DidYouMean)}.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should search methods by textual proximity" do
|
11
|
+
def subject.foo; end
|
12
|
+
subject.methods.search(:fo).first.should == "foo"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should put javascript_driver method in first place when searching by default_javascript_driver" do
|
16
|
+
def subject.javascript_driver; end
|
17
|
+
subject.methods.search(:default_javascript_driver).first.should == "javascript_driver"
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
context "method missing" do
|
23
|
+
subject do
|
24
|
+
Class.new{include(DidYouMean)}.new
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should suggest methods by textual proximity and call it if develepor types yes" do
|
28
|
+
def subject.foo; end
|
29
|
+
$stdout.should_receive(:print).with("Did you mean #{subject.class}.foo (yes/no)?\n")
|
30
|
+
$stdin.should_receive(:gets).and_return('yes')
|
31
|
+
subject.should_receive(:foo)
|
32
|
+
subject.fo
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should suggest methods by textual proximity and dont call it if develepor types no" do
|
36
|
+
def subject.foo; end
|
37
|
+
$stdout.should_receive(:print).with("Did you mean #{subject.class}.foo (yes/no)?\n")
|
38
|
+
$stdin.should_receive(:gets).and_return('no')
|
39
|
+
subject.should_not_receive(:foo)
|
40
|
+
lambda {subject.fo}.should raise_error(NoMethodError)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: did-you-mean
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Leonardo Bessa
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-12 00:00:00 -03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: levenshtein
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 2
|
44
|
+
- 0
|
45
|
+
version: 0.2.0
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
description: Did-you-mean compares the misspelled method name with all other method names and suggests the one with closest textual match.
|
49
|
+
email: leobessa@gmail.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- LICENSE
|
56
|
+
- README.rdoc
|
57
|
+
files:
|
58
|
+
- .document
|
59
|
+
- .gitignore
|
60
|
+
- LICENSE
|
61
|
+
- README.rdoc
|
62
|
+
- Rakefile
|
63
|
+
- VERSION
|
64
|
+
- did-you-mean.gemspec
|
65
|
+
- lib/did-you-mean.rb
|
66
|
+
- spec/did-you-mean_spec.rb
|
67
|
+
- spec/spec.opts
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
has_rdoc: true
|
70
|
+
homepage: http://github.com/leobessa/did-you-mean
|
71
|
+
licenses: []
|
72
|
+
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options:
|
75
|
+
- --charset=UTF-8
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
requirements: []
|
93
|
+
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.3.6
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Runtime corrector for misspelled method calls
|
99
|
+
test_files:
|
100
|
+
- spec/did-you-mean_spec.rb
|
101
|
+
- spec/spec_helper.rb
|