class IOStreams::Tabular::Parser::Fixed::Layout

Attributes

columns[R]
length[R]

Public Class Methods

new(layout) click to toggle source

Returns [Array<FixedLayout>] the layout for this fixed width file. Also validates values

# File lib/io_streams/tabular/parser/fixed.rb, line 117
def initialize(layout)
  @length  = 0
  @columns = parse_layout(layout)
end

Private Instance Methods

parse_layout(layout) click to toggle source
# File lib/io_streams/tabular/parser/fixed.rb, line 124
def parse_layout(layout)
  @length = 0
  layout.collect do |hash|
    raise(Errors::InvalidLayout, "Missing required :size in: #{hash.inspect}") unless hash.key?(:size)

    column = Column.new(**hash)
    if column.size == -1
      if @length == -1
        raise(Errors::InvalidLayout, "Only the last :size can be '-1' or :remainder in: #{hash.inspect}")
      end

      @length = -1
    else
      @length += column.size
    end
    column
  end
end