guessmethod 0.1.2 → 0.1.3

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/History.txt CHANGED
@@ -1,3 +1,11 @@
1
+ == 0.1.3 2007-11-16
2
+
3
+ * Cleaned up / reworked comments and spec to reflect the fact that the original method/const_missing goes first.
4
+ * Got spec coverage to 100%.
5
+ * Slightly modified string/levenshtein to fix coverage issue (my idiocy? rcov bug?)
6
+ * GuessConstant no longer checks if a class/module responds to const_missing. They all do. So that check and the const_missing definition are unnecessary.
7
+ * Added rcov rake task.
8
+
1
9
  == 0.1.2 2007-11-12
2
10
 
3
11
  * GuessMethod and GuessConstant now both try the original method_ or const_missing, capturing the error, before trying. On failure, raises the original error.
data/Manifest.txt CHANGED
@@ -9,6 +9,7 @@ lib/string/levenshtein.rb
9
9
  scripts/txt2html
10
10
  setup.rb
11
11
  spec/guessmethod_spec.rb
12
+ spec/leven_spec.rb
12
13
  spec/spec_helper.rb
13
14
  website/index.html
14
15
  website/index.txt
data/README.txt CHANGED
@@ -14,7 +14,7 @@ If you accidentally try to access <tt>File::RDWr</tt>, you'll get <tt>File::RDWR
14
14
 
15
15
  == Options
16
16
 
17
- <tt>GuessMethod::Options</tt> is a hash with configuration values.
17
+ <tt>GuessMethod::GuessMethodOptions</tt> is a hash with configuration values.
18
18
 
19
19
  * <code>GuessMethodOptions[:insert_weight]</code>, <code>GuessMethodOptions[:delete_weight]</code>, and <code>GuessMethodOptions[:substitution_weight]</code> change how the levenshtein distance is calculated between method and constant names. The default values are all 1.
20
20
  * <code>GuessMethodOptions[:threshold]</code> determines what levenshtein distance a method or constant must be within to count as a possible match. Values over 2 can give you unpredictable results, especially for short names. The default value is 2.
data/Rakefile CHANGED
@@ -133,6 +133,12 @@ Spec::Rake::SpecTask.new do |t|
133
133
  t.spec_files = FileList['spec/*_spec.rb']
134
134
  end
135
135
 
136
+ require 'rcov/rcovtask'
137
+ Rcov::RcovTask.new do |t|
138
+ t.test_files = FileList['spec/*spec.rb']
139
+ # t.verbose = true # uncomment to see the executed command
140
+ end
141
+
136
142
  desc "Default task is to run specs"
137
143
  task :default => :spec
138
144
 
@@ -2,7 +2,7 @@ module GuessMethod #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 2
5
+ TINY = 3
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/lib/guessmethod.rb CHANGED
@@ -2,11 +2,8 @@ require 'string/levenshtein'
2
2
 
3
3
  # The GuessMethod module aliases out method_missing and
4
4
  # replaces it with its own, which attempts to find a
5
- # similarly named method callable on the object at hand.
6
- # If it finds one, it calls that instead, and prints
7
- # a message to stderr explaining as much. If it doesn't
8
- # find a suitable method, the orignial method_missing
9
- # gets called.
5
+ # similarly named method callable on the object at hand
6
+ # if the objects original method_missing fails.
10
7
  module GuessMethod
11
8
 
12
9
  GuessMethodOptions = {
@@ -56,17 +53,11 @@ module GuessMethod
56
53
 
57
54
  # The GuessConstant module aliases out const_missing and
58
55
  # replaces it with its own, which attempts to find a
59
- # similarly named constant for the object at hand.
60
- # If it finds one, it returns that instead, and prints
61
- # a message to stderr explaining as much. If it doesn't
62
- # find a suitable constant, the orignial const_missing
63
- # gets called.
56
+ # similarly named constant for the object at hand
57
+ # if the original method_missing fails.
64
58
  module GuessConstant
65
59
  def self.extended(base) #:nodoc:
66
60
  base.class_eval do
67
- unless respond_to? :const_missing
68
- def self.const_missing(sym); super; end
69
- end
70
61
  class << self
71
62
  alias_method :unguessed_const_missing, :const_missing
72
63
  alias_method :const_missing, :guess_const_missing
@@ -1,6 +1,7 @@
1
1
  class String
2
2
 
3
- # Calculates the levenshtein distance between strings. This method was taken from
3
+ # Calculates the levenshtein distance between strings
4
+ # This method was modified from the version appearing in
4
5
  # The Ruby Way, 2nd Edition, Hal Fulton, pp. 97-98
5
6
  def levenshtein(other, ins=2, del=2, sub=1)
6
7
 
@@ -21,13 +22,14 @@ class String
21
22
  # populate matrix
22
23
  for i in 1..other.length
23
24
  for j in 1..self.length
24
- # critical comparison
25
- dm[i][j] = [
25
+ # critical comparison
26
+ comparisons_array = [
26
27
  dm[i-1][j-1] +
27
28
  (self[j-1] == other[i-1] ? 0 : sub),
28
29
  dm[i][j-1] + ins,
29
30
  dm[i-1][j] + del
30
- ].min
31
+ ]
32
+ dm[i][j] = comparisons_array.min
31
33
  end
32
34
  end
33
35
 
@@ -3,12 +3,21 @@ require 'lib/guessmethod.rb'
3
3
 
4
4
  describe 'GuessMethod naturally' do
5
5
 
6
+ before(:all) do
7
+ class Ambiguous
8
+ FOOD = 2
9
+ FOOF = 3
10
+ def food; end
11
+ def foof; end
12
+ end
13
+ end
14
+
6
15
  it "should define guess_method_missing on Object" do
7
16
  Object.should.respond_to? :guess_method_missing
8
17
  end
9
18
 
10
19
  it 'should define guess_const_missing on Object' do
11
- Object.should.respond_to? 'guess_const_missing'
20
+ Object.should.respond_to? :guess_const_missing
12
21
  end
13
22
 
14
23
  it "should figure out toi means to_i" do
@@ -30,7 +39,7 @@ describe 'GuessMethod naturally' do
30
39
  it "should be off if it gets turned off" do
31
40
  GuessMethodOptions[:active] = false
32
41
  lambda{Class.nw}.should raise_error(NoMethodError)
33
- lambda{Cass == Class}.should raise_error(NameError)
42
+ lambda{Cass}.should raise_error(NameError)
34
43
  GuessMethodOptions[:active] = true
35
44
  end
36
45
 
@@ -48,6 +57,18 @@ describe 'GuessMethod naturally' do
48
57
  it "should replace RUBY_Version with RUBY_VERSION" do
49
58
  RUBY_Version.should == RUBY_VERSION
50
59
  end
60
+
61
+ it 'should raise NoMethodError with an ambiguous method' do
62
+ lambda{Ambiguous.new.foo}.should raise_error(NoMethodError)
63
+ end
64
+
65
+ it "should raise NameError when there's no constant in threshold" do
66
+ lambda{File::NOTACONSTANT}.should raise_error(NameError)
67
+ end
68
+
69
+ it 'should raise NameError with an ambiguous constant' do
70
+ lambda{Ambiguous::FOO}.should raise_error(NameError)
71
+ end
51
72
  end
52
73
 
53
74
  describe "On a method_missing-defining class, GuessMethod" do
@@ -64,12 +85,40 @@ describe "On a method_missing-defining class, GuessMethod" do
64
85
  end
65
86
  end
66
87
 
67
- it "should pass on to the class' method_missing past the threshold" do
88
+ it "should allow method_missing to handle its cases" do
68
89
  A.new.foo.should == 'bar'
69
90
  end
70
91
 
71
92
  it "should let an object's NoMethodError through if there's no method in the threshold" do
72
93
  lambda{A.new.not_a_method}.should raise_error(NoMethodError)
94
+ end
95
+ end
96
+
97
+ describe 'On a const_missing-defining class, GuessMethod' do
98
+ before(:all) do
99
+ class Consty
100
+ BAR = 'French Toast'
101
+ def self.const_missing(sym)
102
+ case sym
103
+ when :FOO
104
+ 42
105
+ else
106
+ raise NameError
107
+ end
108
+ end
109
+ include GuessMethod
110
+ end
73
111
  end
74
112
 
75
- end
113
+ it 'should allow const_missing to handle its cases' do
114
+ Consty::FOO.should == 42
115
+ end
116
+
117
+ it "should let an object's NameError through if there's no constant in the threshold" do
118
+ lambda{Consty::NOWAYJOSE}.should raise_error(NameError)
119
+ end
120
+
121
+ it 'should supply guessed constants (the other specs in this will work without including GuessMethod)' do
122
+ Consty::BAZ.should == 'French Toast'
123
+ end
124
+ end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require 'lib/string/levenshtein.rb'
3
+
4
+ describe 'Levenshtein' do
5
+
6
+ it 'should be 0 for equal strings' do
7
+ 'foo'.levenshtein('foo').should == 0
8
+ end
9
+
10
+ it 'should be 1 for strings that differ by only a subsitution' do
11
+ 'foo'.levenshtein('fof').should == 1
12
+ end
13
+
14
+ it 'should be 2 for strings that differ by only an insertion' do
15
+ 'foo'.levenshtein('fodo').should == 2
16
+ end
17
+
18
+ it 'should be 2 for strings that differ by only a deletion' do
19
+ 'foo'.levenshtein('fo').should == 2
20
+ end
21
+
22
+ it 'should be reflexive' do
23
+ string1 = 'this is one string'
24
+ string2 = 'this is a different one'
25
+ string1.levenshtein(string2).should == string2.levenshtein(string1)
26
+ end
27
+
28
+ it 'should calculate correctly longer distances' do
29
+ string1 = 'this is one string'
30
+ string2 = 'this is a different one'
31
+ string1.levenshtein(string2).should == 17
32
+ end
33
+
34
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: guessmethod
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.2
7
- date: 2007-11-12 00:00:00 -07:00
6
+ version: 0.1.3
7
+ date: 2007-11-16 00:00:00 -07:00
8
8
  summary: an aggressive spell checker for irb
9
9
  require_paths:
10
10
  - lib
@@ -40,6 +40,7 @@ files:
40
40
  - scripts/txt2html
41
41
  - setup.rb
42
42
  - spec/guessmethod_spec.rb
43
+ - spec/leven_spec.rb
43
44
  - spec/spec_helper.rb
44
45
  - website/index.html
45
46
  - website/index.txt