Sha256: 501f1caca749a028e4f2dd1e3e3ad783684f48bb4d696177c050560fc475f587

Contents?: true

Size: 1.78 KB

Versions: 26

Compression:

Stored size: 1.78 KB

Contents

module ETL #:nodoc:
  module Parser #:nodoc:
    # Parser for fixed with files
    class FixedWidthParser < ETL::Parser::Parser
      # Initialize the parser
      # * <tt>source</tt>: The source object
      # * <tt>options</tt>: Parser options Hash
      def initialize(source, options={})
        super
        configure
      end
      
      # Return each row
      def each
        Dir.glob(file).each do |file|
          open(file).each do |line|
            row = {}
            lines_skipped = 0
            fields.each do |name, f|
              if lines_skipped < source.skip_lines
                lines_skipped += 1
                next
              end
              # TODO make strip optional?
              row[name] = line[f.field_start, f.field_length].strip
            end
            yield row
          end
        end
      end
      
      # Return a map of defined fields
      def fields
        @fields ||= {}
      end
      
      private
      def configure
        source.definition.each do |field, options|
          fields[field] = FixedWidthField.new(
            options[:name], options[:start], options[:end], options[:length]
          )
        end
      end
    end
    
    class FixedWidthField #:nodoc:
      attr_reader :name, :field_start, :field_end, :field_length
      # Initialize the field.
      def initialize(name, field_start, field_end=nil, field_length=nil)
        @name = name
        @field_start = field_start - 1
        if field_end
          @field_end = field_end
          @field_length = @field_end - @field_start
        elsif field_length
          @field_length = field_length
          @field_end = @field_start + @field_length
        else
          raise DefinitionError, "Either field_end or field_length required"
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 9 rubygems

Version Path
aeden-activewarehouse-etl-0.9.1 etl/lib/etl/parser/fixed_width_parser.rb
colincasey-activewarehouse-etl-0.9.4 lib/etl/parser/fixed_width_parser.rb
colincasey-activewarehouse-etl-0.9.5 lib/etl/parser/fixed_width_parser.rb
factorylabs-activewarehouse-etl-0.9.1.2 lib/etl/parser/fixed_width_parser.rb
jayzes-activewarehouse-etl-0.9.1.1 lib/etl/parser/fixed_width_parser.rb
jayzes-activewarehouse-etl-0.9.1.2 lib/etl/parser/fixed_width_parser.rb
jayzes-activewarehouse-etl-0.9.1 lib/etl/parser/fixed_width_parser.rb
activewarehouse-etl-1.0.0 lib/etl/parser/fixed_width_parser.rb
activewarehouse-etl-1.0.0.rc1 lib/etl/parser/fixed_width_parser.rb
etl-0.9.5.rc1 lib/etl/parser/fixed_width_parser.rb
activewarehouse-etl-sgonyea-0.9.6 lib/etl/parser/fixed_width_parser.rb
activewarehouse-etl-0.9.5.rc1 lib/etl/parser/fixed_width_parser.rb
darrell-activewarehouse-etl-0.9.1.6 lib/etl/parser/fixed_width_parser.rb
darrell-activewarehouse-etl-0.9.1.4 lib/etl/parser/fixed_width_parser.rb
balinterdi-activewarehouse-etl-0.9.2 lib/etl/parser/fixed_width_parser.rb
activewarehouse-etl-0.6.1 lib/etl/parser/fixed_width_parser.rb
activewarehouse-etl-0.7.0 lib/etl/parser/fixed_width_parser.rb
activewarehouse-etl-0.8.0 lib/etl/parser/fixed_width_parser.rb
activewarehouse-etl-0.7.2 lib/etl/parser/fixed_width_parser.rb
activewarehouse-etl-0.7.1 lib/etl/parser/fixed_width_parser.rb