Class: Dpl::Zip

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

Constant Summary collapse

ZIP_EXT =
%w[.zip .jar].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZip

Returns a new instance of Zip.



9
10
11
12
# File 'lib/dpl/helper/zip.rb', line 9

def initialize(*)
  require 'zip'
  super
end

Instance Attribute Details

#destObject

Returns the value of attribute dest

Returns:

  • (Object)

    the current value of dest



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

def dest
  @dest
end

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



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

def opts
  @opts
end

#srcObject

Returns the value of attribute src

Returns:

  • (Object)

    the current value of src



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

def src
  @src
end

Instance Method Details

#copyObject



49
50
51
# File 'lib/dpl/helper/zip.rb', line 49

def copy
  FileUtils.cp(src, dest)
end

#create(files) ⇒ Object



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

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)


45
46
47
# File 'lib/dpl/helper/zip.rb', line 45

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

#dot_match?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/dpl/helper/zip.rb', line 59

def dot_match?
  opts[:dot_match]
end

#extsObject



63
64
65
# File 'lib/dpl/helper/zip.rb', line 63

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

#globObject



53
54
55
56
57
# File 'lib/dpl/helper/zip.rb', line 53

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

#zipObject



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

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

#zip_dirObject



24
25
26
# File 'lib/dpl/helper/zip.rb', line 24

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

#zip_fileObject



28
29
30
# File 'lib/dpl/helper/zip.rb', line 28

def zip_file
  create([src])
end

#zip_file?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/dpl/helper/zip.rb', line 41

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