slide_rule 0.1.1 → 0.1.2
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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7118aaafe84fdcdf36f2186bfe731f935576e2d2
|
4
|
+
data.tar.gz: e2aa86ba48acbaea385c76ee188e15795ce6fb7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3892833589520f1ad3bbcfe83d1c4c18240cedc522ce45101d34a28cb7ba4e4ccb611f38e49a08cfba839b3ce750e63146d2286eea9d534b14554dcbed5deea
|
7
|
+
data.tar.gz: 1d01a8617ac75abf8ef2d388472f243bb695924c0492ffbf358d4323d6b1b2c8af113dacd1ab92222ae036d7cc43871367f8c09bc6281cc1ca2e6b706434bc88
|
@@ -81,7 +81,11 @@ module SlideRule
|
|
81
81
|
return calculator.new if calculator.is_a?(Class)
|
82
82
|
|
83
83
|
klass_name = "#{calculator.to_s.split('_').collect(&:capitalize).join}"
|
84
|
-
klass =
|
84
|
+
klass = begin
|
85
|
+
::SlideRule::DistanceCalculators.const_get(klass_name)
|
86
|
+
rescue(::NameError)
|
87
|
+
nil
|
88
|
+
end
|
85
89
|
|
86
90
|
fail ArgumentError, "Unable to find calculator #{klass_name}" if klass.nil?
|
87
91
|
|
@@ -106,19 +110,30 @@ module SlideRule
|
|
106
110
|
end
|
107
111
|
end
|
108
112
|
|
113
|
+
# Prepares a duplicate of given rules hash with normalized weights and calculator instances
|
114
|
+
#
|
109
115
|
def prepare_rules(rules)
|
110
|
-
prepared_rules = rules.
|
111
|
-
|
112
|
-
|
116
|
+
prepared_rules = rules.each_with_object({}) do |(attribute, rule), copy|
|
117
|
+
rule = copy[attribute] = safe_dup(rule)
|
118
|
+
|
113
119
|
if rule[:type]
|
114
120
|
puts 'Rule key `:type` is deprecated. Use `:calculator` instead.'
|
115
121
|
rule[:calculator] = rule[:type]
|
116
122
|
end
|
117
123
|
|
118
124
|
rule[:calculator] = get_calculator(rule[:calculator])
|
125
|
+
|
126
|
+
copy
|
119
127
|
end
|
128
|
+
prepared_rules = normalize_weights(prepared_rules)
|
120
129
|
|
121
130
|
prepared_rules
|
122
131
|
end
|
132
|
+
|
133
|
+
def safe_dup(obj)
|
134
|
+
obj.dup
|
135
|
+
rescue
|
136
|
+
obj
|
137
|
+
end
|
123
138
|
end
|
124
139
|
end
|
data/lib/slide_rule/version.rb
CHANGED
@@ -147,25 +147,49 @@ describe ::SlideRule::DistanceCalculator do
|
|
147
147
|
end
|
148
148
|
end
|
149
149
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
description: {
|
154
|
-
weight: 1.00,
|
155
|
-
type: CustomCalc
|
156
|
-
}
|
157
|
-
)
|
158
|
-
end
|
159
|
-
|
160
|
-
it 'should raise error if not valid calculator' do
|
161
|
-
expect do
|
150
|
+
describe '#initialize' do
|
151
|
+
context 'validates rules on initialize' do
|
152
|
+
it 'should allow :type' do
|
162
153
|
::SlideRule::DistanceCalculator.new(
|
163
154
|
description: {
|
164
155
|
weight: 1.00,
|
165
|
-
|
156
|
+
type: CustomCalc
|
166
157
|
}
|
167
158
|
)
|
168
|
-
end
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'should not modify input rule hash' do
|
162
|
+
rules = {
|
163
|
+
description: {
|
164
|
+
weight: 1.0,
|
165
|
+
calculator: CustomCalc
|
166
|
+
},
|
167
|
+
name: {
|
168
|
+
weight: 1.0,
|
169
|
+
type: CustomCalc
|
170
|
+
}
|
171
|
+
}
|
172
|
+
::SlideRule::DistanceCalculator.new(rules)
|
173
|
+
# Run a second time to ensure that no calculator instance is in rules. Will currently throw an error.
|
174
|
+
::SlideRule::DistanceCalculator.new(rules)
|
175
|
+
|
176
|
+
# :type should still be in original hash
|
177
|
+
expect(rules[:name].key?(:calculator)).to eq(false)
|
178
|
+
|
179
|
+
# :weight should not be normalized in original hash
|
180
|
+
expect(rules[:name][:weight]).to eq(1.0)
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'should raise error if not valid calculator' do
|
184
|
+
expect do
|
185
|
+
::SlideRule::DistanceCalculator.new(
|
186
|
+
description: {
|
187
|
+
weight: 1.00,
|
188
|
+
calculator: :some_junk
|
189
|
+
}
|
190
|
+
)
|
191
|
+
end.to raise_error(::ArgumentError, 'Unable to find calculator SomeJunk')
|
192
|
+
end
|
169
193
|
end
|
170
194
|
end
|
171
195
|
end
|
@@ -1,11 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ::SlideRule::DistanceCalculators::Levenshtein do
|
4
|
+
let(:subject) { described_class.new }
|
5
|
+
|
4
6
|
it 'should calculate perfect match' do
|
5
|
-
expect(
|
7
|
+
expect(subject.calculate('this is a test', 'this is a test')).to eq(0.0)
|
6
8
|
end
|
7
9
|
|
8
10
|
it 'should calculate distance as distance divided by length of longest string' do
|
9
|
-
expect(
|
11
|
+
expect(subject.calculate('this is a test', 'this is a test!').round(4)).to eq((1.0 / 15).round(4))
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should handle nils' do
|
15
|
+
expect(subject.calculate(nil, nil)).to eq(0.0)
|
16
|
+
expect(subject.calculate(nil, 'goodbye')).to eq(1.0)
|
17
|
+
expect(subject.calculate('hello', nil)).to eq(1.0)
|
10
18
|
end
|
11
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slide_rule
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mattnichols
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-11-
|
12
|
+
date: 2015-11-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: vladlev
|