slide_rule 0.2.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad1a7e41a1a14345029655bb3ede3e7c274fc885
4
- data.tar.gz: f164640543e98e706e5067abca94133350abe771
3
+ metadata.gz: 66f0ba46303354b7b468dcc13103acffe6e53b65
4
+ data.tar.gz: 92334c787512fe9b2b6b3b7406d7709d6de27c16
5
5
  SHA512:
6
- metadata.gz: 29f83acc7009c6473696870459120a1348074d960d7e053191153dd539c2d428545111a881f06838c84193765504b7855e79f8b703c45fb894fa1d65e4de260b
7
- data.tar.gz: ae9223f7203d59dd3f8e511c06579c93c8c520b5c7f184bddb7ae4eb991e1f40d13c3e6c88d59b8ca45ae7f1eb7fe344064d0c1262c48e2553d2c7202ccbf82b
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, rule|
73
+ distances = @rules.map do |attribute, options|
73
74
  val1 = i1.send(attribute)
74
75
  val2 = i2.send(attribute)
75
- distance = rule[:calculator].calculate(val1, val2)
76
- next { distance: distance.to_f, weight: rule[:weight] } unless distance.nil?
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
- return 1 if days_apart >= DAYS_IN_YEAR
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
 
@@ -1,7 +1,7 @@
1
1
  module SlideRule
2
2
  module DistanceCalculators
3
3
  class Levenshtein
4
- def calculate(first, second)
4
+ def calculate(first, second, options={})
5
5
  ::Vladlev.get_normalized_distance(first, second).to_f
6
6
 
7
7
  # Lower bound is difference in length
@@ -1,3 +1,3 @@
1
1
  module SlideRule
2
- VERSION = '0.2.2'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
@@ -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/mattnichols/slide_rule'
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.2.2
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-01-14 00:00:00.000000000 Z
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/mattnichols/slide_rule
112
+ homepage: https://github.com/mxenabled/slide_rule
113
113
  licenses:
114
114
  - MIT
115
115
  metadata: {}