• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Flat Filer Presentation
 

Flat Filer Presentation

on

  • 385 views

 

Statistics

Views

Total Views
385
Views on SlideShare
385
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • * Mention pack and unpack * Bring up intermediate classes that add domain specific fields

Flat Filer Presentation Flat Filer Presentation Presentation Transcript

  • Flat Filer
      • Andy Libby
      • Tangeis, LLC
      • [email_address]
  • Agenda
    • Our Needs
    • Our Solution
    • What are flat files
    • Filer class and basic usage
    • Typical Tasks
    • Filters and Formatters
    • Q/A
  • Our Need
    • Consume and produce data files provided by various vendors
    • Make it easy to update ActiveRecord objects from flat file records.
    • We could not find anything pre-existing
  • What is Flat Filer
    • Small Ruby library for reading and writing fixed width flat files.
    • Gives syntactically clean interface to pack/ unpack for flat file records
    • MIT Licensed
    • Available at github
  • Typical Flat File
      • 1 2 3
      • 012345678901234567890123456789
      • idfirst last birthday
      • 1Linus Torvalds 19691228
      • 2Randall Schwartz 19611122
      • 3Xavier Leroy 19680315
  • A Basic class
      • class PersonFile < FlatFile
      • add_field :id, :width => 2, :filter => proc { |v| v.to_i }
      • add_field :first_name, :width => 10
      • add_field :last_name, :width => 10
      • add_field :birthday, :width => 8,
      • :filter => proc { |v| Date.parse v },
      • :formatter => proc { |v| v.strftime(“%Y%m%d”)
      • end
  • Typical Task
      • filer = PersonFile.new
      • filer.each_record(fh) do |record, line|
      • person = Person.find(record.id)
      • next unless person
      • record.map_in person
      • person.save!
      • end
  • Writing a flat file
      • @writer_objects.each do |obj|
      • r =PersonFile.new_record(obj)
      • puts r.to_s
      • end
  • Other Options for add_field
    • :map_in_proc – Specify logic to execute when mapping in the field.
    • :aggressive – if true, overwrite existing values in a model when “mapping in”
  • Filters and Formatters
    • Execute code on fields either on the way in or on the way out of a filer
    • Filters execute when reading files
    • Formatters execute when writing files (okay when calling to_s on records)
    • Can be a symbol, lambda, or object responding to filter call.
  • Filter/ Formatter Example
      • class PersonFile < FlatFile
      • add_field :id, :width => 2, :filter => proc { |v| v.to_i }
      • add_field :first_name, :width => 10
      • add_field :last_name, :width => 10
      • add_field :birthday, :width => 8,
      • :filter => proc { |v| Date.parse v },
      • :formatter => proc { |v| v.strftime(“%Y%m%d”)
      • end
  • Getting Flat Filer and Questions
      • http://github.com/alibby/flat_filer/