Sha256: 33412c014acbc49608d7c9424eff6a9dae55b669c0c31181d7dec2ca13e83470

Contents?: true

Size: 992 Bytes

Versions: 2

Compression:

Stored size: 992 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', @options){ |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?
      unless @options.has_key?(:write_headers) && !@options[:write_headers]
        csv << @instance.first.to_comma_headers(@style)
      end
      
      @instance.send(iterator_method) do |object|
        csv << object.to_comma(@style)
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
comma-0.4.3 lib/comma/generator.rb
comma-0.4.2 lib/comma/generator.rb