Skip to content

stevemolitor/pancakes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pancakes

Parse fixed width files into Ruby object model.

DESCRIPTION

FEATURES/PROBLEMS:

  • Friendly DSL

  • Map fixed width fields to Ruby attributes

  • Key based relationships - has_one, has_many

SYNOPSIS:

Fixed width file order.txt:

ORD         123
ORD_HEADER  a special order
ORD_L       1    car engine400.99
ORD_L       2    toothpick   1.99

Ruby code to parse above file:

class Order < Pancakes::PositionalPancake
  field :order_id, :position => 12..16

  has_one  :order_header, :key => "ORD_HEADER"
  has_many :order_lines,  :key => "ORD_L"
end

class OrderHeader < Pancakes::PositionalPancake
  field :description, :position => 12..21
end

class OrderLine < Pancakes::PositionalPancake
  field :line_num,    :start => 12..16, :as => :integer
  field :description, :start => 17..26
  field :price,       :start => 27..32, :as => :float
end

File.open("order.txt") do |f|
  order = Order.load(f)

  puts order.order_id                 # => '123'
  puts order.order_header.description # => 'a special order'

  order.order_lines.each do |order_line|
    puts "line num: #{order_line.line_num}, price: #{order_line.price}"
  end
end

Copyright © 2010 Steve Molitor. See LICENSE for details.

About

Maps fixed width or delimited flat files to ruby objects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages