Class: Pile::Header

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pile/header.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aliases, *indices) ⇒ Header

Returns a new instance of Header.

Parameters:

  • aliases (Hash<String, Array<String>>)

    A hash of aliases from the column name to an array of names that contains aliases, each of which can be used to identify the same column.

  • indices (Array<String>)

    The name of each value.



71
72
73
74
# File 'lib/pile/header.rb', line 71

def initialize(aliases, *indices)
  @aliases = aliases
  @indices = indices
end

Instance Attribute Details

#aliasesObject



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_sensitiveBoolean

Returns Whether indices are case sensitive; defaults to false.

Returns:

  • (Boolean)

    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

#csvCSV

Returns (nil) Optional CSV object associated with this header; used for utility functions such as write_header.

Returns:

  • (CSV)

    (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

#indicesObject

Parameters:

  • indices (Array<String>)

    The name of each value. Conventionally the first row in a CSV file.



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

#eachObject

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

Returns:

  • (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_aObject

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.

Parameters:

  • csv (CSV) (defaults to: nil)

    (nil) If present, the header will be written to the passed CSV object rather than the header’s.



92
93
94
# File 'lib/pile/header.rb', line 92

def write_header csv = nil
  csv << indices
end