lib/unpack.rb in unpack-0.1.4 vs lib/unpack.rb in unpack-0.1.6

- old
+ new

@@ -1,11 +1,11 @@ require 'mimer_plus' require 'unpack/container' require 'fileutils' class Unpack - attr_accessor :files, :options + attr_accessor :files, :options, :directory, :removeable def initialize(args) args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] } @options = { @@ -27,12 +27,32 @@ # Makes shure that every directory structure looks the same @directory = Dir.new(@directory).path rescue nil raise Exception.new("You need to specify a valid path") if @directory.nil? or not Dir.exist?(@directory) raise Exception.new("You need unzip to keep going") if %x{whereis unzip}.empty? + + @files = [] end + def self.it!(args) + # If no to argument is given, file will be unpacked in the same dir + args[:to] = args[:to].nil? ? File.dirname(args[:file]) : args[:to] + + # Adding the options that is being passed to {it!} directly to {Unpack} + this = self.new(directory: args[:to], options: {min_files: 0}.merge(args)) + + # Is the file path absolute ? good, do nothing : get the absolute path + file = args[:file].match(/^\//) ? args[:file] : File.expand_path(args[:file]) + + this.files << file + this.unpack! + this.wipe! if this.options[:remove] + + # Only one files has been unpacked, that is why we select the first object in the list + this.diff.first + end + def self.runner!(directory = '.', options = {}) unpack = Unpack.new(directory: directory, options: options) rescue nil # If the initializer raised any excetions return [] if unpack.nil? @@ -67,10 +87,13 @@ end def unpack! @files.each do |file| type = Mimer.identify(file) - path = File.dirname(file) + + # To what directory want we to unpack the file ? The same as the file : The one that where specified in {initialize} + path = @directory == File.dirname(file) ? File.dirname(file) : @directory + before = Dir.new(path).entries if type.zip? @removeable.merge!(path => {:file_type => 'zip'}) self.unzip(path: path, file: file)