Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.99 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class BinDataRelation < Veritas::Relation
  2.   def initialize(klass, io)
  3.     super([[:index, Integer]] + BinDataRelation.veritas_fields(klass), BinDataRelation.record_enumerator(klass, io))
  4.   end
  5.  
  6.   protected
  7.        
  8.   def self.veritas_fields(klass)
  9.     veritas_fields = []
  10.     for field in klass.fields
  11.       veritas_class = case field.prototype.obj_class.name
  12.         when "BinData::String", "::PascalStringField" then String
  13.         when -> n { n.to_s =~ /int/ } then Integer
  14.         else
  15.           raise "Unknown type: " + field.prototype.obj_class.to_s
  16.       end
  17.       veritas_fields << [ field.name_as_sym, veritas_class ]
  18.     end
  19.     veritas_fields
  20.   end
  21.        
  22.   def self.record_enumerator(klass, io)
  23.     Enumerator.new { |y|
  24.       index = 0
  25.       loop {
  26.         begin
  27.           record = klass.read(io)
  28.         rescue EOFError
  29.           raise StopIteration
  30.         end
  31.         row = [ index ] + klass.fields.collect { |f| record[f.name].value }
  32.         index += 1
  33.         y << row
  34.       }
  35.     }
  36.   end
  37. end