guessmethod 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,4 +1,7 @@
1
- == 0.0.2 2007-07-18
1
+ == 0.0.3 2007-07-18
2
2
 
3
- * 1 minor fix:
4
- * Added HighLine as a dependency
3
+ * 1 major bit:
4
+ * added an override for const_missing, now you can mistype even more!
5
+ * a few minor bits:
6
+ * DRYed up getting the replacement candidates
7
+ * actively dirtied code that needed cleaning (I'm looking at you GuessMethod::Outputter)
data/lib/guessmethod.rb CHANGED
@@ -24,28 +24,48 @@ module GuessMethod
24
24
  end
25
25
  alias_method :old_method_missing, :method_missing
26
26
  alias_method :method_missing, :guess_method_missing
27
+
28
+ unless respond_to? :const_missing
29
+ def self.const_missing(sym); super; end
30
+ end
31
+ base.extend(GuessMethodClassMethods)
32
+ class << self
33
+ alias_method :old_const_missing, :const_missing
34
+ alias_method :const_missing, :guess_const_missing
35
+ end
27
36
  end
28
37
  end
29
38
 
30
39
  def guess_method_missing(meth, *args, &block)
31
- minimum_distance = self.methods.map {|m| m.levenshtein(meth.to_s,
32
- Options[:insert_weight], Options[:delete_weight],
33
- Options[:substitution_weight])}.min
34
- if minimum_distance <= Options[:threshold]
35
- closest_methods = self.methods.find_all {|m| m.levenshtein(meth.to_s,
40
+ possible_methods = Guesser.find_closest(self.methods, meth)
41
+ case possible_methods.size
42
+ when 1
43
+ call_method = possible_methods.first
44
+ $stderr.puts Outputter.replacing_method(meth, call_method, self)
45
+ self.send(call_method, *args, &block)
46
+ when 0
47
+ $stderr.puts Outputter.no_method_in_threshold(meth, self)
48
+ old_method_missing(meth, *args, &block)
49
+ else
50
+ $stderr.puts Outputter.ambiguous_method(meth, possible_methods, self)
51
+ old_method_missing(meth, *args, &block)
52
+ end
53
+ end
54
+
55
+ # GuessMethod::Guesser uses map, min, and levensthtein to find the closest
56
+ # match(es) in an array of strings
57
+ class Guesser
58
+ def self.find_closest(haystack, needle)
59
+ closest_distance = haystack.map {|x| x.levenshtein(needle.to_s,
36
60
  Options[:insert_weight], Options[:delete_weight],
37
- Options[:substitution_weight]) == minimum_distance}
38
- if closest_methods.size == 1
39
- call_method = closest_methods.first
40
- $stderr.puts Outputter.replacing_method(meth, call_method, self)
41
- self.send(call_method, *args, &block)
61
+ Options[:substitution_weight])}.min
62
+ if closest_distance <= Options[:threshold]
63
+ haystack.find_all {|x| x.levenshtein(needle.to_s,
64
+ Options[:insert_weight], Options[:delete_weight],
65
+ Options[:substitution_weight]) == closest_distance}
42
66
  else
43
- $stderr.puts Outputter.ambiguous_method(meth, closest_methods, self)
44
- old_method_missing(meth, *args, &block)
67
+ []
45
68
  end
46
- else
47
- $stderr.puts Outputter.no_method_in_threshold(meth, self)
48
- old_method_missing(meth, *args, &block)
49
69
  end
50
70
  end
51
71
 
@@ -90,8 +110,55 @@ module GuessMethod
90
110
  message << Outputter.object(obj)
91
111
  end
92
112
 
113
+ def self.ambigious_const(sym, possibilities, obj) #:nodoc:
114
+ message = Formatter.color('ambigious constant:', :red)
115
+ message << ' '
116
+ message << Formatter.color(sym, :green)
117
+ message << ' possible matches '
118
+ message << possibilities.map {|m| Formatter.color(m, :cyan)}.join(', ')
119
+ message << ' for '
120
+ message << Outputter.object(obj)
121
+ end
122
+
123
+ def self.no_const_in_threshold(sym, obj) #:nodoc:
124
+ message = Formatter.color('no constant in threshold:', :red)
125
+ message << ' for '
126
+ message << Formatter.color(sym, :green)
127
+ message << ', sending to '
128
+ message << Formatter.color(obj, :green, :bold)
129
+ message << "'s const_missing"
130
+ end
131
+
132
+ def self.replacing_const(sym, call_const, obj) #:nodoc:
133
+ message = Formatter.color('attention:', :red)
134
+ message << ' replacing non-existant constant '
135
+ message << Formatter.color(sym, :cyan)
136
+ message << ' with '
137
+ message << Formatter.color(call_const, :cyan)
138
+ message << ' for '
139
+ message << Formatter.color(obj, :green, :bold)
140
+ end
141
+
93
142
  end
94
143
 
95
144
  end
96
145
 
146
+ module GuessMethodClassMethods
147
+ def guess_const_missing(sym)
148
+ possible_consts = GuessMethod::Guesser.find_closest(self.constants, sym)
149
+ case possible_consts.size
150
+ when 1
151
+ call_const = possible_consts.first
152
+ $stderr.puts GuessMethod::Outputter.replacing_const(sym, call_const, self) #FIXME
153
+ self.const_get(call_const)
154
+ when 0
155
+ $stderr.puts GuessMethod::Outputter.no_const_in_threshold(sym, self)
156
+ old_const_missing(sym)
157
+ else
158
+ $stderr.puts GuessMethod::Outputter.ambigious_const(sym, possible_consts, self)
159
+ old_const_missing(sym)
160
+ end
161
+ end
162
+ end
163
+
97
164
  require 'guessmethod/version'
@@ -2,7 +2,7 @@ module GuessMethod #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 2
5
+ TINY = 3
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/website/index.html CHANGED
@@ -33,7 +33,7 @@
33
33
  <h1>Guess Method</h1>
34
34
  <div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/guessmethod"; return false'>
35
35
  <p>Get Version</p>
36
- <a href="http://rubyforge.org/projects/guessmethod" class="numbers">0.0.2</a>
36
+ <a href="http://rubyforge.org/projects/guessmethod" class="numbers">0.0.3</a>
37
37
  </div>
38
38
  <h1>&#x2192; &#8216;guessmethod&#8217;</h1>
39
39
 
@@ -45,6 +45,18 @@
45
45
  slow you down. Let GuessMethod take care of you.</p>
46
46
 
47
47
 
48
+ <h3>How</h3>
49
+
50
+
51
+ <p>GuessMethod aliases out method_missing and const_missing and replaces it with
52
+ a version that tries to find a close match for your possibly mistyped method or
53
+ constant. You can see that GuessMethod has already outgrown its name.</p>
54
+
55
+
56
+ <p>When GuessMethod can&#8217;t figure anything out, it lets the original method_missing or
57
+ const_missing handle the problem.</p>
58
+
59
+
48
60
  <h2>Installing</h2>
49
61
 
50
62
 
@@ -58,24 +70,21 @@ slow you down. Let GuessMethod take care of you.</p>
58
70
  true
59
71
  002:0&gt; class Object; include GuessMethod; end
60
72
  Object
61
- 003:0&gt; [’1′, ‘2′, ‘3′].mp {|x| x.toi}
62
- attention: sending map instead of mp to [”1″, “2″, “3″]:Array
63
- attention: sending to_i instead of toi to “1″:String
64
- attention: sending to_i instead of toi to “2″:String
65
- attention: sending to_i instead of toi to “3″:String
66
- [1, 2, 3]
67
- 004:0&gt; [’1′,’2′,’3′].mp {|x| x.to_}
68
- attention: sending map instead of mp to [”1″, “2″, “3″]:Array
69
- ambiguous method to_, possible matches to_a, to_i, to_s, to_f for “1″:String
70
- NoMethodError: undefined method `to_’ for “1″:String
71
- from ./mm.rb:9:in `old_method_missing’
72
- from ./mm.rb:34:in `method_missing’
73
+ 003:0&gt; Stirng.tos
74
+ attention: replacing non-existant constant Stirng with String for Object
75
+ attention: sending to_s instead of tos to String:Class
76
+ "String"
77
+ 004:0&gt; ['1','2','3'].mp {|x| x.to_}
78
+ attention: sending map instead of mp to ["1", "2", "3"]:Array
79
+ ambigious method: to_ possible matches to_a, to_i, to_s, to_f for "1":String
80
+ NoMethodError: undefined method `to_' for "1":String
81
+ from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.0.3/lib/guessmethod.rb:23:in `old_method_missing'
82
+ from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.0.3/lib/guessmethod.rb:50:in `method_missing'
73
83
  from (irb):4
74
- from ./mm.rb:25:in `map
75
- from ./mm.rb:25:in `send
76
- from ./mm.rb:25:in `method_missing
84
+ from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.0.3/lib/guessmethod.rb:44:in `map'
85
+ from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.0.3/lib/guessmethod.rb:44:in `send'
86
+ from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.0.3/lib/guessmethod.rb:44:in `method_missing'
77
87
  from (irb):4
78
- from (irb):3
79
88
  005:0&gt; eixt
80
89
  attention: sending exit instead of eixt to main:Object
81
90
  mvb:~ cms$</pre>
@@ -86,6 +95,9 @@ mvb:~ cms$</pre>
86
95
  <p>You can submit patches through the RubyForge page for <a href="http://rubyforge.org/projects/guessmethod/">guessmethod</a>.</p>
87
96
 
88
97
 
98
+ <p>You can check out guessmethod from the svn repository at http://code.tie-rack.org/cms/guessmethod/.</p>
99
+
100
+
89
101
  <p>Read the <a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/">8 steps for fixing other people&#8217;s code</a></p>
90
102
 
91
103
 
@@ -100,7 +112,7 @@ mvb:~ cms$</pre>
100
112
 
101
113
  <p>Comments are welcome. Send an email to chris @@ tie-rack .. org.</p>
102
114
  <p class="coda">
103
- <a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, 19th July 2007<br>
115
+ <a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, 22nd July 2007<br>
104
116
  Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
105
117
  </p>
106
118
  </div>
data/website/index.txt CHANGED
@@ -8,6 +8,15 @@ h2. What
8
8
  GuessMethod is an aggressive spell checker for irb. Don't let sloppy typing
9
9
  slow you down. Let GuessMethod take care of you.
10
10
 
11
+ h3. How
12
+
13
+ GuessMethod aliases out method_missing and const_missing and replaces it with
14
+ a version that tries to find a close match for your possibly mistyped method or
15
+ constant. You can see that GuessMethod has already outgrown its name.
16
+
17
+ When GuessMethod can't figure anything out, it lets the original method_missing or
18
+ const_missing handle the problem.
19
+
11
20
  h2. Installing
12
21
 
13
22
  <pre syntax="ruby">sudo gem install guessmethod</pre>
@@ -19,24 +28,21 @@ h2. Demonstration of usage
19
28
  true
20
29
  002:0> class Object; include GuessMethod; end
21
30
  Object
22
- 003:0> [’1′, ‘2′, ‘3′].mp {|x| x.toi}
23
- attention: sending map instead of mp to [”1″, “2″, “3″]:Array
24
- attention: sending to_i instead of toi to “1″:String
25
- attention: sending to_i instead of toi to “2″:String
26
- attention: sending to_i instead of toi to “3″:String
27
- [1, 2, 3]
28
- 004:0> [’1′,’2′,’3′].mp {|x| x.to_}
29
- attention: sending map instead of mp to [”1″, “2″, “3″]:Array
30
- ambiguous method to_, possible matches to_a, to_i, to_s, to_f for “1″:String
31
- NoMethodError: undefined method `to_’ for “1″:String
32
- from ./mm.rb:9:in `old_method_missing’
33
- from ./mm.rb:34:in `method_missing’
31
+ 003:0> Stirng.tos
32
+ attention: replacing non-existant constant Stirng with String for Object
33
+ attention: sending to_s instead of tos to String:Class
34
+ "String"
35
+ 004:0> ['1','2','3'].mp {|x| x.to_}
36
+ attention: sending map instead of mp to ["1", "2", "3"]:Array
37
+ ambigious method: to_ possible matches to_a, to_i, to_s, to_f for "1":String
38
+ NoMethodError: undefined method `to_' for "1":String
39
+ from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.0.3/lib/guessmethod.rb:23:in `old_method_missing'
40
+ from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.0.3/lib/guessmethod.rb:50:in `method_missing'
34
41
  from (irb):4
35
- from ./mm.rb:25:in `map
36
- from ./mm.rb:25:in `send
37
- from ./mm.rb:25:in `method_missing
42
+ from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.0.3/lib/guessmethod.rb:44:in `map'
43
+ from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.0.3/lib/guessmethod.rb:44:in `send'
44
+ from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.0.3/lib/guessmethod.rb:44:in `method_missing'
38
45
  from (irb):4
39
- from (irb):3
40
46
  005:0> eixt
41
47
  attention: sending exit instead of eixt to main:Object
42
48
  mvb:~ cms$</pre>
@@ -45,6 +51,8 @@ h2. How to submit patches
45
51
 
46
52
  You can submit patches through the RubyForge page for "guessmethod":http://rubyforge.org/projects/guessmethod/.
47
53
 
54
+ You can check out guessmethod from the svn repository at http://code.tie-rack.org/cms/guessmethod/.
55
+
48
56
  Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/
49
57
 
50
58
  h2. License
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.0.2
7
- date: 2007-07-19 00:00:00 -06:00
6
+ version: 0.0.3
7
+ date: 2007-07-22 00:00:00 -06:00
8
8
  summary: hijacks method_missing to act as a spellchecker
9
9
  require_paths:
10
10
  - lib