buren/honey_format

View on GitHub
examples/convert_values.rb

Summary

Maintainability
A
20 mins
Test Coverage
# frozen_string_literal: true

require 'bundler/setup'
require 'honey_format'

csv_string = <<~CSV
  name,age,country
  John Doe,42,SE
CSV

HoneyFormat.configure do |config|
  config.converter_registry.register(:upcased, proc { |v| v.upcase })
  config.converter_registry.register(:country, proc { |v| v == 'SE' ? 'SWEDEN' : v })
end

type_map = {
  name: :upcased,
  country: :country,
}

puts '== EXAMPLE: Upcase name & convert country =='
puts
puts '== CSV START =='
csv = HoneyFormat::CSV.new(csv_string, type_map: type_map)
puts csv.to_csv
puts '== CSV END =='