Created
August 6, 2011 19:14
-
-
Save ahamid/1129651 to your computer and use it in GitHub Desktop.
BinData + Veritas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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