Sha256: 80617a8f069953ac2ea207d08dc6c148e77b2476877c5eaa0425d7a94e1510c4

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module ETL #:nodoc:
  module Parser #:nodoc:
    # Parses delimited files
    class DelimitedParser < ETL::Parser::Parser
      include Enumerable
      # Initialize the parser
      # * <tt>source</tt>: The Source object
      def initialize(source)
        super
        configure
      end
      
      # Returns each row
      def each
        options = {}
        Dir.glob(file).each do |file|
          FasterCSV.foreach(file, options) do |raw_row|
            row = {}
            raw_row.each_with_index do |record, index|
              f = fields[index]
              row[f.name] = convert(f.name, record, f.type)
            end
            yield row
          end
        end
      end
      
      # Get an array of defined fields
      def fields
        @fields ||= []
      end
      
      private
      def configure
        source.definition.each do |options|
          case options
          when Symbol
            fields << Field.new(options)
          when Hash
            fields << Field.new(options[:name], options[:type])
          else
            raise DefinitionError, "Each field definition must either be a symbol or a hash"
          end
        end
      end
      
      class Field
        attr_reader :name, :type
        def initialize(name, type=:string)
          @name = name
          @type = type
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activewarehouse-etl-0.1.0 lib/etl/parser/delimited_parser.rb
activewarehouse-etl-0.2.0 lib/etl/parser/delimited_parser.rb
activewarehouse-etl-0.3.0 lib/etl/parser/delimited_parser.rb