Module: Linkage::Helpers::CSV

Included in:
MatchSets::CSV, ScoreSets::CSV
Defined in:
lib/linkage/helpers/csv.rb

Instance Method Summary collapse

Instance Method Details

#csv_filename(options) ⇒ Object



4
5
6
# File 'lib/linkage/helpers/csv.rb', line 4

def csv_filename(options)
  File.expand_path(options[:filename], options[:dir] || '.')
end

#open_csv_for_reading(options) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/linkage/helpers/csv.rb', line 8

def open_csv_for_reading(options)
  filename = csv_filename(options)
  if !File.exist?(filename)
    raise MissingError, "#{filename} does not exist"
  end
  ::CSV.open(filename, 'rb', :headers => true)
end

#open_csv_for_writing(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/linkage/helpers/csv.rb', line 16

def open_csv_for_writing(options)
  filename = csv_filename(options)
  if !options[:overwrite] && File.exist?(filename)
    raise ExistsError, "#{filename} exists and not in overwrite mode"
  end
  if options[:dir]
    FileUtils.mkdir_p(File.dirname(filename))
  end
  ::CSV.open(filename, 'wb')
end