Class: Uncsv::Row

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/uncsv/row.rb

Overview

A single data row from a CSV. Fields can be accessed by header or zero-based index.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, fields, config = nil) ⇒ Row

Create a new Row object

The header and fields arrays do not need to be the same length. If they are not, the missing values will be filled with nil.

Parameters:

  • header (Array)

    The field headers

  • fields (Array)

    The field values

  • config (Config) (defaults to: nil)

    Configuration options. Default options if nil.



32
33
34
35
36
37
# File 'lib/uncsv/row.rb', line 32

def initialize(header, fields, config = nil)
  @config = config || Config.new
  @header = square(header, fields.size)
  @fields = square(fields, header.size).map { |f| process(f) }
  @map = Hash[header.zip(@fields)]
end

Instance Attribute Details

#fieldsArray (readonly)

The fields ordered from left to right

An array of zero-indexed field values. If a field is empty it will be nil, or '' if nil_empty is false.

Returns:

  • (Array)

    An array of the field values



22
23
24
# File 'lib/uncsv/row.rb', line 22

def fields
  @fields
end

#headerArray (readonly)

The headers for each field

If a header for a given field is not defined, it will be nil.

Returns:

  • (Array)

    An array of the field headers



14
15
16
# File 'lib/uncsv/row.rb', line 14

def header
  @header
end

Instance Method Details

#[](key) ⇒ String?

Get a field by index or header

If key is an Integer, get a field by a zero-based index. If key is a header, access a field by it's header. If key is nil, or if a field does not exist, will return nil.

Parameters:

  • key (Integer, String)

    The index or header

Returns:

  • (String, nil)

    The field value if it exists



47
48
49
50
51
52
# File 'lib/uncsv/row.rb', line 47

def [](key)
  return if key.nil?

  value = key.is_a?(Integer) ? @fields[key] : @map[key]
  process(value)
end

#each {|row| ... } ⇒ Enumerator

Iterate over each pair of headers and fields

Yields:

  • A block to run for each pair

Yield Parameters:

  • row (Row)

    A row object

Returns:

  • (Enumerator)

    An enumerator over each pair



68
69
70
# File 'lib/uncsv/row.rb', line 68

def each(&block)
  @map.each_pair(&block)
end

#fetch(key, default = nil) {|key| ... } ⇒ String, Object

Get a field by index or header and specify a default

Tries to get the field specified by key (see #[]). If the field is nil, returns the default. If a block is given, the default is the block's return value, otherwise the default is the default argument.

Yields:

  • A block to run if the field is nil

Yield Parameters:

  • key (String)

    The key parameter

Returns:

  • (String, Object)

    The field value or default



81
82
83
84
85
86
# File 'lib/uncsv/row.rb', line 81

def fetch(key, default = nil)
  value = self[key]
  return value unless value.nil?

  block_given? ? yield(key) : default
end

#to_hHash

Gets a hash of headers to fields

nil headers will not be included in the hash.

Returns:

  • (Hash)

    A hash of headers to fields



59
60
61
# File 'lib/uncsv/row.rb', line 59

def to_h
  Hash[@header.compact.map { |h| [h, self[h]] }]
end