Class: Stockboy::Readers::FixedWidth
- Inherits:
-
Stockboy::Reader
- Object
- Stockboy::Reader
- Stockboy::Readers::FixedWidth
- Defined in:
- lib/stockboy/readers/fixed_width.rb
Overview
For reading fixed-width data split by column widths
Options collapse
-
#encoding ⇒ String
Override original file encoding.
-
#headers ⇒ Array<Integer>, Hash{Object=>Integer}
Widths of data columns with optional names.
-
#row_format ⇒ String
String format used for unpacking rows.
-
#skip_footer_rows ⇒ Integer
Number of file rows to skip at end of file.
-
#skip_header_rows ⇒ Integer
Number of file rows to skip from start of file.
Instance Method Summary collapse
-
#initialize(opts = {}, &block) ⇒ FixedWidth
constructor
Initialize a new fixed-width reader.
- #parse(data) ⇒ Object
Constructor Details
#initialize(opts = {}, &block) ⇒ FixedWidth
Initialize a new fixed-width reader
74 75 76 77 78 79 80 |
# File 'lib/stockboy/readers/fixed_width.rb', line 74 def initialize(opts={}, &block) super @headers = opts[:headers] @skip_header_rows = opts.fetch(:skip_header_rows, 0) @skip_footer_rows = opts.fetch(:skip_footer_rows, 0) DSL.new(self).instance_eval(&block) if block_given? end |
Instance Method Details
#encoding ⇒ String
Override original file encoding
62 |
# File 'lib/stockboy/readers/fixed_width.rb', line 62 dsl_attr :encoding |
#headers ⇒ Array<Integer>, Hash{Object=>Integer}
Widths of data columns with optional names
Array format will use numeric indexes for field keys. Hash will use the keys for naming the fields.
27 |
# File 'lib/stockboy/readers/fixed_width.rb', line 27 dsl_attr :headers |
#parse(data) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/stockboy/readers/fixed_width.rb', line 82 def parse(data) validate_headers data.force_encoding(encoding) if encoding data = StringIO.new(data) unless data.is_a? StringIO skip_header_rows.times { data.readline } records = data.each_with_object([]) do |row, a| a << parse_row(row) unless row.strip.empty? end .times { records.pop } records end |
#row_format ⇒ String
String format used for unpacking rows
This is read from the #headers attribute by default but can be overridden. Uses implementation from String#unpack to set field widths and types.
40 |
# File 'lib/stockboy/readers/fixed_width.rb', line 40 dsl_attr :row_format, attr_reader: false |
#skip_footer_rows ⇒ Integer
Number of file rows to skip at end of file
Useful if the file ends with a summary or notice
56 |
# File 'lib/stockboy/readers/fixed_width.rb', line 56 dsl_attr :skip_footer_rows |
#skip_header_rows ⇒ Integer
Number of file rows to skip from start of file
Useful if the file starts with a preamble or header metadata
48 |
# File 'lib/stockboy/readers/fixed_width.rb', line 48 dsl_attr :skip_header_rows |