Skip to content

Support generation of fixed width data #1

Description

@seanbehan

This code should do it...

# Fixedwidth.generate(data: input, header: header, start: start, stop: stop, delimiter: "|") do |line|
#  output << line
# end

module Fixedwidth
  def self.generate(options)
    @options = options
    @options[:delimiter] ||= ","

    @options[:data].each do |hash|
      string = " " * (string_length*2) # FIXME: Find of end of line!
      hash.sort_by { |k,v| header_positions["#{k}"][0] }.each do |k,v|
        positions = header_positions["#{k}"]
        string[positions[0],positions[1]]="#{v}"
      end

      yield(string)
    end
  end

  def self.string_length
    column_positions.last.inject(0) { |i,sum| sum += i }
  end

  def self.header_positions
    @header_positions ||= {}.tap do |positions|
      start.zip(stop).each_with_index do |item,index|
        a, b = item[0].to_i, item[1].to_i
        a = a - 1
        b = b-a

        positions.merge!(header[index] => [a,b])
      end
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions