Class: ZipDir::Zipper

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_dir/zipper.rb

Defined Under Namespace

Classes: Proxy

Constant Summary collapse

DEFAULT_FILENAME =
"zipper.zip".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = DEFAULT_FILENAME) ⇒ Zipper

Returns a new instance of Zipper.



9
10
11
# File 'lib/zip_dir/zipper.rb', line 9

def initialize(filename=DEFAULT_FILENAME)
  @filename = filename
end

Instance Attribute Details

#copy_pathObject (readonly)

Returns the value of attribute copy_path.



5
6
7
# File 'lib/zip_dir/zipper.rb', line 5

def copy_path
  @copy_path
end

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/zip_dir/zipper.rb', line 5

def filename
  @filename
end

Instance Method Details

#cleanupObject



39
40
41
42
43
# File 'lib/zip_dir/zipper.rb', line 39

def cleanup
  FileUtils.remove_entry_secure copy_path if copy_path
  @file = @copy_path = nil
  @generated = false
end

#fileObject



34
35
36
37
# File 'lib/zip_dir/zipper.rb', line 34

def file
  return unless generated?
  @file
end

#generate(source_path = nil, root_directory: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/zip_dir/zipper.rb', line 17

def generate(source_path=nil, root_directory: false)
  cleanup if generated?

  @copy_path = Dir.mktmpdir
  proxy = Proxy.new(copy_path)
  if source_path
    raise "should not give block and source_path" if block_given?
    proxy.add_path source_path, root_directory: root_directory
  else
    yield proxy
  end

  @file = ZipDir::Zip.new(copy_path, filename).file
ensure
  @generated = true
end

#generated?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/zip_dir/zipper.rb', line 13

def generated?
  !!@generated
end