Skip to content

Instantly share code, notes, and snippets.

@ahamid
Created August 6, 2011 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahamid/1129651 to your computer and use it in GitHub Desktop.
Save ahamid/1129651 to your computer and use it in GitHub Desktop.
BinData + Veritas
class BinDataRelation < Veritas::Relation
def initialize(klass, io)
super([[:index, Integer]] + BinDataRelation.veritas_fields(klass), BinDataRelation.record_enumerator(klass, io))
end
protected
def self.veritas_fields(klass)
veritas_fields = []
for field in klass.fields
veritas_class = case field.prototype.obj_class.name
when "BinData::String", "::PascalStringField" then String
when -> n { n.to_s =~ /int/ } then Integer
else
raise "Unknown type: " + field.prototype.obj_class.to_s
end
veritas_fields << [ field.name_as_sym, veritas_class ]
end
veritas_fields
end
def self.record_enumerator(klass, io)
Enumerator.new { |y|
index = 0
loop {
begin
record = klass.read(io)
rescue EOFError
raise StopIteration
end
row = [ index ] + klass.fields.collect { |f| record[f.name].value }
index += 1
y << row
}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment