Class: Linkage::MatchSets::CSV

Inherits:
Linkage::MatchSet show all
Includes:
Helpers::CSV
Defined in:
lib/linkage/match_sets/csv.rb

Overview

MatchSets::CSV is an implementation of Linkage::MatchSet for saving matches in a CSV file.

There are three options available:

  • :filename - which file to store matches in; can be an absolute path or relative path
  • :dir - which directory to put the file in; used if :filename is a relative path
  • :overwrite - indicate whether or not to overwrite an existing file

By default, :filename is 'matches.csv', and the other options are blank. This means that it will write matches to the 'matches.csv' file in the current working directory and will raise an error if the file already exists.

If you specify :dir, that path will be created if it doesn't exist yet.

The resulting file looks like this:

id_1,id_2,score
123,456,0.75
124,457,1

See Also:

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :filename => 'matches.csv'
}

Instance Method Summary collapse

Methods included from Helpers::CSV

#csv_filename, #open_csv_for_reading, #open_csv_for_writing

Methods inherited from Linkage::MatchSet

klass_for, register

Constructor Details

#initialize(options = {}) ⇒ CSV

Returns a new instance of CSV.



37
38
39
# File 'lib/linkage/match_sets/csv.rb', line 37

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options.reject { |k, v| v.nil? })
end

Instance Method Details

#add_match(id_1, id_2, score) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/linkage/match_sets/csv.rb', line 49

def add_match(id_1, id_2, score)
  raise "not in write mode" if @mode != :write

  if score.floor.equal?(score.ceil)
    score = score.floor
  end
  @csv << [id_1, id_2, score]
end

#closeObject



58
59
60
61
# File 'lib/linkage/match_sets/csv.rb', line 58

def close
  @mode = nil
  @csv.close if @csv
end

#open_for_writingObject



41
42
43
44
45
46
47
# File 'lib/linkage/match_sets/csv.rb', line 41

def open_for_writing
  return if @mode == :write

  @csv = open_csv_for_writing(@options)
  @csv << %w{id_1 id_2 score}
  @mode = :write
end