Coder Social home page Coder Social logo

slither's Introduction

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

Repeating Sections

To enable repeating sections simple define the prefix you want for the repeating keys in the hash and pass the repeatable option to the section that is repeatable.

Slither.define :sac_bee, :repeater => 'r' do |d|
  d.header do |h|
    h.trap { |line| line[0] == '1' }
    h.column :header_begin, 1
    h.column :destination
    h.column :payment_deposit_date, 6
  end
  d.payment_records :repeatable => true do |pr|
    pr.trap { |line| line[0] == '6' }
    pr.column :payment_amount, 10
  end
  d.batch_trailer_record :repeatable => true do |btr|
    btr.trap { |line| line[0] == '7' }
    btr.column :beginning_of_batch_trailer_record, 1
    btr.column :total_count_of_records_in_batch, 4
    btr.column :total_payment_amount_of_batch, 10
  end
  d.file_trailer_record do |ftr|
    ftr.trap { |line| line[0] == '8' }
    ftr.column :beginning_of_trailer_record, 1
    ftr.column :total_count_of_records, 5
    ftr.column :total_payment_amount, 10
  end
end

The data will be parsed into a hash as above but will have keys like “payment_records#{repeater}{num}” for each repeating section.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.