class JsonCsv::CsvBuilder
Attributes
known_headers_to_indexes[R]
Public Class Methods
create_csv_without_headers(csv_outfile_path, csv_write_mode = 'wb') { |csv_builder| ... }
click to toggle source
Writes out a CSV file that does NOT contain a header row. Only data values. Returns an array of headers that correspond to the written-out CSV file's columns.
Why don't we include CSV headers in the CSV? Because don't know what set of headers we're working with while we dynamically create this CSV. Different JSON documents may or may not all contain the same headers. For this reason, this is more of an internal method that isn't called directly by users of this gem.
# File lib/json_csv/csv_builder.rb, line 31 def self.create_csv_without_headers(csv_outfile_path, csv_write_mode = 'wb') csv_builder = nil CSV.open(csv_outfile_path, csv_write_mode) do |csv| csv_builder = new(csv) yield csv_builder end csv_builder.known_headers_to_indexes.keys end
new(open_csv_handle)
click to toggle source
# File lib/json_csv/csv_builder.rb, line 9 def initialize(open_csv_handle) @known_headers_to_indexes = {} @open_csv_handle = open_csv_handle end
original_header_indexes_to_sorted_indexes(csv_headers, column_header_comparator)
click to toggle source
# File lib/json_csv/csv_builder.rb, line 42 def self.original_header_indexes_to_sorted_indexes(csv_headers, column_header_comparator) original_headers_to_indexes = Hash[csv_headers.map.with_index { |header, index| [header, index] }] headers_to_sorted_indexes = Hash[csv_headers.sort(&column_header_comparator).map.with_index { |header, index| [header, index] }] original_to_sorted_index_map = {} original_headers_to_indexes.each do |header, original_index| original_to_sorted_index_map[original_index] = headers_to_sorted_indexes[header] end original_to_sorted_index_map end
Public Instance Methods
add(json_hash)
click to toggle source
Adds data from the given json hash to the CSV we're building.
# File lib/json_csv/csv_builder.rb, line 15 def add(json_hash) row_to_write = [] JsonCsv.json_hash_to_flat_csv_row_hash(json_hash).each do |column_header, cell_value| known_headers_to_indexes[column_header] = known_headers_to_indexes.length unless known_headers_to_indexes.key?(column_header) row_to_write[known_headers_to_indexes[column_header]] = cell_value end @open_csv_handle << row_to_write end