lib/files.rb in files-0.0.2 vs lib/files.rb in files-0.0.3
- old
+ new
@@ -1,15 +1,24 @@
require "files/version"
module Files
- def self.create options = {:remove => true}, &block
+
+ def self.default_options level = 2
+ {:remove => true, :name => called_from(level)}
+ end
+
+ def self.called_from level = 1
+ File.basename caller[level].split(':').first, ".rb"
+ end
+
+ def self.create options = default_options, &block
require 'tmpdir'
require 'fileutils'
- called_from = File.basename caller.first.split(':').first, ".rb"
- path = File.join(Dir::tmpdir, "#{called_from}_#{Time.now.to_i}_#{rand(1000)}")
+ name = options[:name]
+ path = File.join(Dir::tmpdir, "#{name}_#{Time.now.to_i}_#{rand(1000)}")
files = Files.new path, block, options
files.root
end
@@ -23,17 +32,18 @@
@dirs = [path]
Dir.mkdir(path)
at_exit {FileUtils.rm_rf(path) if File.exists?(path)} if options[:remove]
- instance_eval &block
+ instance_eval &block if block
end
def dir name, &block
Dir.mkdir "#{current}/#{name}"
@dirs << name
- instance_eval &block
+ instance_eval &block if block
+ @dirs.pop
end
def file name, contents = "contents of #{name}"
if name.is_a? File
FileUtils.cp name.path, current
@@ -55,8 +65,8 @@
end
end
end
-def Files *args, &block
- Files.create *args, &block
+def Files options = Files.default_options, &block
+ Files.create options, &block
end