lib/files.rb in files-0.1.0 vs lib/files.rb in files-0.2.0
- old
+ new
@@ -1,9 +1,10 @@
require "files/version"
-
module Files
+
+ # class methods
def self.default_options level = 2
{:remove => true, :name => called_from(level)}
end
@@ -16,24 +17,37 @@
require 'fileutils'
name = options[:name]
path = File.join(Dir::tmpdir, "#{name}_#{Time.now.to_i}_#{rand(1000)}")
- files = Files.new path, block, options
-
- files.root
+ Files.new path, block, options
end
+ # mixin methods
+ def files options = ::Files.default_options # todo: block
+ @files ||= ::Files.create(options)
+ end
+
+ def file *args, &block
+ files.file *args, &block
+ end
+
+ def dir *args, &block
+ files.dir *args, &block
+ end
+
+ # concrete class for creating files and dirs under a temporary directory
class Files
attr_reader :root
def initialize path, block, options
@root = path
@dirs = []
dir path, &block
- at_exit {FileUtils.rm_rf(path) if File.exists?(path)} if options[:remove]
+ @dirs = [path]
+ at_exit {remove} if options[:remove]
end
def dir name, &block
path = "#{current}/#{name}"
Dir.mkdir path
@@ -60,16 +74,21 @@
end
path
end
end
+ def remove
+ FileUtils.rm_rf(@root) if File.exists?(@root)
+ end
+
private
def current
@dirs.join('/')
end
end
end
def Files options = Files.default_options, &block
- Files.create options, &block
+ files = Files.create options, &block
+ files.root
end