Class: HoneyFormat::Header
- Inherits:
-
Object
- Object
- HoneyFormat::Header
- Includes:
- Enumerable
- Defined in:
- lib/honey_format/matrix/header.rb
Overview
Represents a header
Instance Method Summary collapse
-
#columns ⇒ Array<Symbol>
Returns columns as array.
-
#each {|row| ... } ⇒ Enumerator
If no block is given, an enumerator object will be returned.
-
#empty? ⇒ true, false
Returns true if columns contains no elements.
-
#initialize(header, converter: HoneyFormat.header_converter, deduplicator: HoneyFormat.config.header_deduplicator) ⇒ Header
constructor
Instantiate a Header.
-
#length ⇒ Integer
(also: #size)
Return the number of header columns.
-
#original ⇒ Array<String>
Returns the original header.
-
#to_a ⇒ Array<Symbol>
Returns columns as array.
-
#to_csv(columns: nil) ⇒ String
Header as CSV-string.
Constructor Details
#initialize(header, converter: HoneyFormat.header_converter, deduplicator: HoneyFormat.config.header_deduplicator) ⇒ Header
Instantiate a Header
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/honey_format/matrix/header.rb', line 28 def initialize( header, converter: HoneyFormat.header_converter, deduplicator: HoneyFormat.config.header_deduplicator ) if header.nil? || header.empty? raise(Errors::MissingHeaderError, "CSV header can't be empty.") end @original_header = header self.deduplicator = deduplicator self.converter = converter @columns = build_columns(@original_header) end |
Instance Method Details
#columns ⇒ Array<Symbol>
Returns columns as array.
65 66 67 |
# File 'lib/honey_format/matrix/header.rb', line 65 def columns @columns end |
#each {|row| ... } ⇒ Enumerator
Returns If no block is given, an enumerator object will be returned.
59 60 61 |
# File 'lib/honey_format/matrix/header.rb', line 59 def each(&block) columns.each(&block) end |
#empty? ⇒ true, false
Returns true if columns contains no elements.
51 52 53 |
# File 'lib/honey_format/matrix/header.rb', line 51 def empty? @columns.empty? end |
#length ⇒ Integer Also known as: size
Return the number of header columns
77 78 79 |
# File 'lib/honey_format/matrix/header.rb', line 77 def length columns.length end |
#original ⇒ Array<String>
Returns the original header
45 46 47 |
# File 'lib/honey_format/matrix/header.rb', line 45 def original @original_header end |
#to_a ⇒ Array<Symbol>
Returns columns as array.
71 72 73 |
# File 'lib/honey_format/matrix/header.rb', line 71 def to_a columns end |
#to_csv(columns: nil) ⇒ String
Header as CSV-string
84 85 86 87 88 89 90 91 92 |
# File 'lib/honey_format/matrix/header.rb', line 84 def to_csv(columns: nil) attributes = if columns self.columns & columns.map(&:to_sym) else self.columns end ::CSV.generate_line(attributes) end |