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.
14 15 16 17 |
# File 'lib/json_csv/csv_builder.rb', line 14 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
12 13 14 |
# File 'lib/json_csv/csv_builder.rb', line 12 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.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/json_csv/csv_builder.rb', line 39 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
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/json_csv/csv_builder.rb', line 50 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 do |header, index| [header, index] end ] 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.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/json_csv/csv_builder.rb', line 20 def add(json_hash) row_to_write = [] JsonCsv.json_hash_to_flat_csv_row_hash(json_hash).each do |column_header, cell_value| unless known_headers_to_indexes.key?(column_header) known_headers_to_indexes[column_header] = known_headers_to_indexes.length end row_to_write[known_headers_to_indexes[column_header]] = cell_value end @open_csv_handle << row_to_write end |