Class: JsonCsv::CsvBuilder
- Inherits:
-
Object
- Object
- JsonCsv::CsvBuilder
- Defined in:
- lib/json_csv/csv_builder.rb
Instance Attribute Summary collapse
-
#known_headers_to_indexes ⇒ Object
readonly
map of all headers seen by this CsvBuilder, mapped to their column order indexes.
Class Method Summary collapse
-
.create_csv_without_headers(csv_outfile_path, csv_write_mode = 'wb') ⇒ Object
Writes out a CSV file that does NOT contain a header row.
- .original_header_indexes_to_sorted_indexes(csv_headers, column_header_comparator) ⇒ Object
Instance Method Summary collapse
-
#add(json_hash) ⇒ Object
Adds data from the given json hash to the CSV we’re building.
-
#initialize(open_csv_handle) ⇒ CsvBuilder
constructor
A new instance of CsvBuilder.
Constructor Details
#initialize(open_csv_handle) ⇒ CsvBuilder
Returns a new instance of CsvBuilder.
9 10 11 12 |
# 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 |
Instance Attribute Details
#known_headers_to_indexes ⇒ Object (readonly)
map of all headers seen by this CsvBuilder, mapped to their column order indexes
7 8 9 |
# File 'lib/json_csv/csv_builder.rb', line 7 def known_headers_to_indexes @known_headers_to_indexes end |
Class Method Details
.create_csv_without_headers(csv_outfile_path, csv_write_mode = 'wb') ⇒ Object
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.
31 32 33 34 35 36 37 38 39 40 |
# 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 |
.original_header_indexes_to_sorted_indexes(csv_headers, column_header_comparator) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# 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 |
Instance Method Details
#add(json_hash) ⇒ Object
Adds data from the given json hash to the CSV we’re building.
15 16 17 18 19 20 21 22 |
# 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 |