Class: Fetchers::Tar
- Inherits:
-
Object
- Object
- Fetchers::Tar
- Defined in:
- lib/fetchers/tar.rb
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
Class Method Summary collapse
Instance Method Summary collapse
- #archive_path ⇒ Object
-
#initialize(target) ⇒ Tar
constructor
A new instance of Tar.
- #read(file) ⇒ Object
- #read_from_tar(file) ⇒ Object
Constructor Details
#initialize(target) ⇒ Tar
Returns a new instance of Tar.
26 27 28 29 30 31 32 33 |
# File 'lib/fetchers/tar.rb', line 26 def initialize(target) @target = target @contents = {} @files = [] Gem::Package::TarReader.new(Zlib::GzipReader.open(@target)) do |tar| @files = tar.map(&:full_name) end end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
13 14 15 |
# File 'lib/fetchers/tar.rb', line 13 def files @files end |
Class Method Details
.resolve(target) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/fetchers/tar.rb', line 15 def self.resolve(target) unless target.is_a?(String) && File.file?(target) && target.end_with?('.tar.gz', '.tgz') return nil end new(target) end |
Instance Method Details
#archive_path ⇒ Object
22 23 24 |
# File 'lib/fetchers/tar.rb', line 22 def archive_path target end |
#read(file) ⇒ Object
35 36 37 |
# File 'lib/fetchers/tar.rb', line 35 def read(file) @contents[file] ||= read_from_tar(file) end |
#read_from_tar(file) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/fetchers/tar.rb', line 39 def read_from_tar(file) return nil unless @files.include?(file) res = nil # NB `TarReader` includes `Enumerable` beginning with Ruby 2.x Gem::Package::TarReader.new(Zlib::GzipReader.open(@target)) do |tar| tar.each do |entry| next unless entry.file? && file == entry.full_name res = entry.read break end end res end |