Sha256: 9d1fdd8fff0354e774763995f5398419977ad66aea809c1c89efc29b84dbbf4c

Contents?: true

Size: 1.13 KB

Versions: 57

Compression:

Stored size: 1.13 KB

Contents

module Dap
module Input

  require 'csv'

  #
  # CSV
  #
  class InputCSV

    include FileSource

    attr_accessor :has_header, :headers

    def initialize(args)
      self.headers = []

      fname = args.shift
      self.open(fname)

      args.each do |arg|
        if arg =~ /^header=(.*)/
          val =$1
          self.has_header = !! (val =~ /^y|t|1/i)
        end
      end

      if self.has_header
        data = read_record
        unless (data == :eof or data == :empty)
          self.headers = data.values.map{|x| x.to_s.strip }
        end
      end
    end

    def read_record
      res = {}
      line = self.fd.readline rescue nil
      return Error::EOF unless line
      line.force_encoding('BINARY')

      # Short-circuit the slow CSV parser if the data does not contain double quotes
      arr = line.index('"') ?
        ( CSV.parse(line) rescue nil ) :
        [ line.split(',').map{|x| x.strip } ]

      return Error::Empty unless arr
      cnt = 0
      arr.first.each do |x|
        cnt += 1
        if x.to_s.length > 0
          res[headers[cnt-1] || cnt.to_s] = x
        end
      end
      res
    end

  end

end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
dap-1.3.1 lib/dap/input/csv.rb
dap-1.3.0 lib/dap/input/csv.rb
dap-1.2.9 lib/dap/input/csv.rb
dap-1.2.8 lib/dap/input/csv.rb
dap-1.2.7 lib/dap/input/csv.rb
dap-1.2.6 lib/dap/input/csv.rb
dap-1.2.5 lib/dap/input/csv.rb
dap-1.2.4 lib/dap/input/csv.rb
dap-1.2.3 lib/dap/input/csv.rb
dap-1.2.2 lib/dap/input/csv.rb
dap-1.2.1 lib/dap/input/csv.rb
dap-1.2.0 lib/dap/input/csv.rb
dap-1.0.2 lib/dap/input/csv.rb
dap-1.0.1 lib/dap/input/csv.rb
dap-1.0.0 lib/dap/input/csv.rb
dap-0.1.24 lib/dap/input/csv.rb
dap-0.1.23 lib/dap/input/csv.rb
dap-0.1.22 lib/dap/input/csv.rb
dap-0.1.21 lib/dap/input/csv.rb
dap-0.1.20 lib/dap/input/csv.rb