Class: Linkage::Comparator Abstract
- Inherits:
-
Object
- Object
- Linkage::Comparator
- Defined in:
- lib/linkage/comparator.rb
Overview
Abstract class to represent record comparators.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#lhs_args ⇒ Object
readonly
Returns the value of attribute lhs_args.
-
#rhs_args ⇒ Object
readonly
Returns the value of attribute rhs_args.
Class Method Summary collapse
- .[](name) ⇒ Object
- .comparator_name ⇒ String abstract
- .parameters ⇒ Array abstract
-
.register(klass) ⇒ Object
Register a new comparator.
- .score_range ⇒ Range abstract
Instance Method Summary collapse
-
#initialize(*args) ⇒ Comparator
constructor
Create a new Comparator object.
- #score(record_1, record_2) ⇒ Numeric abstract
Constructor Details
#initialize(*args) ⇒ Comparator
Create a new Comparator object.
77 78 79 80 81 82 83 |
# File 'lib/linkage/comparator.rb', line 77 def initialize(*args) @args = args @lhs_args = [] @rhs_args = [] @options = args.last.is_a?(Hash) ? args.pop : {} process_args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
73 74 75 |
# File 'lib/linkage/comparator.rb', line 73 def args @args end |
#lhs_args ⇒ Object (readonly)
Returns the value of attribute lhs_args.
73 74 75 |
# File 'lib/linkage/comparator.rb', line 73 def lhs_args @lhs_args end |
#rhs_args ⇒ Object (readonly)
Returns the value of attribute rhs_args.
73 74 75 |
# File 'lib/linkage/comparator.rb', line 73 def rhs_args @rhs_args end |
Class Method Details
.[](name) ⇒ Object
41 42 43 |
# File 'lib/linkage/comparator.rb', line 41 def self.[](name) @comparators ? @comparators[name] : nil end |
.comparator_name ⇒ String
Override this to return the name of the comparator.
47 48 49 |
# File 'lib/linkage/comparator.rb', line 47 def self.comparator_name raise NotImplementedError end |
.parameters ⇒ Array
Override this to require a specific number of arguments of a certain class. To require two parameters of either String or Integer, do something like this:
@@parameters = [[String, Integer], [String, Integer]]
def self.parameters
@@parameters
end
At least one argument must be defined.
62 63 64 |
# File 'lib/linkage/comparator.rb', line 62 def self.parameters raise NotImplementedError end |
.register(klass) ⇒ Object
Register a new comparator.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/linkage/comparator.rb', line 7 def self.register(klass) name = nil begin name = klass.comparator_name rescue NotImplementedError raise ArgumentError, "comparator_name class method must be defined" end if !klass.instance_methods(false).include?(:score) raise ArgumentError, "class must define the score method" end begin if klass.parameters.length > 0 @comparators ||= {} @comparators[name] = klass else raise ArgumentError, "class must have at least one parameter" end rescue NotImplementedError raise ArgumentError, "parameters class method must be defined" end begin range = klass.score_range if !range.is_a?(Range) || !range.first.is_a?(Numeric) || !range.last.is_a?(Numeric) raise ArgumentError, "score_range must be a Range of two numbers" end rescue NotImplementedError raise ArgumentError, "score_range class method must be defined" end end |
.score_range ⇒ Range
Override this to return a Range of the possible scores for the comparator.
69 70 71 |
# File 'lib/linkage/comparator.rb', line 69 def self.score_range raise NotImplementedError end |
Instance Method Details
#score(record_1, record_2) ⇒ Numeric
Override this to return the score of the linkage strength of two records.
88 89 90 |
# File 'lib/linkage/comparator.rb', line 88 def score(record_1, record_2) raise NotImplementedError end |