Class: Dpl::Zip

Inherits:
Struct
  • Object
show all
Defined in:
lib/dpl/helper/zip.rb

Constant Summary collapse

ZIP_EXT =
%w(.zip .jar)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZip

Returns a new instance of Zip.



7
8
9
10
# File 'lib/dpl/helper/zip.rb', line 7

def initialize(*)
  require 'zip'
  super
end

Instance Attribute Details

#destObject

Returns the value of attribute dest

Returns:

  • (Object)

    the current value of dest



4
5
6
# File 'lib/dpl/helper/zip.rb', line 4

def dest
  @dest
end

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



4
5
6
# File 'lib/dpl/helper/zip.rb', line 4

def opts
  @opts
end

#srcObject

Returns the value of attribute src

Returns:

  • (Object)

    the current value of src



4
5
6
# File 'lib/dpl/helper/zip.rb', line 4

def src
  @src
end

Instance Method Details

#copyObject



47
48
49
# File 'lib/dpl/helper/zip.rb', line 47

def copy
  FileUtils.cp(src, dest)
end

#create(files) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/dpl/helper/zip.rb', line 30

def create(files)
  ::Zip::File.open(dest, ::Zip::File::CREATE) do |zip|
    files.each do |file|
      zip.add(file.sub("#{src}/", ''), file)
    end
  end
  File.new(dest)
end

#dir?(path = src) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/dpl/helper/zip.rb', line 43

def dir?(path = src)
  File.directory?(path)
end

#dot_match?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/dpl/helper/zip.rb', line 57

def dot_match?
  opts[:dot_match]
end

#extsObject



61
62
63
# File 'lib/dpl/helper/zip.rb', line 61

def exts
  opts[:exts] ||= ZIP_EXT
end

#globObject



51
52
53
54
55
# File 'lib/dpl/helper/zip.rb', line 51

def glob
  glob = ["#{src}/**/*"]
  glob << File::FNM_DOTMATCH if dot_match?
  glob
end

#zipObject



12
13
14
15
16
17
18
19
20
# File 'lib/dpl/helper/zip.rb', line 12

def zip
  if zip_file?
    File.new(src)
  elsif dir?
    zip_dir
  else
    zip_file
  end
end

#zip_dirObject



22
23
24
# File 'lib/dpl/helper/zip.rb', line 22

def zip_dir
  create(Dir.glob(*glob).reject { |path| dir?(path) })
end

#zip_fileObject



26
27
28
# File 'lib/dpl/helper/zip.rb', line 26

def zip_file
  create([src])
end

#zip_file?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/dpl/helper/zip.rb', line 39

def zip_file?
  exts.include?(File.extname(src))
end