Class: HoneyFormat::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/honey_format/configuration.rb

Overview

Holds HoneyFormat configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Instantiate configuration



15
16
17
18
19
20
21
22
23
# File 'lib/honey_format/configuration.rb', line 15

def initialize
  @converter_registry = nil
  @header_converter = nil
  @header_deduplicator = nil
  @delimiter = ','
  @row_delimiter = :auto
  @quote_character = '"'
  @skip_lines = nil
end

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



12
13
14
# File 'lib/honey_format/configuration.rb', line 12

def delimiter
  @delimiter
end

#quote_characterObject

Returns the value of attribute quote_character.



12
13
14
# File 'lib/honey_format/configuration.rb', line 12

def quote_character
  @quote_character
end

#row_delimiterObject

Returns the value of attribute row_delimiter.



12
13
14
# File 'lib/honey_format/configuration.rb', line 12

def row_delimiter
  @row_delimiter
end

#skip_linesObject

Returns the value of attribute skip_lines.



12
13
14
# File 'lib/honey_format/configuration.rb', line 12

def skip_lines
  @skip_lines
end

Instance Method Details

#converter_registry#call

Returns the converter registry

Returns:

  • (#call)

    converter the configured converter registry



93
94
95
# File 'lib/honey_format/configuration.rb', line 93

def converter_registry
  @converter_registry ||= Registry.new(default_converters)
end

#default_convertersHash

Default converter registry

Returns:

  • (Hash)

    hash with default converters



99
100
101
# File 'lib/honey_format/configuration.rb', line 99

def default_converters
  @default_converters ||= Converters::DEFAULT
end

#default_header_deduplicatorsHash

Default header deduplicate strategies

Returns:

  • (Hash)

    the default header deduplicatation strategies



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/honey_format/configuration.rb', line 68

def default_header_deduplicators
  @default_header_deduplicators ||= {
    deduplicate: proc do |columns|
      Helpers.key_count_to_deduplicated_array(columns)
    end,
    raise: proc do |columns|
      duplicates = Helpers.duplicated_items(columns)
      if duplicates.any?
        message = "all columns must be unique, duplicates are: #{duplicates}"
        raise(Errors::DuplicateHeaderColumnError, message)
      end
      columns
    end,
    none: proc { |columns| columns },
  }.freeze
end

#header_converter#call

Returns the header converter

Returns:

  • (#call)

    header_converter the configured header converter



27
28
29
# File 'lib/honey_format/configuration.rb', line 27

def header_converter
  @header_converter ||= converter_registry[:header_column]
end

#header_converter=(converter) ⇒ #call

Set the header converter

Parameters:

  • converter (Symbol, #call)

    for registered converter registry or object that responds to #call

Returns:

  • (#call)

    the header converter



35
36
37
38
39
40
41
# File 'lib/honey_format/configuration.rb', line 35

def header_converter=(converter)
  @header_converter = if converter.is_a?(Symbol)
                        converter_registry[converter]
                      else
                        converter
                      end
end

#header_deduplicator#call

Return the deduplication header strategy

Returns:

  • (#call)

    the header deduplication strategy



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

def header_deduplicator
  @header_deduplicator ||= header_deduplicator_registry[:deduplicate]
end

#header_deduplicator=(strategy) ⇒ #call

Set the deduplication header strategy

Parameters:

  • symbol (Symbol, #call)

    with known strategy identifier or method that responds to #call(colums, key_count)

Returns:

  • (#call)

    the header deduplication strategy

Raises:



55
56
57
58
59
60
61
62
63
64
# File 'lib/honey_format/configuration.rb', line 55

def header_deduplicator=(strategy)
  if header_deduplicator_registry.type?(strategy)
    @header_deduplicator = header_deduplicator_registry[strategy]
  elsif strategy.respond_to?(:call)
    @header_deduplicator = strategy
  else
    message = "unknown deduplication strategy: '#{strategy}'"
    raise(Errors::UnknownDeduplicationStrategyError, message)
  end
end

#header_deduplicator_registry#call

Returns the column deduplication registry

Returns:

  • (#call)

    column deduplication registry



87
88
89
# File 'lib/honey_format/configuration.rb', line 87

def header_deduplicator_registry
  @header_deduplicator_registry ||= Registry.new(default_header_deduplicators)
end