Class: Linkage::Comparator Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/linkage/comparator.rb

Overview

This class is abstract.

Abstract class to represent record comparators.

Direct Known Subclasses

Linkage::Comparators::Binary

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Comparator

Create a new Comparator object.

Parameters:



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

#argsObject (readonly)

Returns the value of attribute args.



73
74
75
# File 'lib/linkage/comparator.rb', line 73

def args
  @args
end

#lhs_argsObject (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_argsObject (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_nameString

This method is abstract.

Override this to return the name of the comparator.

Returns:

  • (String)

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/linkage/comparator.rb', line 47

def self.comparator_name
  raise NotImplementedError
end

.parametersArray

This method is abstract.

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.

Returns:

  • (Array)

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/linkage/comparator.rb', line 62

def self.parameters
  raise NotImplementedError
end

.register(klass) ⇒ Object

Register a new comparator.

Parameters:

  • klass (Class)

    Comparator subclass



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_rangeRange

This method is abstract.

Override this to return a Range of the possible scores for the comparator.

Returns:

  • (Range)

Raises:

  • (NotImplementedError)


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

This method is abstract.

Override this to return the score of the linkage strength of two records.

Returns:

  • (Numeric)

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/linkage/comparator.rb', line 88

def score(record_1, record_2)
  raise NotImplementedError
end