Class: BlockTxt
- Inherits:
-
Object
- Object
- BlockTxt
- Defined in:
- lib/sportdb/parser/blocktxt.rb
Overview
todo/chekc - find a better name SectTxt or ??
Constant Summary collapse
- END_RE =
quick support for __END__
%r{ ^ [ ]* __END__ [ ]* .*? \z ## note - until end-of-string/file !!! }mx- SECT_RE =
e.g. § or §§ or § § § or such
maybe allow :: § :: or such too or --- § --- or such %r{^ [ ]* § ([ ]*§)* [ ]* $}x- SUBSECT_RE =
split by “ => or =====> ” todo/check - subsect?? find a better name? in/out or txt1/txt2
%r{^ [ ]* =+ > [ ]* $}x
Class Method Summary collapse
Instance Method Summary collapse
- #_parse(txt) ⇒ Object
-
#dump ⇒ Object
for debugging.
- #each(&blk) ⇒ Object
- #each_with_index(&blk) ⇒ Object
-
#initialize(txt) ⇒ BlockTxt
constructor
A new instance of BlockTxt.
- #size ⇒ Object
- #text ⇒ Object
Constructor Details
#initialize(txt) ⇒ BlockTxt
Returns a new instance of BlockTxt.
12 13 14 15 |
# File 'lib/sportdb/parser/blocktxt.rb', line 12 def initialize( txt ) @sects = _parse( txt ) self end |
Class Method Details
.parse(txt) ⇒ Object
8 |
# File 'lib/sportdb/parser/blocktxt.rb', line 8 def self.parse( txt ) new( txt); end |
.read(path) ⇒ Object
9 |
# File 'lib/sportdb/parser/blocktxt.rb', line 9 def self.read( path ) parse( read_text( path )); end |
Instance Method Details
#_parse(txt) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sportdb/parser/blocktxt.rb', line 70 def _parse( txt ) blocks = [] ## note - holds [txt,exp] pairs txt = txt.sub( END_RE, '' ) ## split by § sections = txt.split( SECT_RE ) sections.each_with_index do |sect,i| ## puts ">>> start #{i+1}" ## pp sect ## puts "<<< end #{i+1}" txt1, txt2 = sect.split( SUBSECT_RE ) blocks << [txt1,txt2] end blocks end |
#dump ⇒ Object
for debugging
29 30 31 32 33 |
# File 'lib/sportdb/parser/blocktxt.rb', line 29 def dump ## for debugging puts "==> sects (#{@sects.size}):" pp @sects puts " #{@sects.size} sect(s)" end |
#each(&blk) ⇒ Object
19 |
# File 'lib/sportdb/parser/blocktxt.rb', line 19 def each( &blk ) @sects.each( &blk ); end |
#each_with_index(&blk) ⇒ Object
20 |
# File 'lib/sportdb/parser/blocktxt.rb', line 20 def each_with_index( &blk ) @sects.each_with_index( &blk ); end |
#size ⇒ Object
17 |
# File 'lib/sportdb/parser/blocktxt.rb', line 17 def size() @sects.size; end |
#text ⇒ Object
23 24 25 26 |
# File 'lib/sportdb/parser/blocktxt.rb', line 23 def text ## only get all txt1 parts joined as single all-in-one string txt @sects.map {|sect| sect[0] }.join( "\n" ) end |