Sha256: e3ee82ab77b6c9781e53a63a6554b941275cf924ba1c02d9fc2acc648e4b898e
Contents?: true
Size: 1.86 KB
Versions: 7
Compression:
Stored size: 1.86 KB
Contents
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'fathom')) require 'import' require 'open-uri' # TODO: Work this out with Ruby 1.9. We don't need to load this for all Rubies. require 'fastercsv' require 'ext/faster_csv' module Fathom class CSVImport < Import # @content could be a filename, URI, or actual file contents. We figure out which # it is and then parse the contents with FasterCSV. We assume that there are column # headers that are the names of each node, and that the values in the node are # values for a DataNode. def import_csv parsed = parse_contents extracted = extract_columns(parsed) [DataNode, extracted] end # These are the options we use to parse the file. They can be overriden # by calling CSVImport.new(:parse_options => {...}, ...) def parse_options return @parse_options if @parse_options @parse_options = @options[:parse_options] @parse_options ||= {:converters => [:all], :headers => true, :skip_blanks => true, :header_converters => [:strip, :symbol]} @parse_options end protected def parse_contents arr_of_arrs = FasterCSV.parse(get_contents, parse_options) end # Tries to read a file or URL. That failing, assumes the contents are CSV contents. def get_contents begin content = open(@content).read rescue content = @content end return content end # Returns an array of hashes with :name => row header, :values => values def extract_columns(parsed) transposed = parsed.to_a.transpose transposed.inject([]) do |list, column| list << {:name => column.shift, :values => column} end end end end if __FILE__ == $0 Fathom::CSVImport.import(:content => ARGV.first) end
Version data entries
7 entries across 7 versions & 1 rubygems