Skip to content

Instantly share code, notes, and snippets.

@ahamid ahamid/gist:1129651
Created Aug 6, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.