]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/boulder.rb
temporary fix of bzip2 read problem
[biopieces.git] / code_ruby / lib / maasha / boulder.rb
1 # Copyright (C) 2007-2011 Martin A. Hansen.
2
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17 # http://www.gnu.org/copyleft/gpl.html
18
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
20
21 # This software is part of the Biopieces framework (www.biopieces.org).
22
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24
25 require 'maasha/filesys'
26
27 # Error class for all exceptions to do with Boulder.
28 class BoulderError < StandardError; end
29
30 # Record seperator
31 SEP = "\n=\n"
32
33 # Class to manipulate boulder records - Lincoln Steins own
34 # YAML like format:
35 # http://stein.cshl.org/boulder/docs/Boulder.html
36 class Boulder < Filesys
37   # Initialize a Boulder object.
38   # Options are for testing purposes only.
39   def initialize(input=STDIN, output=STDOUT)
40     @input  = input
41     @output = output
42   end
43
44   def each
45     while not @input.eof? do
46       block = @input.gets(SEP)
47       raise BoulderError, "Missing record seperator" unless block =~ /#{SEP}$/
48
49       record = {}
50
51       block.chomp(SEP).each_line do |line|
52         key, val = line.chomp.split('=', 2)
53
54         raise BoulderError, "Missing key/value seperator" if val.nil?
55         raise BoulderError, "Missing key"                 if key.empty?
56         raise BoulderError, "Missing value"               if val.empty?
57
58         record[key.to_sym] = val
59       end
60
61       yield record
62     end
63   end
64
65   # Method that converts and returns a hash record as
66   # a Boulder string.
67   def to_boulder(record)
68     str = ""
69
70     record.each_pair do |key, val|
71       str << "#{key}=#{val}\n"
72     end
73
74     str << "=\n"
75
76     str
77   end
78 end