Class: Linkage::ResultSet

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

Overview

A ResultSet is a convenience class for wrapping a ScoreSet and a MatchSet. Most of the time, you'll want to use the same storage format for both scores and matches. ResultSet provides a way to group both sets together.

The default implementation of ResultSet merely returns whatever ScoreSet and MatchSet you pass to it during creation (see #initialize). However, ResultSet can be subclassed to provide easy initialization of sets of the same format. Currently there are two subclasses:

If you want to implement a custom ResultSet, create a class that inherits ResultSet and defines both #score_set and #match_set to return a ScoreSet and MatchSet respectively. You can then register that class via ResultSet.register to make it easier to use.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(score_set, match_set) ⇒ ResultSet



56
57
58
59
# File 'lib/linkage/result_set.rb', line 56

def initialize(score_set, match_set)
  @score_set = score_set
  @match_set = match_set
end

Class Method Details

.klass_for(name) ⇒ Class? Also known as: []

Return a registered ResultSet subclass or nil if it doesn't exist.



48
49
50
# File 'lib/linkage/result_set.rb', line 48

def klass_for(name)
  @result_set ? @result_set[name] : nil
end

.register(name, klass) ⇒ Object

Register a new result set. Subclasses must define #score_set and #match_set. Otherwise, an ArgumentError will be raised when you try to call register.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/linkage/result_set.rb', line 27

def register(name, klass)
  methods = klass.instance_methods
  missing = []
  unless methods.include?(:score_set)
    missing.push("#score_set")
  end
  unless methods.include?(:match_set)
    missing.push("#match_set")
  end
  unless missing.empty?
    raise ArgumentError, "class must define #{missing.join(" and ")}"
  end

  @result_set ||= {}
  @result_set[name] = klass
end

Instance Method Details

#match_setMatchSet

Returns a MatchSet.



71
72
73
# File 'lib/linkage/result_set.rb', line 71

def match_set
  @match_set
end

#score_setScoreSet

Returns a ScoreSet.



64
65
66
# File 'lib/linkage/result_set.rb', line 64

def score_set
  @score_set
end