Class: Pile::Header
Overview
Header that contains the names of each column, used to refer to values of records by name.
For example, given a CSV file containing the following header followed by multiple lines each containing a record,
“ID,Name,Address Line”
The parsed array from this row can be passed to initialize after the aliases hash, which can look like this, assuming case_sensitive is false:
=> [‘identity’, ‘#’], ‘address line’ => [‘address’]
Instance Attribute Summary collapse
- #aliases ⇒ Object
-
#case_sensitive ⇒ Boolean
Whether indices are case sensitive; defaults to
false. -
#csv ⇒ CSV
(nil) Optional CSV object associated with this header; used for utility functions such as
write_header. - #indices ⇒ Object
Class Method Summary collapse
-
.from_csv_row(row, aliases = {}) ⇒ Object
Construct a ‘Header’ from a CSV-formatted line.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#column_index(i) ⇒ Object
Return the integer position that
irefers to. -
#each ⇒ Object
Enumerate the record after converting to an array with
to_a. - #eql?(other) ⇒ Boolean
-
#initialize(aliases, *indices) ⇒ Header
constructor
A new instance of Header.
-
#to_a ⇒ Object
Enumerate each column header.
-
#write_header(csv = nil) ⇒ Object
Write this header to the header’s CSV object, if present.
Constructor Details
#initialize(aliases, *indices) ⇒ Header
Returns a new instance of Header.
71 72 73 74 |
# File 'lib/pile/header.rb', line 71 def initialize(aliases, *indices) @aliases = aliases @indices = indices end |
Instance Attribute Details
#aliases ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/pile/header.rb', line 41 def aliases if case_sensitive @aliases elsif @aliases_downcased @aliases_downcased else downcased = {} @aliases.each_pair {|k, v| downcased[k.downcase] = v} @aliases_downcased = downcased end end |
#case_sensitive ⇒ Boolean
Returns Whether indices are case sensitive; defaults to false.
57 58 59 |
# File 'lib/pile/header.rb', line 57 def case_sensitive @case_sensitive.nil? ? @case_sensitive = false : @case_sensitive end |
#csv ⇒ CSV
Returns (nil) Optional CSV object associated with this header; used for utility functions such as write_header.
64 65 66 |
# File 'lib/pile/header.rb', line 64 def csv @csv end |
#indices ⇒ Object
54 55 56 |
# File 'lib/pile/header.rb', line 54 def indices @indices end |
Class Method Details
.from_csv_row(row, aliases = {}) ⇒ Object
Construct a ‘Header’ from a CSV-formatted line.
23 24 25 |
# File 'lib/pile/header.rb', line 23 def self.from_csv_row row, aliases = {} self.new aliases, *row.parse_csv(converters: [:integer]) end |
Instance Method Details
#==(other) ⇒ Object
96 97 98 |
# File 'lib/pile/header.rb', line 96 def ==(other) self.aliases == other.aliases && self.indices == other.indices && self.csv == other.csv end |
#column_index(i) ⇒ Object
Return the integer position that i refers to. This takes into account the name of each column and the alias hash.
78 79 80 81 82 83 84 85 86 |
# File 'lib/pile/header.rb', line 78 def column_index i if case_sensitive position = indices.find_index {|column| column == i || (@aliases.has_key?(column) && @aliases[column].member?(i))} else position = indices.find_index {|column| column.downcase == i.to_s.downcase || (aliases.has_key?(column.downcase) && aliases[column.downcase].any? {|the_alias| the_alias.downcase == i.to_s.downcase})} end position ||= (i.kind_of?(Fixnum) ? i : nil) end |
#each ⇒ Object
Enumerate the record after converting to an array with to_a.
105 106 107 |
# File 'lib/pile/header.rb', line 105 def each to_a.each end |
#eql?(other) ⇒ Boolean
100 101 102 |
# File 'lib/pile/header.rb', line 100 def eql?(other) self.aliases.eql?(other.aliases) && self.indices.eql?(other.indices) && self.csv.eql?(other.csv) end |
#to_a ⇒ Object
Enumerate each column header.
110 111 112 |
# File 'lib/pile/header.rb', line 110 def to_a indices end |
#write_header(csv = nil) ⇒ Object
Write this header to the header’s CSV object, if present.
92 93 94 |
# File 'lib/pile/header.rb', line 92 def write_header csv = nil csv << indices end |