# Class ReserveDB (C)2009 B.O. # refer below for MonitorMixin # http://www.ruby-lang.org/ja/man/html/MonitorMixin.html require( "monitor") require( "IEPGD") require( 'to_WinFileName' )
class ReserveDB include To_WinFileName include Enumerable @@EXT = "tvpid"
def initialize( workFolder ) @workFolder = File::expand_path( workFolder) self.extend(MonitorMixin) if ! FileTest::directory?( @workFolder) Dir::mkdir( @workFolder ) end end #def initialize
def to_s "ReserveDB[#{@workFolder}]" end #def to_s
def clear synchronize { Dir::glob( "#{@workFolder}/*.#{@@EXT}").each {|f| File.delete( f ) } } end #def clear
def length to_a().length() end # def length
def empty? length() == 0 end #def empty?
# adding an iEPGD data # needs to check if the same program has existed already def push( iEPGo ) synchronize { del( iEPGo) fname = iEPGo.to_s("%D__%T_%eT_%program-title:_%station-name:_%station:" ) fname = to_WinFileName(fname ) fname += ".#{@@EXT}" fh = File.open("#{@workFolder}/#{fname}",'w') fh.puts( iEPGo.to_s ) fh.close } end # def push( iEPG )
# delete # if successful, returns true # if not returns nil def del( iEPGo ) ret = false # gather all data in the DB synchronize { Dir::glob( "#{@workFolder}/*.#{@@EXT}").each {|f| if (arrayiEPG= IEPG.File2Array( f)) if arrayiEPG[0].sameProgram?( iEPGo ) File.delete( f ) ret = true end end } } ret end # def _del( oiEPG )
def to_a ar = Array.new synchronize { # gather all data in the DB Dir::glob( "#{@workFolder}/*.#{@@EXT}").each {|f| if (arrayiEPG= IEPG.File2Array( f)) ar += arrayiEPG end } } ar end #def to_a
def each ar = to_a ar.each{|prof| yield(prof) } end # def each
def top() min() end # def top
def pop synchronize { if (tmp = top()) del( tmp ) return tmp else return nil end } end #def pop