Class: Stockboy::Readers::FixedWidth

Inherits:
Stockboy::Reader show all
Defined in:
lib/stockboy/readers/fixed_width.rb

Overview

For reading fixed-width data split by column widths

Options collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ FixedWidth

Initialize a new fixed-width reader

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • headers (Array<Integer>, Hash<Integer>)
  • skip_header_rows (Integer)
  • skip_footer_rows (Integer)
  • encoding (String)


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

#encodingString

Override original file encoding

Returns:

  • (String)


62
# File 'lib/stockboy/readers/fixed_width.rb', line 62

dsl_attr :encoding

#headersArray<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.

Examples:

reader.headers = [10, 5, 10, 42]
reader.parse(data)
#=> [{0=>"Arthur", 1=>"42", 2=>"Earth", 3=>""}]

reader.headers = {name: 10, age: 5, planet: 10, notes: 42}
reader.parse(data)
#=> [{name: "Arthur", age: "42", planet: "Earth", notes: ""}]

Returns:

  • (Array<Integer>, Hash{Object=>Integer})


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
  skip_footer_rows.times { records.pop }
  records
end

#row_formatString

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.

Examples:

row_format "U16U32" # column A: 16 unicode, column B: 32 unicode

Returns:

  • (String)

See Also:



40
# File 'lib/stockboy/readers/fixed_width.rb', line 40

dsl_attr :row_format, attr_reader: false

Number of file rows to skip at end of file

Useful if the file ends with a summary or notice

Returns:

  • (Integer)


56
# File 'lib/stockboy/readers/fixed_width.rb', line 56

dsl_attr :skip_footer_rows

#skip_header_rowsInteger

Number of file rows to skip from start of file

Useful if the file starts with a preamble or header metadata

Returns:

  • (Integer)


48
# File 'lib/stockboy/readers/fixed_width.rb', line 48

dsl_attr :skip_header_rows