slither

by Ryan Wood
http://ryanwood.com

DESCRIPTION:

A simple, clean DSL for describing, writing, and parsing fixed-width text files.

FEATURES:

  • Easy DSL syntax

  • Can parse and format fixed width files

  • Templated sections for reuse

SYNOPSIS:

# Create a Slither::Defintion to describe a file format
Slither.define :simple do |d|
  # This is a template section that can be reused in other sections
  d.template :boundary do |t|
    t.column :record_type, 4  
    t.column :company_id, 12      
  end 
  # Create a header section
  d.header, :align => :left do |header|      
    # The trap tells Slither which lines should fall into this section
    header.trap { |line| line[0,4] == 'HEAD' }
    # Use the boundary template for the columns
    header.template :boundary
  end
  d.body do |body|
    body.trap { |line| line[0,4] =~ /[^(HEAD|FOOT)]/ }
    body.column :id, 10, :type => :integer
    body.column :name, 10, :align => :left
    body.spacer 3
    body.column :state, 2
  end
  d.footer do |footer|
    footer.trap { |line| line[0,4] == 'FOOT' }
    footer.template :boundary
    footer.column :record_count, 10
  end
end

Supported types are: string, integer, date, float, money, and money_with_implied_decimal.

Then either feed it a nested struct with data values to create the file in the defined format:

test_data = {
  :body => [
    { :id => 12, :name => "Ryan", :state => 'SC' },
    { :id => 23, :name => "Joe", :state => 'VA' },
    { :id => 42, :name => "Tommy", :state => 'FL' },
  ], 
  :header => { :record_type => 'HEAD', :company_id => 'ABC'  }, 
  :footer => { :record_type => 'FOOT', :company_id => 'ABC'  }
}
# Generates the file as a string
puts Slither.generate(:simple, test_data)
# Writes the file
Slither.write('outfile.txt', :simple, test_data)

or parse files already in that format into a nested hash:

parsed_data = Slither.parse('infile.txt', :test).inspect

INSTALL:

sudo gem install slither

LICENSE:

(The MIT License)

Copyright © 2008

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.