Class: Datafile::ZipPackage
- 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
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
class ZipPackage::Entry.
-
#path ⇒ Object
readonly
class ZipPackage::Entry.
Instance Method Summary collapse
- #each(pattern:) ⇒ Object
- #find(name) ⇒ Object
-
#initialize(path) ⇒ ZipPackage
constructor
A new instance of ZipPackage.
Methods inherited from Package
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
#name ⇒ Object (readonly)
class ZipPackage::Entry
88 89 90 |
# File 'lib/sportdb/formats/datafile_package.rb', line 88 def name @name end |
#path ⇒ Object (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 |