Sha256: 67c27cd6baab93f465825728f57f632e6ef72d05754618000162f31d2950fb90

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

module HoneyFormat
  # Header column converter
  module ConvertHeaderValue
    # Replace map
    REPLACE_MAP = [
      [/ \(/, '('],
      [/ \[/, '['],
      [/ \{/, '{'],
      [/\) /, ')'],
      [/\] /, ']'],
      [/\} /, '}'],
      [/ /, '_'],
      [/-/, '_']
    ].map { |array| array.freeze }.freeze

    # Returns converted value and mutates the argument.
    # @return [Symbol] the cleaned header column.
    # @param [String] column the string to be cleaned.
    # @param [Integer] column index.
    # @example Convert simple header
    #     ConvertHeaderValue.call("  User name ") #=> "user_name"
    # @example Convert complex header
    #     ConvertHeaderValue.call(" First name (user)") #=> :'first_name(user)'
    def self.call(column, index)
      return :"column#{index}" if column.nil? || column.empty?

      column = column.dup
      column.strip!
      column.downcase!
      REPLACE_MAP.each do |data|
        from, to = data
        column.gsub!(from, to)
      end
      column.to_sym
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
honey_format-0.12.0 lib/honey_format/convert_header_value.rb
honey_format-0.11.0 lib/honey_format/convert_header_value.rb
honey_format-0.10.0 lib/honey_format/convert_header_value.rb