Sha256: 4c55a75f0e21315d31165191c71505a318800ae1438adffe7e467ef48e39af89

Contents?: true

Size: 751 Bytes

Versions: 3

Compression:

Stored size: 751 Bytes

Contents

require 'json'
module DocParser
  # The JSONOutput class generates a JSON file containing all rows as seperate
  # Array elements
  # @see Output
  class JSONOutput < Output
    # @!visibility private
    def open_file
      @file << '['
      @first = true
      @doc = {}
    end

    def write_row(row)
      raise MissingHeaderException if @header.nil? || @header.length == 0
      if @first
        @first = false
      else
        @file << ','
      end
      0.upto(@header.length - 1) do |counter|
        if row.length > counter
          @doc[@header[counter]] = row[counter]
        else
          @doc[@header[counter]] = ''
        end
      end
      @file << JSON.dump(@doc)
    end

    def footer
      @file << ']'
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
docparser-0.1.2 lib/docparser/output/json_output.rb
docparser-0.1.1 lib/docparser/output/json_output.rb
docparser-0.1.0 lib/docparser/output/json_output.rb