class Datafile::ZipPackage

helper wrapper for datafiles in zips

Attributes

name[R]
path[R]

Public Class Methods

new( path ) click to toggle source
# File lib/sportdb/formats/datafile_package.rb, line 90
def initialize( path )
  @path = path

  extname  = File.extname( path )    ## todo/check: double check if extension is .zip - why? why not?
  basename = File.basename( path, extname )
  @name = basename
end

Public Instance Methods

each( pattern: ) { |entry| ... } click to toggle source
# File lib/sportdb/formats/datafile_package.rb, line 98
def each( pattern: )
  Zip::File.open( @path ) do |zipfile|
    zipfile.each do |entry|
      if entry.directory?
        next ## skip
      elsif entry.file?
        if EXCLUDE_RE.match( entry.name )
          ## note: skip dot dirs (e.g. .build/, .git/, etc.)
        elsif pattern.match( entry.name )
          yield( Entry.new( self, entry ) )   # wrap entry in uniform access interface / api
        else
          ## puts "  skipping >#{entry.name}<"
        end
      else
        puts "** !!! ERROR !!! #{entry.name} is unknown zip file type in >#{@path}<, sorry"
        exit 1
      end
    end
  end
end
find( name ) click to toggle source
# File lib/sportdb/formats/datafile_package.rb, line 119
def find( name )
   entries = match_entry( name )
   if entries.empty?
     puts "** !!! ERROR !!! zip entry >#{name}< not found in >#{@path}<; sorry"
     exit 1
   elsif entries.size > 1
     puts "** !!! ERROR !!! ambigious zip entry >#{name}<; found #{entries.size} entries in >#{@path}<:"
     pp entries
     exit 1
   else
     Entry.new( self, entries[0] )    # wrap entry in uniform access interface / api
   end
end

Private Instance Methods

match_entry( name ) click to toggle source
# File lib/sportdb/formats/datafile_package.rb, line 134
def match_entry( name )
  ## todo/fix:  use Zip::File.glob or find_entry or something better/faster?  why? why not?

  pattern = %r{ #{Regexp.escape( name )}    ## match string if ends with name
                 $
              }x

  entries = []
  Zip::File.open( @path ) do |zipfile|
    zipfile.each do |entry|
      if entry.directory?
        next ## skip
      elsif entry.file?
        if EXCLUDE_RE.match( entry.name )
          ## note: skip dot dirs (e.g. .build/, .git/, etc.)
        elsif pattern.match( entry.name )
          entries << entry
        else
          ## no match; skip too
        end
      else
        puts "** !!! ERROR !!! #{entry.name} is unknown zip file type in >#{@path}<, sorry"
        exit 1
      end
    end
  end
  entries
end