Class: CsvFactory::Csv
- Inherits:
-
Object
- Object
- CsvFactory::Csv
- Defined in:
- lib/csv_factory/csv.rb
Instance Method Summary collapse
-
#add_rows_to_section(section_name, rows) ⇒ Object
Parameter: section_name Example: ‘Trailer’.
- #generate_content ⇒ Object
-
#initialize(delimeter: ',', sections: []) ⇒ Csv
constructor
Instance variable: @sections Example: [ { section_name: ‘Header’, columns: [ { column_name: ‘Record Type’ }, { column_name: ‘Environment Type’ }, ] }, { section_name: ‘Detail’, columns: [ { column_name: ‘Record Type’ }, { column_name: ‘Business Unit’ }, { column_name: ‘Claim Number’ }, ] }, { section_name: ‘Trailer’, columns: [ { column_name: ‘Record Type’ }, { column_name: ‘Total Batch File LineItem Count’ }, { column_name: ‘Total Batch File Amount Total’ }, ] } ].
Constructor Details
#initialize(delimeter: ',', sections: []) ⇒ Csv
Instance variable: @sections Example: [
{
section_name: 'Header',
columns: [
{ column_name: 'Record Type' },
{ column_name: 'Environment Type' },
]
},
{
section_name: 'Detail',
columns: [
{ column_name: 'Record Type' },
{ column_name: 'Business Unit' },
{ column_name: 'Claim Number' },
]
},
{
section_name: 'Trailer',
columns: [
{ column_name: 'Record Type' },
{ column_name: 'Total Batch File LineItem Count' },
{ column_name: 'Total Batch File Amount Total' },
]
}
]
Instance variable: @column_positions A filter and transformation of the @sections information. Example: {
'Header': {
'Record Type': 0,
'Environment Type': 1
},
'Detail': {
'Record Type': 0,
'Business Unit': 1,
'Claim Number': 2,
},
'Trailer': {
'Record Type': 0,
'Total Batch File LineItem Count': 1,
'Total Batch File Amount Total': 2
}
}
Instance variable: @data Example:
'Header': [['value', nil]],
'Detail': [['value', nil, 'value'], ['value', nil, 'value']]
'Trailer': [['value', nil, 'value']]
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/csv_factory/csv.rb', line 58 def initialize(delimeter: ',', sections: []) @delimeter = delimeter @sections = sections @column_positions = {} # Note: since ruby 1.9, the insertion order is maintained # https://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash @data = {} sections.each do |section| if @data.key?(section[:section_name]) raise CsvFactory::Exception.new("The section `#{section[:section_name]}` is defined twice") end @column_positions[section[:section_name]] = {} section[:columns].each_with_index do |column, idx| if @column_positions[section[:section_name]].key?(column[:column_name]) # NOTE! this is a limitation: this library doesn't support 2 columns of the same name # in the same section. Each column name within a section must be unique :( raise CsvFactory::Exception.new("The column `#{column[:column_name]}` in the section `#{section[:section_name]}` is defined twice") end @column_positions[section[:section_name]][column[:column_name]] = idx end @data[section[:section_name]] = [] end end |
Instance Method Details
#add_rows_to_section(section_name, rows) ⇒ Object
Parameter: section_name Example: ‘Trailer’
Parameter: rows Example 1: { ‘Record Type’: ‘T’, ‘Total Batch File LineItem Count’: ‘21’ } Example 2: [{ ‘Record Type’: ‘T’, ‘Total Batch File LineItem Count’: ‘21’ }]
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/csv_factory/csv.rb', line 95 def add_rows_to_section(section_name, rows) unless @data.key?(section_name) raise CsvFactory::Exception.new( "The section `#{section_name}` is not defined. " \ 'Maybe it is a symbol/string incosistency with the definition?' ) end unless rows.kind_of?(Array) rows = [rows] end rows.each do |row| new_row = Array.new(@column_positions[section_name].length) row.each do |column_name, value| new_row[@column_positions[section_name][column_name]] = value end @data[section_name] << new_row end end |
#generate_content ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/csv_factory/csv.rb', line 118 def generate_content ret_array = [] @data.each do |_section_name, rows| rows.each do |row| ret_array << row.join(@delimeter) end end ret_array.join("\n") end |