--- /dev/null
+class BioPieces
+ RECORD_DELIMITER = "\n---\n"
+
+ class Record
+ attr_reader :__hash__
+
+ def initialize( data )
+ @__hash__ = data
+ end
+
+ def method_missing( name, *args, &block )
+ if args.empty? && @__hash__.has_key?( name ) then
+ @__hash__[ name ]
+ else
+ super # get the original exception
+ end
+ end
+
+ def to_s
+ @__hash__.map { | key, value | "#{ key }: #{ value }" }.join( "\n" ) + RECORD_DELIMITER
+ end
+
+ def to_hash
+ @__hash__.dup
+ end
+ end
+
+ def self.file( path )
+ bp = new( File.open( path ) )
+ yield bp if block_given?
+ bp
+ ensure
+ # if no block was given, the user wants the BioPieces instance
+ # with a still open File so he can read it, so only close if a block
+ # had been given.
+ bp.close if block_given?
+ end
+
+ def initialize( stream )
+ @stream = stream
+ end
+
+ def close
+ @stream.close if @stream.respond_to? :close
+ end
+
+ def record_get
+ return unless block = @stream.gets( RECORD_DELIMITER )
+
+ block.chomp!( RECORD_DELIMITER )
+ data = Hash[ *block.split( /: |\n/, 2 ) ]
+ Record.new( data )
+ end
+
+ def each
+ yield record_get until @stream.eof?
+ self # not to have a messy return value
+ end
+end
+
+
+__END__