Class: ETL::Parser::FixedWidthParser
- Defined in:
- lib/etl/parser/fixed_width_parser.rb
Overview
Parser for fixed with files
Instance Attribute Summary
Attributes inherited from Parser
Instance Method Summary collapse
-
#each ⇒ Object
Return each row.
-
#fields ⇒ Object
Return a map of defined fields.
-
#initialize(source, options = {}) ⇒ FixedWidthParser
constructor
Initialize the parser *
source
: The source object *options
: Parser options Hash.
Methods inherited from Parser
Constructor Details
#initialize(source, options = {}) ⇒ FixedWidthParser
Initialize the parser
-
source
: The source object -
options
: Parser options Hash
8 9 10 11 |
# File 'lib/etl/parser/fixed_width_parser.rb', line 8 def initialize(source, ={}) super configure end |
Instance Method Details
#each ⇒ Object
Return each row
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/etl/parser/fixed_width_parser.rb', line 14 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 |
#fields ⇒ Object
Return a map of defined fields
33 34 35 |
# File 'lib/etl/parser/fixed_width_parser.rb', line 33 def fields @fields ||= {} end |