Class: HoneyFormat::Header

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

Overview

Represents a header

Instance Method Summary collapse

Constructor Details

#initialize(header, converter: HoneyFormat.header_converter, deduplicator: HoneyFormat.config.header_deduplicator) ⇒ Header

Instantiate a Header

Examples:

Instantiate a header with a custom converter

converter = ->(col) { col == 'username' ? 'handle' : col }
header = HoneyFormat::Header.new(['name', 'username'], converter: converter)
header.to_a # => ['name', 'handle']

Parameters:

  • header (Array<String>)

    array of strings.

  • converter (#call, Symbol, Hash) (defaults to: HoneyFormat.header_converter)

    header converter that implements a #call method that takes one column (string) argument OR symbol for a registered converter registry OR a hash mapped to a symbol or something that responds to #call.

  • deduplicator (#call, Symbol) (defaults to: HoneyFormat.config.header_deduplicator)

    header deduplicator that implements a #call method that takes columns Array<String> argument OR symbol for a registered deduplicator registry.

Raises:



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

#columnsArray<Symbol>

Returns columns as array.

Returns:

  • (Array<Symbol>)

    of columns.



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.

Yields:

  • (row)

    The given block will be passed for every column.

Yield Parameters:

  • a (Row)

    column in the CSV header.

Returns:

  • (Enumerator)

    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.

Returns:

  • (true, false)

    true if columns contains no elements.



51
52
53
# File 'lib/honey_format/matrix/header.rb', line 51

def empty?
  @columns.empty?
end

#lengthInteger Also known as: size

Return the number of header columns

Returns:

  • (Integer)

    the number of header columns



77
78
79
# File 'lib/honey_format/matrix/header.rb', line 77

def length
  columns.length
end

#originalArray<String>

Returns the original header

Returns:

  • (Array<String>)

    the original header



45
46
47
# File 'lib/honey_format/matrix/header.rb', line 45

def original
  @original_header
end

#to_aArray<Symbol>

Returns columns as array.

Returns:

  • (Array<Symbol>)

    of columns.



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

Returns:

  • (String)

    CSV-string representation.



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