class IOStreams::Tabular::Parser::Fixed
Parsing and rendering fixed length data
Attributes
Public Class Methods
Returns [IOStreams::Tabular::Parser]
Parameters:
layout: [Array<Hash>] [ {size: 23, key: "name"}, {size: 40, key: "address"}, {size: 2}, {size: 5, key: "zip"}, {size: 8, key: "age", type: :integer}, {size: 10, key: "weight", type: :float, decimals: 2} ]
Notes:
-
Leave out the name of the key to ignore that column during parsing, and to space fill when rendering. For example as a filler.
Types:
:string This is the default type. Applies space padding and the value is left justified. Returns value as a String :integer Applies zero padding to the left. Returns value as an Integer Raises Errors::ValueTooLong when the supplied value cannot be rendered in `size` characters. :float Applies zero padding to the left. Returns value as a float. The :size is the total size of this field including the `.` and the decimals. Number of :decimals Raises Errors::ValueTooLong when the supplied value cannot be rendered in `size` characters.
In some circumstances the length of the last column is variable.
layout: [Array<Hash>] [ {size: 23, key: "name"}, {size: :remainder, key: "rest"} ]
By setting a size of `:remainder` it will take the rest of the line as the value for that column.
A size of `:remainder` and no `:key` will discard the remainder of the line without validating the length.
layout: [Array<Hash>] [ {size: 23, key: "name"}, {size: :remainder} ]
# File lib/io_streams/tabular/parser/fixed.rb, line 56 def initialize(layout:, truncate: true) @layout = Layout.new(layout) @truncate = truncate end
Public Instance Methods
The required line length for every fixed length line
# File lib/io_streams/tabular/parser/fixed.rb, line 62 def line_length layout.length end
Returns [Hash<Symbol, String>] fixed layout values extracted from the supplied line. String will be encoded to `encoding`
# File lib/io_streams/tabular/parser/fixed.rb, line 83 def parse(line) unless line.is_a?(String) raise(Errors::TypeMismatch, "Line must be a String when format is :fixed. Actual: #{line.class.name}") end if layout.length.positive? && (line.length != layout.length) raise(Errors::InvalidLineLength, "Expected line length: #{layout.length}, actual line length: #{line.length}") end hash = {} index = 0 layout.columns.each do |column| if column.size == -1 hash[column.key] = column.parse(line[index..-1]) if column.key break end # Ignore "columns" that have no keys. E.g. Fillers hash[column.key] = column.parse(line[index, column.size]) if column.key index += column.size end hash end
Returns [String] fixed layout values extracted from the supplied hash.
Notes:
-
A nil value is considered an empty string
-
When a supplied value exceeds the column size it is truncated.
# File lib/io_streams/tabular/parser/fixed.rb, line 71 def render(row, header) hash = header.to_hash(row) result = "" layout.columns.each do |column| result << column.render(hash[column.key], truncate) end result end
The header is required as an argument and cannot be supplied in the file itself.
# File lib/io_streams/tabular/parser/fixed.rb, line 108 def requires_header? false end