Module: IMW::Resources::Formats::Delimited Abstract

Included in:
Csv, Tsv
Defined in:
lib/imw/resources/formats/delimited.rb

Overview

This module is abstract.

Defines methods used for parsing and writing delimited data formats (CSV, TSV, &c.) with the FasterCSV library. This module is not used to directly extend a resource. Instead, more specific modules (e.g. - IMW::Resources::Formats::Csv) include this one and also define delimited_options which is actually what’s passed to FasterCSV.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#delimited_settingsObject

Returns the value of attribute delimited_settings.



15
16
17
# File 'lib/imw/resources/formats/delimited.rb', line 15

def delimited_settings
  @delimited_settings
end

Instance Method Details

#dump(data, options = {}) ⇒ Object

Dump an array of arrays into this resource.

Parameters:

  • data (Array)

    array of arrays to dump

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :persist (true, false)

    Keep this resource’s IO object open after dumping



41
42
43
44
45
46
47
48
# File 'lib/imw/resources/formats/delimited.rb', line 41

def dump data, options={}
  require 'fastercsv'
  data.each do |row|
    write(FasterCSV.generate_line(row, delimited_options))
  end
  io.close unless options[:persist]
  self
end

#load {|Array| ... } ⇒ Array

Return the data in this delimited resource as an array of arrays.

Yield each outer array (row) if passed a block.

Yields:

  • (Array)

    each row of the data

Returns:

  • (Array)

    the full data matrix



24
25
26
27
# File 'lib/imw/resources/formats/delimited.rb', line 24

def load &block
  require 'fastercsv'
  FasterCSV.parse(read, delimited_options, &block)
end

#map {|Array| ... } ⇒ Object

Map each row in this delimited resource.

Yields:

  • (Array)

    each row of the data



32
33
34
# File 'lib/imw/resources/formats/delimited.rb', line 32

def map &block
  load.map(&block)
end