Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- require 'rubygems'
- require 'ar-extensions'
- require 'active_record'
- require 'ostruct'
- class TextFile
- module DescribeMethods
- def file(identifier, global_field_options={})
- file = TextFile.new(identifier, global_field_options)
- yield file
- file
- end
- end
- class Field
- attr_reader :start, :stop, :name
- def initialize(name, options)
- @name = name
- @start, @stop = options[:start], options[:stop]
- @strip = options[:strip]
- raise "Missing :start and/or :stop" if !(@start && @stop)
- end
- def strip?
- @strip
- end
- end
- class Record < OpenStruct
- end
- class << self
- def parse(data, options={})
- identifier = options[:as]
- definitions[identifier].parse data
- end
- def definitions
- @definitions ||= {}
- end
- end
- attr_accessor :record_separator
- attr_accessor :skip_first_line
- def initialize(identifier, field_options)
- @identifier = identifier
- @field_options = field_options
- self.class.definitions[@identifier] = self
- end
- def field(name, options={})
- @fields ||= []
- @fields << Field.new(name, @field_options.merge(options.dup))
- end
- def parse(data)
- lines = data.split @record_separator
- lines.shift if @skip_first_line
- records = []
- lines.each do |line|
- records << Record.new
- record = records.last
- @fields.each_with_index do |field, rowno|
- data = line[field.start...field.stop]
- if data.nil?
- puts "nil data for #{field.name} on row #{rowno}"
- else
- data = data.strip if field.strip?
- end
- record.send("#{field.name}=", data)
- end
- end
- records
- end
- end
- # PETER: all of the above was to just set up some simple classes, below is
- # where the beauty is IMO. Easy to read, easy to change.
- include TextFile::DescribeMethods
- file :my_custom_parseable_file, :strip => true do |f|
- f.record_separator = "\n"
- f.skip_first_line = true # ignore header
- #Dave, these are all the stop start values
- f.field :account_no, :start => 0, :stop => 8
- f.field :mail_date, :start => 9, :stop => 17
- f.field :source_code, :start => 17, :stop => 20
- f.field :mail_cat, :start => 20, :stop => 23
- f.field :mail_code, :start => 23, :stop => 25
- f.field :cus_no, :start => 122, :stop => 131
- f.field :last_name, :start => 131, :stop => 151
- f.field :first_name, :start => 151, :stop => 166
- f.field :add1, :start => 166, :stop => 196
- f.field :add2, :start => 196, :stop => 226
- f.field :city, :start => 226, :stop => 249
- f.field :prov, :start => 249, :stop => 251
- f.field :pc, :start => 253, :stop => 259
- f.field :lang, :start => 281, :stop => 282
- f.field :vin, :start => 282, :stop => 299
- f.field :make, :start => 299, :stop => 309
- f.field :model, :start => 309, :stop => 329
- f.field :term, :start => 341, :stop => 343
- f.field :day_due, :start => 343, :stop => 345
- f.field :mnthly_amnt, :start => 345, :stop => 352
- f.field :maturity_date, :start => 352, :stop => 360
- f.field :dealer_code, :start => 370, :stop => 375
- f.field :residual, :start => 376, :stop => 383
- f.field :branch, :start => 394, :stop => 400
- end
- #File location
- records = TextFile.parse IO.read("/tmp/feb_10.TXT"), :as => :my_custom_parseable_file
- puts records.first.inspect
- #DB Connection
- # ActiveRecord::Base.establish_connection(:adapter => "mysql", :host => "localhost", :encoding => "utf8", :username => "root", :password => "", :database => "welcome")
- #Customer Model
- # class Customer < ActiveRecord::Base
- # end
- #Clear table before loadiing
- # ActiveRecord::Base.connection.execute("truncate table customers")
- #Column & Values definition for AR-Extension
- # columns = [:account_no, :mail_date, :source_code, :mail_cat, :mail_code, :cus_no, :last_name, :first_name, :add1, :add2, :city, :prov, :pc, :lang, :vin, :make, :model, :term, :day_due, :mnthly_amnt, :maturity_date, :dealer_code, :residual, :branch]
- # values = records.map { |r| [r.account_no, r.mail_date, r.source_code, r.mail_cat, r.mail_code, r.cus_no, r.last_name, r.first_name, r.add1, r.add2, r.city, r.prov, r.pc, r.lang, r.vin, r.make, r.model, r.term, r.day_due, r.mnthly_amnt, r.maturity_date, r.dealer_code, r.residual, r.branch ] }
- # Customer.import columns, values, :optimize=>false
- puts "finished!"
Add Comment
Please, Sign In to add comment