Class: Datafile::Bundle

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/formats/datafile.rb

Overview

todo/fix: turn into Datafile::Bundle.new and Bundle#write/save -why? why not?

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Bundle

Returns a new instance of Bundle.



15
16
17
18
# File 'lib/sportdb/formats/datafile.rb', line 15

def initialize( path )
  @path = path
  @buf  = String.new('')
end

Instance Method Details

#<<(value) ⇒ Object Also known as: write



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sportdb/formats/datafile.rb', line 20

def <<(value)
   if value.is_a?( Array )  ## assume array of datafiles (file paths)
     datafiles = value
     datafiles.each do |datafile|
       text = Datafile.read( datafile )
       ## todo/fix/check:  move  sub __END__ to Datafile.read and turn it always on  -  why? why not?
       text = text.sub( /__END__.*/m, '' )    ## note: add/allow support for __END__; use m-multiline flag
       @buf << text
       @buf << "\n\n"
      end
    else ## assume string (e.g. header, comments, etc.)
      text = value
      @buf << text
      @buf << "\n\n"
    end
end

#closeObject

todo/fix/check: write only on close? or write on every write and use close for close?



39
40
41
42
43
# File 'lib/sportdb/formats/datafile.rb', line 39

def close
  File.open( @path, 'w:utf-8' ) do |f|
    f.write @buf
  end
end