From 0222eae3a0d6efdecf9198b622d9d7b3ce10b01b Mon Sep 17 00:00:00 2001 From: martinahansen Date: Thu, 11 Jun 2009 13:37:46 +0000 Subject: [PATCH] added Data::Dumper support to bioscript git-svn-id: http://biopieces.googlecode.com/svn/trunk@522 74ccb610-7750-0410-82ae-013aeee3265d --- bp_bin/bioscript | 1 + code_ruby/Maasha/biopieces.rb | 62 +++++++++++++++++++++++++++++++++ code_ruby/Maasha/test_module.rb | 7 ++++ code_ruby/Sfern/test_module.rb | 7 ++++ 4 files changed, 77 insertions(+) create mode 100755 code_ruby/Maasha/biopieces.rb create mode 100755 code_ruby/Maasha/test_module.rb create mode 100755 code_ruby/Sfern/test_module.rb diff --git a/bp_bin/bioscript b/bp_bin/bioscript index ee172a5..6681da2 100755 --- a/bp_bin/bioscript +++ b/bp_bin/bioscript @@ -28,6 +28,7 @@ use strict; use Maasha::Biopieces qw( get_record put_record ); +use Data::Dumper; use Maasha::Common; diff --git a/code_ruby/Maasha/biopieces.rb b/code_ruby/Maasha/biopieces.rb new file mode 100755 index 0000000..0289d50 --- /dev/null +++ b/code_ruby/Maasha/biopieces.rb @@ -0,0 +1,62 @@ +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__ diff --git a/code_ruby/Maasha/test_module.rb b/code_ruby/Maasha/test_module.rb new file mode 100755 index 0000000..a94e234 --- /dev/null +++ b/code_ruby/Maasha/test_module.rb @@ -0,0 +1,7 @@ +class Test + puts "Modules Test1 loaded" + + def greet_world + puts "Hello" + end +end diff --git a/code_ruby/Sfern/test_module.rb b/code_ruby/Sfern/test_module.rb new file mode 100755 index 0000000..681661c --- /dev/null +++ b/code_ruby/Sfern/test_module.rb @@ -0,0 +1,7 @@ +class Test + puts "Modules Test2 loaded" + + def greet_world + puts "World" + end +end -- 2.39.2