did_you_mean 0.9.8 → 0.9.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +5 -18
- data/ext/did_you_mean/extconf.rb +2 -7
- data/lib/did_you_mean.rb +1 -0
- data/lib/did_you_mean/core_ext/name_error.rb +3 -1
- data/lib/did_you_mean/core_ext/no_method_error.rb +35 -0
- data/lib/did_you_mean/finders/similar_method_finder.rb +1 -12
- data/lib/did_you_mean/levenshtein.rb +6 -3
- data/lib/did_you_mean/version.rb +1 -1
- data/lib/did_you_mean/word_collection.rb +4 -1
- data/test/all_test.rb +1 -0
- data/test/no_method_error_extension_test.rb +30 -6
- data/test/word_collection_test.rb +26 -0
- metadata +5 -4
- data/ext/did_you_mean/org/yukinishijima/ReceiverCapturer.java +0 -57
- data/lib/did_you_mean/receiver_capturer.jar +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5f3bcb4803e10d0fc470b852daabeb13100378f
|
4
|
+
data.tar.gz: 1d0b43e694999ee1e512f5c55177e8d6e61538a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b62459e83c059e069c7a43a5e67161044757a636bcbb570205cf8bba3b1f21076e9e2843dbcc8030dc8c917297a1a9df4f6442456c6a7b0f6f903a24e677e51d
|
7
|
+
data.tar.gz: a921df196d4a1be1074660d7b6988bae7d81c460d82324bd1ca9201f9c3fa8b488b1682eaeaac856907eb045f801569958a13fffb5cdbd921ee1fafd49baa96f
|
data/Rakefile
CHANGED
@@ -4,28 +4,15 @@ def mri?
|
|
4
4
|
defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby"
|
5
5
|
end
|
6
6
|
|
7
|
-
def jruby?
|
8
|
-
defined?(JRuby)
|
9
|
-
end
|
10
|
-
|
11
7
|
desc "run tests"
|
12
8
|
task default: [:test]
|
13
9
|
|
14
|
-
if mri?
|
15
|
-
|
16
|
-
require 'rake/extensiontask'
|
10
|
+
if mri?
|
11
|
+
require 'rake/extensiontask'
|
17
12
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
elsif jruby?
|
23
|
-
require 'rake/javaextensiontask'
|
24
|
-
|
25
|
-
Rake::JavaExtensionTask.new('did_you_mean') do |ext|
|
26
|
-
ext.name = "receiver_capturer"
|
27
|
-
ext.lib_dir = "lib/did_you_mean"
|
28
|
-
end
|
13
|
+
Rake::ExtensionTask.new('did_you_mean') do |ext|
|
14
|
+
ext.name = "method_missing"
|
15
|
+
ext.lib_dir = "lib/did_you_mean"
|
29
16
|
end
|
30
17
|
|
31
18
|
desc "Run tests"
|
data/ext/did_you_mean/extconf.rb
CHANGED
data/lib/did_you_mean.rb
CHANGED
@@ -9,7 +9,9 @@ class NameError
|
|
9
9
|
|
10
10
|
def to_s_with_did_you_mean
|
11
11
|
msg = original_message
|
12
|
-
|
12
|
+
bt = caller.first(6)
|
13
|
+
|
14
|
+
msg << did_you_mean?.to_s if IGNORED_CALLERS.all? {|ignored| bt.grep(ignored).empty? }
|
13
15
|
msg
|
14
16
|
rescue
|
15
17
|
original_message
|
@@ -0,0 +1,35 @@
|
|
1
|
+
case RUBY_ENGINE
|
2
|
+
when 'ruby'
|
3
|
+
require 'did_you_mean/method_missing'
|
4
|
+
|
5
|
+
when 'jruby'
|
6
|
+
NoMethodError.class_eval do
|
7
|
+
def to_s
|
8
|
+
receiver unless defined?(@receiver)
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def receiver
|
13
|
+
@receiver ||= begin
|
14
|
+
field = JRuby.reference(__message__).java_class.getDeclaredField("object")
|
15
|
+
field.setAccessible(true)
|
16
|
+
field.get(__message__)
|
17
|
+
rescue
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
if JRUBY_VERSION >= '9.0.0.0'
|
25
|
+
def __message__
|
26
|
+
JRuby.reference(self).getMessage
|
27
|
+
end
|
28
|
+
else
|
29
|
+
def __message__
|
30
|
+
error = JRuby.reference(self)
|
31
|
+
error.java_class.getField("message").get(error)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -28,18 +28,7 @@ module DidYouMean
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
if
|
31
|
+
if RUBY_ENGINE == 'ruby' || RUBY_ENGINE == 'jruby'
|
32
32
|
finders["NoMethodError"] = SimilarMethodFinder
|
33
|
-
|
34
|
-
case RUBY_ENGINE
|
35
|
-
when 'ruby'
|
36
|
-
require 'did_you_mean/method_missing'
|
37
|
-
when 'jruby'
|
38
|
-
require 'did_you_mean/receiver_capturer'
|
39
|
-
org.yukinishijima.ReceiverCapturer.setup(JRuby.runtime)
|
40
|
-
NoMethodError.send(:attr, :receiver)
|
41
|
-
else
|
42
|
-
finders.delete("NoMethodError")
|
43
|
-
end
|
44
33
|
end
|
45
34
|
end
|
@@ -12,11 +12,12 @@ module DidYouMean
|
|
12
12
|
x = nil
|
13
13
|
|
14
14
|
# to avoid duplicating an enumerable object, create it outside of the loop
|
15
|
-
|
15
|
+
str2_codepoints = str2.codepoints.to_a
|
16
16
|
|
17
17
|
str1.each_codepoint.with_index(1) do |char1, i|
|
18
|
-
|
19
|
-
|
18
|
+
j = 0
|
19
|
+
while j < m
|
20
|
+
cost = (char1 == str2_codepoints[j]) ? 0 : 1
|
20
21
|
x = min3(
|
21
22
|
d[j+1] + 1, # insertion
|
22
23
|
i + 1, # deletion
|
@@ -24,6 +25,8 @@ module DidYouMean
|
|
24
25
|
)
|
25
26
|
d[j] = i
|
26
27
|
i = x
|
28
|
+
|
29
|
+
j += 1
|
27
30
|
end
|
28
31
|
d[m] = x
|
29
32
|
end
|
data/lib/did_you_mean/version.rb
CHANGED
@@ -15,7 +15,10 @@ module DidYouMean
|
|
15
15
|
target_word = target_word.to_s
|
16
16
|
threshold = threshold(target_word)
|
17
17
|
|
18
|
-
|
18
|
+
map {|word| [Levenshtein.distance(word.to_s, target_word), word] }
|
19
|
+
.select {|distance, _| distance <= threshold }
|
20
|
+
.sort
|
21
|
+
.map(&:last)
|
19
22
|
end
|
20
23
|
|
21
24
|
private
|
data/test/all_test.rb
CHANGED
@@ -1,14 +1,38 @@
|
|
1
1
|
require_relative 'test_helper'
|
2
2
|
|
3
3
|
class NoMethodErrorExtensionTest < Minitest::Test
|
4
|
-
def
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
def test_receiver_with_string
|
5
|
+
receiver = "receiver"
|
6
|
+
error = assert_raises(NoMethodError) do
|
7
|
+
receiver.doesnt_exist
|
8
8
|
end
|
9
|
+
|
10
|
+
assert_same receiver, error.receiver
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_receiver_with_class
|
14
|
+
error = assert_raises(NoMethodError) do
|
15
|
+
Object.doesnt_exist
|
16
|
+
end
|
17
|
+
|
18
|
+
assert_same Object, error.receiver
|
9
19
|
end
|
10
20
|
|
11
|
-
def
|
12
|
-
|
21
|
+
def test_receiver_with_class_after_calling_to_s
|
22
|
+
error = assert_raises(NoMethodError) do
|
23
|
+
Object.doesnt_exist
|
24
|
+
end
|
25
|
+
|
26
|
+
error.to_s
|
27
|
+
assert_same Object, error.receiver
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_receiver_with_class_after_calling_message
|
31
|
+
error = assert_raises(NoMethodError) do
|
32
|
+
Object.doesnt_exist
|
33
|
+
end
|
34
|
+
|
35
|
+
error.message
|
36
|
+
assert_same Object, error.receiver
|
13
37
|
end
|
14
38
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class WordCollectionTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
names = %w(
|
6
|
+
name12
|
7
|
+
name123
|
8
|
+
name1234
|
9
|
+
name12345
|
10
|
+
name123456
|
11
|
+
)
|
12
|
+
|
13
|
+
@word_collection = DidYouMean::WordCollection.new(names)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_similar_to
|
17
|
+
expected = %w(
|
18
|
+
name123456
|
19
|
+
name12345
|
20
|
+
name1234
|
21
|
+
name123
|
22
|
+
)
|
23
|
+
|
24
|
+
assert_equal expected, @word_collection.similar_to("name123456")
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: did_you_mean
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuki Nishijima
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: interception
|
@@ -102,7 +102,6 @@ files:
|
|
102
102
|
- doc/did_you_mean_example.png
|
103
103
|
- ext/did_you_mean/extconf.rb
|
104
104
|
- ext/did_you_mean/method_missing.c
|
105
|
-
- ext/did_you_mean/org/yukinishijima/ReceiverCapturer.java
|
106
105
|
- gemfiles/activerecord_32.gemfile
|
107
106
|
- gemfiles/activerecord_40.gemfile
|
108
107
|
- gemfiles/activerecord_41.gemfile
|
@@ -110,6 +109,7 @@ files:
|
|
110
109
|
- gemfiles/activerecord_edge.gemfile
|
111
110
|
- lib/did_you_mean.rb
|
112
111
|
- lib/did_you_mean/core_ext/name_error.rb
|
112
|
+
- lib/did_you_mean/core_ext/no_method_error.rb
|
113
113
|
- lib/did_you_mean/finders.rb
|
114
114
|
- lib/did_you_mean/finders/name_error_finders.rb
|
115
115
|
- lib/did_you_mean/finders/name_error_finders/similar_class_finder.rb
|
@@ -117,7 +117,6 @@ files:
|
|
117
117
|
- lib/did_you_mean/finders/similar_attribute_finder.rb
|
118
118
|
- lib/did_you_mean/finders/similar_method_finder.rb
|
119
119
|
- lib/did_you_mean/levenshtein.rb
|
120
|
-
- lib/did_you_mean/receiver_capturer.jar
|
121
120
|
- lib/did_you_mean/test_helper.rb
|
122
121
|
- lib/did_you_mean/version.rb
|
123
122
|
- lib/did_you_mean/word_collection.rb
|
@@ -130,6 +129,7 @@ files:
|
|
130
129
|
- test/similar_method_finder_test.rb
|
131
130
|
- test/similar_name_finder_test.rb
|
132
131
|
- test/test_helper.rb
|
132
|
+
- test/word_collection_test.rb
|
133
133
|
homepage: https://github.com/yuki24/did_you_mean
|
134
134
|
licenses:
|
135
135
|
- MIT
|
@@ -164,3 +164,4 @@ test_files:
|
|
164
164
|
- test/similar_method_finder_test.rb
|
165
165
|
- test/similar_name_finder_test.rb
|
166
166
|
- test/test_helper.rb
|
167
|
+
- test/word_collection_test.rb
|
@@ -1,57 +0,0 @@
|
|
1
|
-
package org.yukinishijima;
|
2
|
-
|
3
|
-
import org.jruby.anno.JRubyMethod;
|
4
|
-
import org.jruby.exceptions.RaiseException;
|
5
|
-
import org.jruby.runtime.builtin.IRubyObject;
|
6
|
-
import org.jruby.runtime.Block;
|
7
|
-
import org.jruby.runtime.ThreadContext;
|
8
|
-
import org.jruby.runtime.Visibility;
|
9
|
-
import org.jruby.Ruby;
|
10
|
-
import org.jruby.RubyArray;
|
11
|
-
import org.jruby.RubyException;
|
12
|
-
import org.jruby.RubyModule;
|
13
|
-
import org.jruby.RubyNoMethodError;
|
14
|
-
import org.jruby.internal.runtime.methods.CallConfiguration;
|
15
|
-
import org.jruby.internal.runtime.methods.DynamicMethod;
|
16
|
-
import org.jruby.internal.runtime.methods.JavaMethod.JavaMethodNBlock;
|
17
|
-
|
18
|
-
public class ReceiverCapturer extends JavaMethodNBlock {
|
19
|
-
private DynamicMethod methodMissingMethod;
|
20
|
-
|
21
|
-
public ReceiverCapturer(RubyModule implementationClass, DynamicMethod methodMissingMethod) {
|
22
|
-
super(implementationClass, Visibility.PRIVATE, CallConfiguration.FrameFullScopeNone);
|
23
|
-
this.methodMissingMethod = methodMissingMethod;
|
24
|
-
}
|
25
|
-
|
26
|
-
@Override
|
27
|
-
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
|
28
|
-
try {
|
29
|
-
return this.methodMissingMethod.call(context, self, clazz, name, args, block);
|
30
|
-
} catch (RaiseException exception) {
|
31
|
-
appendReceiverToException(exception, self);
|
32
|
-
throw exception;
|
33
|
-
}
|
34
|
-
}
|
35
|
-
|
36
|
-
@JRubyMethod
|
37
|
-
public static void setup(Ruby runtime) {
|
38
|
-
RubyModule module = runtime.defineModule("Kernel");
|
39
|
-
|
40
|
-
DynamicMethod privateMethodMissing = runtime.getPrivateMethodMissing();
|
41
|
-
DynamicMethod protectedMethodMissing = runtime.getProtectedMethodMissing();
|
42
|
-
DynamicMethod superMethodMissing = runtime.getSuperMethodMissing();
|
43
|
-
DynamicMethod normalMethodMissing = runtime.getNormalMethodMissing();
|
44
|
-
|
45
|
-
runtime.setPrivateMethodMissing(new ReceiverCapturer(module, privateMethodMissing));
|
46
|
-
runtime.setProtectedMethodMissing(new ReceiverCapturer(module, protectedMethodMissing));
|
47
|
-
runtime.setSuperMethodMissing(new ReceiverCapturer(module, superMethodMissing));
|
48
|
-
runtime.setNormalMethodMissing(new ReceiverCapturer(module, normalMethodMissing));
|
49
|
-
}
|
50
|
-
|
51
|
-
private void appendReceiverToException(RaiseException exception, IRubyObject self) {
|
52
|
-
if (exception.getException() instanceof RubyNoMethodError) {
|
53
|
-
RubyNoMethodError noMethodError = (RubyNoMethodError) exception.getException();
|
54
|
-
noMethodError.setInstanceVariable("@receiver", self);
|
55
|
-
}
|
56
|
-
}
|
57
|
-
}
|
Binary file
|