Class: Parcel::ZipFileRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/parcel/zip_file_repository.rb

Overview

The FileRepository class represents a compressed archive of files providing an easy interface for enumerating and updating the contents.

Instance Method Summary collapse

Constructor Details

#initialize(owner = nil, options = Hash.new) ⇒ ZipFileRepository

Create a blank FileRepository, or open one from an existing file object.



12
13
14
15
16
# File 'lib/parcel/zip_file_repository.rb', line 12

def initialize(owner = nil, options = Hash.new)
  @temp_file = Parcel.temp_path("#{owner.object_id}_#{rand(99999)}.zip")
  @owner = owner
  @name = options[:name]
end

Instance Method Details

#add_file(filename, contents) ⇒ Object

Adds a file to the repository.



73
74
75
76
77
# File 'lib/parcel/zip_file_repository.rb', line 73

def add_file(filename, contents)
  Zip::ZipFile.open( @temp_file, Zip::ZipFile::CREATE ) do |writer|
    writer.get_output_stream(filename) { |file| file.write contents }
  end
end

#blank?Boolean

Returns true if there are no files in the repository, or if the attachment is nil.

Returns:

  • (Boolean)


31
32
33
# File 'lib/parcel/zip_file_repository.rb', line 31

def blank?
  files.blank?
end

#clear!Object

Clears the repository.



36
37
38
39
40
41
# File 'lib/parcel/zip_file_repository.rb', line 36

def clear!
  File.unlink(@temp_file) if File.exist?(@temp_file)
  
  Zip::ZipFile.open( @temp_file, Zip::ZipFile::CREATE ) do
  end
end

#commit!Object

Raises:

  • (ArgumentError)


89
90
91
92
# File 'lib/parcel/zip_file_repository.rb', line 89

def commit!
  raise ArgumentError, "No owner defined, use \#save_to instead" if @owner == nil
  save_to(commit_path)
end

#commit_pathObject



26
27
28
# File 'lib/parcel/zip_file_repository.rb', line 26

def commit_path
  @owner.parcel_data_path(@name) + ".zip"
end

#filesObject

Returns all the files present in the repository.



44
45
46
47
48
49
50
# File 'lib/parcel/zip_file_repository.rb', line 44

def files
  return [] if @temp_file.nil? || !File.exist?(@temp_file)

  result = Array.new
  Zip::ZipFile.foreach(@temp_file) { |entry| result << OpenStruct.new(:name => entry.name, :size => entry.size) }
  result
end

#import(source) ⇒ Object



18
19
20
# File 'lib/parcel/zip_file_repository.rb', line 18

def import(source)
  File.open(@temp_file, "w") { |f| f.write source.read }
end

#import_data(data) ⇒ Object



22
23
24
# File 'lib/parcel/zip_file_repository.rb', line 22

def import_data(data)
  File.open(@temp_file, "w") { |f| f.write data }
end

#read_file(filename) ⇒ Object

Returns the contents of the given file. Supports wildcard matching, in which case returns the contents of the first file found.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/parcel/zip_file_repository.rb', line 54

def read_file(filename)
  return nil if blank?

  file_match = Regexp.new("^" + filename.gsub(".", "\\.").gsub("*", ".*").gsub("?", ".") + "$", Regexp::IGNORECASE)

  Zip::ZipFile.foreach(@temp_file) do |entry|
    if entry.name =~ file_match
      read_file = Parcel.temp_path(File.basename(entry.name))
      entry.extract(read_file)
      result = IO.read(read_file)
      File.unlink(read_file)
      return result
    end
  end

  return nil
end

#save_to(destination) ⇒ Object



84
85
86
87
# File 'lib/parcel/zip_file_repository.rb', line 84

def save_to(destination)
  FileUtils.mkdir_p(File.dirname(destination))
  FileUtils.cp(@temp_file, destination) if @temp_file && File.exist?(@temp_file)
end

#to_fileObject

Returns an open file stream of the repository.



80
81
82
# File 'lib/parcel/zip_file_repository.rb', line 80

def to_file
  blank? ? nil : File.open(@temp_file)
end