Sha256: 95a12f7769765550cd308125370c444e7eb9e7b724faec4620bf62814c32a2c7

Contents?: true

Size: 888 Bytes

Versions: 3

Compression:

Stored size: 888 Bytes

Contents

module Comma

  class Generator

    def initialize(instance, style)
      @instance = instance
      @style    = style
      @options  = {}

      if @style.is_a? Hash
        @options  = @style.clone
        @style    = @options.delete(:style) || :default
        @filename = @options.delete(:filename)
      end
    end

    def run(iterator_method)
      if @filename
        FasterCSV.open(@filename, 'w'){ |csv| append_csv(csv, iterator_method) } and return true
      else
        FasterCSV.generate(@options){ |csv| append_csv(csv, iterator_method) }
      end
    end

    private
    def append_csv(csv, iterator_method)
      return '' if @instance.empty?
      csv << @instance.first.to_comma_headers(@style) # REVISIT: request to optionally include headers
      @instance.send(iterator_method) do |object|
        csv << object.to_comma(@style)
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
comma-0.3.1 lib/comma/generator.rb
liangzan-comma-0.3.1 lib/comma/generator.rb
comma-0.3.0 lib/comma/generator.rb