Class: Linkage::ScoreSets::CSV
- Inherits:
-
Linkage::ScoreSet
- Object
- Linkage::ScoreSet
- Linkage::ScoreSets::CSV
- Includes:
- Helpers::CSV
- Defined in:
- lib/linkage/score_sets/csv.rb
Overview
ScoreSets::CSV is an implementation of Linkage::ScoreSet for saving scores in a CSV file.
There are three options available:
:filename
- which file to store scores 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 'scores.csv'
, and the other options are
blank. This means that it will write scores to the 'scores.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:
comparator_id,id_1,id_2,score
1,123,456,1
1,124,457,0.5
2,123,456,0
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :filename => 'scores.csv' }
Instance Method Summary collapse
- #add_score(comparator_id, id_1, id_2, score) ⇒ Object
- #close ⇒ Object
- #each_pair ⇒ Object
-
#initialize(options = {}) ⇒ CSV
constructor
A new instance of CSV.
- #open_for_reading ⇒ Object
- #open_for_writing ⇒ Object
Methods included from Helpers::CSV
#csv_filename, #open_csv_for_reading, #open_csv_for_writing
Methods inherited from Linkage::ScoreSet
Constructor Details
#initialize(options = {}) ⇒ CSV
Returns a new instance of CSV.
42 43 44 |
# File 'lib/linkage/score_sets/csv.rb', line 42 def initialize( = {}) @options = DEFAULT_OPTIONS.merge(.reject { |k, v| v.nil? }) end |
Instance Method Details
#add_score(comparator_id, id_1, id_2, score) ⇒ Object
63 64 65 66 |
# File 'lib/linkage/score_sets/csv.rb', line 63 def add_score(comparator_id, id_1, id_2, score) raise "not in write mode" if @mode != :write @csv << [comparator_id, id_1, id_2, score] end |
#close ⇒ Object
82 83 84 85 |
# File 'lib/linkage/score_sets/csv.rb', line 82 def close @mode = nil @csv.close if @csv end |
#each_pair ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/linkage/score_sets/csv.rb', line 68 def each_pair raise "not in read mode" if @mode != :read pairs = Hash.new { |h, k| h[k] = {} } @csv.each do |row| key = [row['id_1'], row['id_2']] score = row['score'] pairs[key][row['comparator_id'].to_i] = score.to_f end pairs.each_pair do |pair, scores| yield pair[0], pair[1], scores end end |
#open_for_reading ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/linkage/score_sets/csv.rb', line 46 def open_for_reading raise "already open for writing, try closing first" if @mode == :write return if @mode == :read @csv = open_csv_for_reading(@options) @mode = :read end |
#open_for_writing ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/linkage/score_sets/csv.rb', line 54 def open_for_writing raise "already open for reading, try closing first" if @mode == :read return if @mode == :write @csv = open_csv_for_writing(@options) @csv << %w{comparator_id id_1 id_2 score} @mode = :write end |