Sha256: de6c4571239102a135899a3ee5968267e375aea9a04068c32c5deab6c2c3b5d7

Contents?: true

Size: 1012 Bytes

Versions: 15

Compression:

Stored size: 1012 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) || Comma::DEFAULT_OPTIONS[:style]
        @filename                 = @options.delete(:filename)
      end
    end

    def run(iterator_method)
      if @filename
        CSV_HANDLER.open(@filename, 'w', @options){ |csv| append_csv(csv, iterator_method) } and return true
      else
        CSV_HANDLER.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

15 entries across 15 versions & 1 rubygems

Version Path
comma-4.1.0 lib/comma/generator.rb
comma-4.0.1 lib/comma/generator.rb
comma-4.0.0 lib/comma/generator.rb
comma-3.2.4 lib/comma/generator.rb
comma-3.2.3 lib/comma/generator.rb
comma-3.2.2 lib/comma/generator.rb
comma-3.2.1 lib/comma/generator.rb
comma-3.2.0 lib/comma/generator.rb
comma-3.1.1 lib/comma/generator.rb
comma-3.1.0 lib/comma/generator.rb
comma-3.0.6 lib/comma/generator.rb
comma-3.0.5 lib/comma/generator.rb
comma-3.0.4 lib/comma/generator.rb
comma-3.0.3 lib/comma/generator.rb
comma-3.0.2 lib/comma/generator.rb