Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generation of fixed width data #1

Open
seanbehan opened this issue Oct 30, 2012 · 1 comment
Open

Support generation of fixed width data #1

seanbehan opened this issue Oct 30, 2012 · 1 comment

Comments

@seanbehan
Copy link
Member

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
@darrencauthon
Copy link

Why not issue a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants