Sha256: a61a864e1c3e1ea7c0fbb20debc27f2c2404c14fdf8279537cb1c6beb275179a
Contents?: true
Size: 1.61 KB
Versions: 3
Compression:
Stored size: 1.61 KB
Contents
require 'csv' module CsvShaper # Encoder # Takes a Header and Array of Rows and converts them to a valid CSV formatted String # Example: # ``` # CsvShaper::Encoder.new(@header, @rows).to_csv # ``` class Encoder def initialize(header, rows = []) if header.nil? raise MissingHeadersError, 'you must define some headers using csv.headers ...' end @header = header @rows = rows end # Public: converts the Shaper mapped headers and rows into # a CSV String # # Returns a String def to_csv(local_config = nil) csv_options = options.merge(local_options(local_config)) rows = padded_rows.map do |data| CSV::Row.new(@header.mapped_columns, data, false) end if csv_options[:write_headers] && rows.empty? rows << CSV::Row.new(@header.mapped_columns, [], true) end table = CSV::Table.new(rows) csv_options.except!(*custom_options.keys) table.to_csv(csv_options) end private def options CsvShaper::Shaper.config && CsvShaper::Shaper.config.options || {} end def local_options(local_config) local_config && local_config.options || {} end def custom_options CsvShaper::Config::CUSTOM_DEFAULT_OPTIONS end # Internal: make use of `CSV#values_at` to pad out the # cells into the correct columns for the headers # # Returns an Array of Arrays def padded_rows rows = @rows.map do |row| CSV::Row.new(row.cells.keys, row.cells.values) end table = CSV::Table.new(rows) table.values_at(*@header.columns) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
csv_shaper-1.3.1 | lib/csv_shaper/encoder.rb |
csv_shaper-1.3.0 | lib/csv_shaper/encoder.rb |
csv_shaper-1.2.0 | lib/csv_shaper/encoder.rb |