Skip to content

Instantly share code, notes, and snippets.

@bozydar
Created August 29, 2012 09:21
Show Gist options
  • Save bozydar/3508956 to your computer and use it in GitHub Desktop.
Save bozydar/3508956 to your computer and use it in GitHub Desktop.
class BoardingFile < DataPump
input :hash do
record :merchant do
column :name, :validate => {:re => /[A-Z][a-z]+/}
end
records :equipments do
column :id, :validate => {:type => Integer}
column :name, :validate => {:re => /.{,10}/}
end
end
converter do |input, output|
input[:equipments].each do |item|
output[:chain] << {:id => item[:id], :equipment_name => item[:name], :merchant_name => input[:merchant][:name]}
end
output[:header] << {:phys_number_of_records => output[:chain].length + 1}
end
output :fixed_width_file do
record :header, 'H' do
column(:type, 1, 3, :default => 'HDR')
column(:file_title, 4, 3, :default => 'MBF')
column(:phys_number_of_records, 7, 2, :format => "%02d")
end
record :chain, 'C' do
column(:id, 1, 7, :format => "%07d")
column(:equipment_name, 8, 20)
column(:merchant_name, 28, 50)
end
end
# and then somewhere in the axiom...
exporter = BoardingFile.new(:output => File.new("result.dat"))
begin
exporter << { :merchant => {:name => "Robert"}, :equipments => [ { :id => 1, :name => "screwdrive"}, { :id => 2, "hammer" } ] }
rescue DataPumpError => err
err.errors.each |item| do
# position is path for hash. eg: merchant->equipments[2]->name
# for file reading it would be line number and offset in the line
puts "Err: #{item.position}, #{item.description}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment