Class: Datafile::ZipPackage

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

Overview

helper wrapper for datafiles in zips

Defined Under Namespace

Classes: Entry

Constant Summary

Constants inherited from Package

Package::EXCLUDE_RE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Package

match_exclude

Constructor Details

#initialize(path) ⇒ ZipPackage

Returns a new instance of ZipPackage.



90
91
92
93
94
95
96
# 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

Instance Attribute Details

#nameObject (readonly)

class ZipPackage::Entry



88
89
90
# File 'lib/sportdb/formats/datafile_package.rb', line 88

def name
  @name
end

#pathObject (readonly)

class ZipPackage::Entry



88
89
90
# File 'lib/sportdb/formats/datafile_package.rb', line 88

def path
  @path
end

Instance Method Details

#each(pattern:) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# 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