slide_rule 0.2.2 → 1.0.0
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 +4 -4
- data/lib/slide_rule/distance_calculator.rb +4 -3
- data/lib/slide_rule/distance_calculators/day_of_month.rb +1 -1
- data/lib/slide_rule/distance_calculators/day_of_year.rb +5 -5
- data/lib/slide_rule/distance_calculators/levenshtein.rb +1 -1
- data/lib/slide_rule/version.rb +1 -1
- data/slide_rule.gemspec +1 -1
- data/spec/slide_rule/distance_calculator_spec.rb +2 -2
- data/spec/slide_rule/distance_calculators/day_of_year_spec.rb +13 -0
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 66f0ba46303354b7b468dcc13103acffe6e53b65
|
|
4
|
+
data.tar.gz: 92334c787512fe9b2b6b3b7406d7709d6de27c16
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 85b76abcb7b43301d8a16a5200e69ba2a9f734dac717e983e930da2fdc258261a61936481672bdf2f9523f0270254fb5e81010c0d8a764151f8a1f9525e713fa
|
|
7
|
+
data.tar.gz: 29f0fa854708b4abb50c5ba563abf9334f6a3b410fa4bea7d85f320ac86d9ddaa12b1ab0ac22481cf0537bfc3ccea8b480af64c37d273aeb70ed0a306a0b3903
|
|
@@ -58,6 +58,7 @@ module SlideRule
|
|
|
58
58
|
# :attribute_name => {
|
|
59
59
|
# :weight => 0.90,
|
|
60
60
|
# :calculator => :distance_calculator,
|
|
61
|
+
# :threshold => 30
|
|
61
62
|
# }
|
|
62
63
|
# }
|
|
63
64
|
def calculate_distance(i1, i2)
|
|
@@ -69,11 +70,11 @@ module SlideRule
|
|
|
69
70
|
private
|
|
70
71
|
|
|
71
72
|
def calculate_weighted_distances(i1, i2)
|
|
72
|
-
distances = @rules.map do |attribute,
|
|
73
|
+
distances = @rules.map do |attribute, options|
|
|
73
74
|
val1 = i1.send(attribute)
|
|
74
75
|
val2 = i2.send(attribute)
|
|
75
|
-
distance =
|
|
76
|
-
next { distance: distance.to_f, weight:
|
|
76
|
+
distance = options[:calculator].calculate(val1, val2, options)
|
|
77
|
+
next { distance: distance.to_f, weight: options[:weight] } unless distance.nil?
|
|
77
78
|
|
|
78
79
|
nil
|
|
79
80
|
end
|
|
@@ -7,7 +7,7 @@ module SlideRule
|
|
|
7
7
|
# Calculates distance using 15 as the max point.
|
|
8
8
|
# Does not take into account the number of days in the actual month being considered.
|
|
9
9
|
#
|
|
10
|
-
def calculate(first, second)
|
|
10
|
+
def calculate(first, second, options={})
|
|
11
11
|
return nil if first.nil? || second.nil?
|
|
12
12
|
first = cleanse_date(first)
|
|
13
13
|
second = cleanse_date(second)
|
|
@@ -3,15 +3,15 @@ module SlideRule
|
|
|
3
3
|
class DayOfYear
|
|
4
4
|
DAYS_IN_YEAR = 365
|
|
5
5
|
|
|
6
|
-
def calculate(date_1, date_2)
|
|
6
|
+
def calculate(date_1, date_2, options={})
|
|
7
7
|
date_1 = cleanse_date(date_1)
|
|
8
8
|
date_2 = cleanse_date(date_2)
|
|
9
|
-
|
|
9
|
+
threshold = options[:threshold] || DAYS_IN_YEAR
|
|
10
10
|
days_apart = (date_1.mjd - date_2.mjd).abs
|
|
11
|
+
|
|
12
|
+
return 1 if days_apart >= threshold
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
distance = days_apart.to_f / DAYS_IN_YEAR
|
|
14
|
+
distance = days_apart.to_f / threshold
|
|
15
15
|
distance.round(2)
|
|
16
16
|
end
|
|
17
17
|
|
data/lib/slide_rule/version.rb
CHANGED
data/slide_rule.gemspec
CHANGED
|
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
|
|
|
7
7
|
s.version = ::SlideRule::VERSION
|
|
8
8
|
s.authors = %w(mattnichols fergmastaflex)
|
|
9
9
|
s.email = ['dev@mx.com']
|
|
10
|
-
s.homepage = 'https://github.com/
|
|
10
|
+
s.homepage = 'https://github.com/mxenabled/slide_rule'
|
|
11
11
|
s.summary = 'Ruby object distance calculator'
|
|
12
12
|
s.description = 'Calculates the distance between 2 arbitrary objects using specified fields and algorithms.'
|
|
13
13
|
s.license = 'MIT'
|
|
@@ -14,13 +14,13 @@ describe ::SlideRule::DistanceCalculator do
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
class CustomCalc
|
|
17
|
-
def calculate(_first, _second)
|
|
17
|
+
def calculate(_first, _second, _options)
|
|
18
18
|
0.9
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
class NilCalc
|
|
23
|
-
def calculate(_first, _second)
|
|
23
|
+
def calculate(_first, _second, _options)
|
|
24
24
|
nil
|
|
25
25
|
end
|
|
26
26
|
end
|
|
@@ -22,4 +22,17 @@ describe ::SlideRule::DistanceCalculators::DayOfYear do
|
|
|
22
22
|
expect(described_class.new.calculate('2015-10-8', '2015-11-8')).to eq(0.08)
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
|
+
|
|
26
|
+
context 'when there is a threshold' do
|
|
27
|
+
it 'should return a 1 distance when there are too many days apart' do
|
|
28
|
+
expect(described_class.new.calculate('2016-02-03', '2016-03-09', :threshold => 30)).to eq(1)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'should return a more sane number' do
|
|
32
|
+
result_with_threshold = described_class.new.calculate('2016-02-03', '2016-02-10', :threshold => 30)
|
|
33
|
+
result_without_threshold = described_class.new.calculate('2016-02-03', '2016-02-10')
|
|
34
|
+
|
|
35
|
+
expect(result_with_threshold).to be > result_without_threshold
|
|
36
|
+
end
|
|
37
|
+
end
|
|
25
38
|
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.
|
|
4
|
+
version: 1.0.0
|
|
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: 2016-
|
|
12
|
+
date: 2016-02-03 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -109,7 +109,7 @@ files:
|
|
|
109
109
|
- spec/slide_rule/distance_calculators/day_of_year_spec.rb
|
|
110
110
|
- spec/slide_rule/distance_calculators/levenshtein_spec.rb
|
|
111
111
|
- spec/spec_helper.rb
|
|
112
|
-
homepage: https://github.com/
|
|
112
|
+
homepage: https://github.com/mxenabled/slide_rule
|
|
113
113
|
licenses:
|
|
114
114
|
- MIT
|
|
115
115
|
metadata: {}
|