class CsvGenerator::RowGenerator
Constants
- KNOWN_OPTIONS
Attributes
escaped_quote[R]
Public Class Methods
new(options = {})
click to toggle source
# File lib/csv_generator.rb, line 51 def initialize(options = {}) default_config.merge(filter_options options).each do |k, v| instance_variable_set :"@#{k}", v end @escaped_quote = quote_character * 2 end
Public Instance Methods
generate(row_values)
click to toggle source
# File lib/csv_generator.rb, line 79 def generate(row_values) row_values.map { |value| stringify value }.join(field_separator) + line_separator end
Private Instance Methods
default_config()
click to toggle source
# File lib/csv_generator.rb, line 65 def default_config { line_separator: "\r\n", field_separator: ',', quote_character: '"', } end
escape_quote_character(c)
click to toggle source
# File lib/csv_generator.rb, line 73 def escape_quote_character(c) c * 2 end
filter_options(hash)
click to toggle source
# File lib/csv_generator.rb, line 61 def filter_options(hash) hash.select { |k, _| KNOWN_OPTIONS.include? k } end
quote(str)
click to toggle source
# File lib/csv_generator.rb, line 98 def quote(str) escaped_string = str.gsub quote_character, escaped_quote quote_character + escaped_string + quote_character end
stringify(value)
click to toggle source
# File lib/csv_generator.rb, line 85 def stringify(value) case value when nil '' when String quote value when Fixnum, Float value.to_s else stringify value.to_s end end