Sha256: 079f2666e4bfcac843e6650c99f45ea330aaf2dc7ed57dcdf9b0e4c56ebbde71
Contents?: true
Size: 1.18 KB
Versions: 1
Compression:
Stored size: 1.18 KB
Contents
require 'honey_format/row' module HoneyFormat # Represents rows. class Rows include Enumerable # Returns array of cleaned strings. # @return [Rows] new instance of Rows. # @param [Array] rows the array of rows. # @param [Array] columns the array of column symbols. def initialize(rows, columns, builder: nil) @rows = prepare_rows(Row.new(columns, builder: builder), rows) end # @yield [row] The given block will be passed every row. # @yieldparam [Row] a row in the CSV file. # @return [Enumerator] # If no block is given, an enumerator object will be returned. def each(&block) @rows.each(&block) end # Returns rows as array. # @return [Array] of rows. def to_a @rows end # @return [String] CSV-string representation. def to_csv to_a.map(&:to_csv).join end private def prepare_rows(builder, rows) built_rows = [] rows.each do |row| # ignore empty rows - the Ruby CSV library can return empty lines as [nil] next if row.empty? || row.nil? || row == [nil] built_rows << builder.build(Sanitize.array!(row)) end built_rows end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
honey_format-0.6.0 | lib/honey_format/rows.rb |