fuzzyhash 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/fuzzy_hash.rb +10 -2
- data/spec/fuzzy_hash_spec.rb +6 -0
- metadata +2 -2
data/VERSION.yml
CHANGED
data/lib/fuzzy_hash.rb
CHANGED
@@ -68,7 +68,15 @@ class FuzzyHash
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def [](key)
|
71
|
-
@hash.key?(key) ? @hash[key] : regex_lookup(key)
|
71
|
+
@hash.key?(key) ? @hash[key] : (lookup = regex_lookup(key)) && lookup && lookup.first
|
72
|
+
end
|
73
|
+
|
74
|
+
def match_with_result(key)
|
75
|
+
if @hash.key?(key)
|
76
|
+
[@hash[key], key]
|
77
|
+
else
|
78
|
+
regex_lookup(key)
|
79
|
+
end
|
72
80
|
end
|
73
81
|
|
74
82
|
private
|
@@ -79,7 +87,7 @@ class FuzzyHash
|
|
79
87
|
def regex_lookup(key)
|
80
88
|
if !@regexes.empty? && key.is_a?(String) && (data = regex_test.match(key))
|
81
89
|
(data_array = data.to_a).each_index do |i|
|
82
|
-
break @regexes[i].last if data_array.at(i+1)
|
90
|
+
break [@regexes[i].last, data_array.at(i+1)] if data_array.at(i+1)
|
83
91
|
end
|
84
92
|
end
|
85
93
|
end
|
data/spec/fuzzy_hash_spec.rb
CHANGED
@@ -14,6 +14,12 @@ describe "Fuzzy hash" do
|
|
14
14
|
l['asdqweasd'].should == 'qwe'
|
15
15
|
end
|
16
16
|
|
17
|
+
it "should accept regexs too with the match" do
|
18
|
+
l = FuzzyHash.new
|
19
|
+
l[/asd.*/] = 'qwe'
|
20
|
+
l.match_with_result('asdqweasd').should == ['qwe', 'asdqweasd']
|
21
|
+
end
|
22
|
+
|
17
23
|
it "should prefer string to regex matches" do
|
18
24
|
l = FuzzyHash.new
|
19
25
|
l['asd'] = 'qwe2'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fuzzyhash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Hull
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-07-21 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|