Class: Linkage::Comparators::Within
- Inherits:
-
Linkage::Comparator
- Object
- Linkage::Comparator
- Linkage::Comparators::Within
- Defined in:
- lib/linkage/comparators/within.rb
Overview
Within is a integer comparator. It checks if two values are within a specified range. Score is either 0 to 1.
To use Within, you must specify one field for each record to use in the comparison, along with a range value.
Consider the following example, using a Linkage::Configuration as part of Dataset#link_with:
config.within(:foo, :bar, 5)
For each pair of records, if value of foo
is within 5 (inclusive) of
the value of bar
, the score is 1. Otherwise, the score is 0.
Instance Attribute Summary
Attributes inherited from Linkage::Comparator
Instance Method Summary collapse
-
#initialize(field_1, field_2, value) ⇒ Within
constructor
A new instance of Within.
- #score(record_1, record_2) ⇒ Object
Methods inherited from Linkage::Comparator
klass_for, register, #score_and_notify, #score_dataset, #score_datasets, #type, #weigh
Constructor Details
#initialize(field_1, field_2, value) ⇒ Within
Returns a new instance of Within.
19 20 21 22 23 24 25 26 27 |
# File 'lib/linkage/comparators/within.rb', line 19 def initialize(field_1, field_2, value) if field_1.ruby_type != field_2.ruby_type raise "fields must have the same type" end @name_1 = field_1.name @name_2 = field_2.name @value = value end |
Instance Method Details
#score(record_1, record_2) ⇒ Object
29 30 31 |
# File 'lib/linkage/comparators/within.rb', line 29 def score(record_1, record_2) (record_1[@name_1] - record_2[@name_2]).abs <= @value ? 1 : 0 end |