Class: Uncsv::Row
Overview
A single data row from a CSV. Fields can be accessed by header or zero-based index.
Instance Attribute Summary collapse
- 
  
    
      #fields  ⇒ Array 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
The fields ordered from left to right.
 - 
  
    
      #header  ⇒ Array 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
The headers for each field.
 
Instance Method Summary collapse
- 
  
    
      #[](key)  ⇒ String? 
    
    
  
  
  
  
  
  
  
  
  
    
Get a field by index or header.
 - 
  
    
      #each {|row| ... } ⇒ Enumerator 
    
    
  
  
  
  
  
  
  
  
  
    
Iterate over each pair of headers and fields.
 - 
  
    
      #fetch(key, default = nil) {|key| ... } ⇒ String, Object 
    
    
  
  
  
  
  
  
  
  
  
    
Get a field by index or header and specify a default.
 - 
  
    
      #initialize(header, fields, config = nil)  ⇒ Row 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
Create a new
Rowobject. - 
  
    
      #to_h  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    
Gets a hash of headers to fields.
 
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.
      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
#fields ⇒ Array (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.
      22 23 24  | 
    
      # File 'lib/uncsv/row.rb', line 22 def fields @fields end  | 
  
#header ⇒ Array (readonly)
The headers for each field
If a header for a given field is not defined, it will be nil.
      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.
      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
      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.
      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_h ⇒ Hash
Gets a hash of headers to fields
nil headers will not be included in the hash.
      59 60 61  | 
    
      # File 'lib/uncsv/row.rb', line 59 def to_h Hash[@header.compact.map { |h| [h, self[h]] }] end  |