Class: Uncsv::Header
- Inherits:
-
Object
- Object
- Uncsv::Header
- Defined in:
- lib/uncsv/header.rb
Overview
A parsed CSV header.
Class Method Summary collapse
-
.parse!(csv, config) ⇒ OpenStruct
Parse headers from a CSV.
Instance Method Summary collapse
-
#each {|row| ... } ⇒ Enumerator
Iterate over each header field.
-
#initialize(headers, config = nil) ⇒ Header
constructor
Create a new
Header
object. -
#to_a ⇒ Array
Get an array of parsed header fields.
Constructor Details
Class Method Details
.parse!(csv, config) ⇒ OpenStruct
Parse headers from a CSV
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/uncsv/header.rb', line 53 def parse!(csv, config) index = config.header_rows.empty? ? 0 : (config.header_rows.max + 1) rows = read_rows(csv, index) headers = config.header_rows.map { |i| rows[i] } OpenStruct.new( header: new(headers, config), index: index, rows: rows ) end |
Instance Method Details
#each {|row| ... } ⇒ Enumerator
Iterate over each header field
21 22 23 |
# File 'lib/uncsv/header.rb', line 21 def each(&block) to_a.each(&block) end |
#to_a ⇒ Array
Get an array of parsed header fields
The header fields are cached, so consecutive calls to this method return the same array.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/uncsv/header.rb', line 31 def to_a @to_a ||= begin headers = nil_empty(@headers) headers = square(headers) headers = normalize(headers) if @config.normalize_headers headers = (headers) combined = combine(headers) combined = unique(combined) if @config.unique_headers combined end end |