Guest User

Untitled

a guest
Mar 1st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. require 'rubygems'
  4. require 'ar-extensions'
  5. require 'active_record'
  6. require 'ostruct'
  7.  
  8. class TextFile
  9. module DescribeMethods
  10. def file(identifier, global_field_options={})
  11. file = TextFile.new(identifier, global_field_options)
  12. yield file
  13. file
  14. end
  15. end
  16.  
  17. class Field
  18. attr_reader :start, :stop, :name
  19.  
  20. def initialize(name, options)
  21. @name = name
  22. @start, @stop = options[:start], options[:stop]
  23. @strip = options[:strip]
  24. raise "Missing :start and/or :stop" if !(@start && @stop)
  25. end
  26.  
  27. def strip?
  28. @strip
  29. end
  30. end
  31.  
  32. class Record < OpenStruct
  33. end
  34.  
  35. class << self
  36. def parse(data, options={})
  37. identifier = options[:as]
  38. definitions[identifier].parse data
  39. end
  40.  
  41. def definitions
  42. @definitions ||= {}
  43. end
  44. end
  45.  
  46. attr_accessor :record_separator
  47. attr_accessor :skip_first_line
  48.  
  49. def initialize(identifier, field_options)
  50. @identifier = identifier
  51. @field_options = field_options
  52. self.class.definitions[@identifier] = self
  53. end
  54.  
  55. def field(name, options={})
  56. @fields ||= []
  57. @fields << Field.new(name, @field_options.merge(options.dup))
  58. end
  59.  
  60. def parse(data)
  61. lines = data.split @record_separator
  62. lines.shift if @skip_first_line
  63. records = []
  64. lines.each do |line|
  65. records << Record.new
  66. record = records.last
  67. @fields.each_with_index do |field, rowno|
  68. data = line[field.start...field.stop]
  69. if data.nil?
  70. puts "nil data for #{field.name} on row #{rowno}"
  71. else
  72. data = data.strip if field.strip?
  73. end
  74. record.send("#{field.name}=", data)
  75. end
  76. end
  77. records
  78. end
  79. end
  80.  
  81. # PETER: all of the above was to just set up some simple classes, below is
  82. # where the beauty is IMO. Easy to read, easy to change.
  83. include TextFile::DescribeMethods
  84.  
  85. file :my_custom_parseable_file, :strip => true do |f|
  86. f.record_separator = "\n"
  87. f.skip_first_line = true # ignore header
  88.  
  89. #Dave, these are all the stop start values
  90. f.field :account_no, :start => 0, :stop => 8
  91. f.field :mail_date, :start => 9, :stop => 17
  92. f.field :source_code, :start => 17, :stop => 20
  93. f.field :mail_cat, :start => 20, :stop => 23
  94. f.field :mail_code, :start => 23, :stop => 25
  95. f.field :cus_no, :start => 122, :stop => 131
  96. f.field :last_name, :start => 131, :stop => 151
  97. f.field :first_name, :start => 151, :stop => 166
  98. f.field :add1, :start => 166, :stop => 196
  99. f.field :add2, :start => 196, :stop => 226
  100. f.field :city, :start => 226, :stop => 249
  101. f.field :prov, :start => 249, :stop => 251
  102. f.field :pc, :start => 253, :stop => 259
  103. f.field :lang, :start => 281, :stop => 282
  104. f.field :vin, :start => 282, :stop => 299
  105. f.field :make, :start => 299, :stop => 309
  106. f.field :model, :start => 309, :stop => 329
  107. f.field :term, :start => 341, :stop => 343
  108. f.field :day_due, :start => 343, :stop => 345
  109. f.field :mnthly_amnt, :start => 345, :stop => 352
  110. f.field :maturity_date, :start => 352, :stop => 360
  111. f.field :dealer_code, :start => 370, :stop => 375
  112. f.field :residual, :start => 376, :stop => 383
  113. f.field :branch, :start => 394, :stop => 400
  114. end
  115.  
  116. #File location
  117. records = TextFile.parse IO.read("/tmp/feb_10.TXT"), :as => :my_custom_parseable_file
  118.  
  119.  
  120. puts records.first.inspect
  121. #DB Connection
  122. # ActiveRecord::Base.establish_connection(:adapter => "mysql", :host => "localhost", :encoding => "utf8", :username => "root", :password => "", :database => "welcome")
  123.  
  124. #Customer Model
  125. # class Customer < ActiveRecord::Base
  126. # end
  127.  
  128. #Clear table before loadiing
  129. # ActiveRecord::Base.connection.execute("truncate table customers")
  130.  
  131. #Column & Values definition for AR-Extension
  132. # 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]
  133. # 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 ] }
  134. # Customer.import columns, values, :optimize=>false
  135. puts "finished!"
Add Comment
Please, Sign In to add comment