files 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -24,6 +24,7 @@ see `test/files_test.rb` for more examples
24
24
 
25
25
  * the directory will be removed at exit
26
26
  * unless you pass `:remove => false`
27
+ * the directory name is based on the name of the source file you called Files from
27
28
 
28
29
  ## TODO
29
30
 
data/lib/files.rb CHANGED
@@ -2,12 +2,21 @@ require "files/version"
2
2
 
3
3
 
4
4
  module Files
5
- def self.create options = {:remove => true}, &block
5
+
6
+ def self.default_options level = 2
7
+ {:remove => true, :name => called_from(level)}
8
+ end
9
+
10
+ def self.called_from level = 1
11
+ File.basename caller[level].split(':').first, ".rb"
12
+ end
13
+
14
+ def self.create options = default_options, &block
6
15
  require 'tmpdir'
7
16
  require 'fileutils'
8
17
 
9
- called_from = File.basename caller.first.split(':').first, ".rb"
10
- path = File.join(Dir::tmpdir, "#{called_from}_#{Time.now.to_i}_#{rand(1000)}")
18
+ name = options[:name]
19
+ path = File.join(Dir::tmpdir, "#{name}_#{Time.now.to_i}_#{rand(1000)}")
11
20
 
12
21
  files = Files.new path, block, options
13
22
 
@@ -25,13 +34,14 @@ module Files
25
34
  Dir.mkdir(path)
26
35
  at_exit {FileUtils.rm_rf(path) if File.exists?(path)} if options[:remove]
27
36
 
28
- instance_eval &block
37
+ instance_eval &block if block
29
38
  end
30
39
 
31
40
  def dir name, &block
32
41
  Dir.mkdir "#{current}/#{name}"
33
42
  @dirs << name
34
- instance_eval &block
43
+ instance_eval &block if block
44
+ @dirs.pop
35
45
  end
36
46
 
37
47
  def file name, contents = "contents of #{name}"
@@ -57,6 +67,6 @@ module Files
57
67
  end
58
68
  end
59
69
 
60
- def Files *args, &block
61
- Files.create *args, &block
70
+ def Files options = Files.default_options, &block
71
+ Files.create options, &block
62
72
  end
data/lib/files/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Files
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/test/files_test.rb CHANGED
@@ -18,6 +18,8 @@ dir = Files.create do # creates a temporary directory inside Dir.tmpdir
18
18
  end
19
19
  end
20
20
 
21
+ assert { dir.split('/').last =~ /^files_test/ }
22
+
21
23
  assert { File.read("#{dir}/hello.txt") == "contents of hello.txt" }
22
24
  assert { File.read("#{dir}/web/snippet.html") == "<h1>File under F for fantastic!</h1>" }
23
25
  assert {
@@ -35,3 +37,38 @@ dir = Files do
35
37
  end
36
38
  assert { File.read("#{dir}/hello.txt") == "contents of hello.txt" }
37
39
  assert { File.read("#{dir}/web/hello.html") == "contents of hello.html" }
40
+ assert { dir.split('/').last =~ /^files_test/ }
41
+
42
+
43
+ assert { Files.called_from(0) == "files_test" }
44
+
45
+
46
+ dir = Files do
47
+ dir "foo" do
48
+ file "foo.txt"
49
+ end
50
+ dir "bar" do
51
+ file "bar.txt"
52
+ dir "baz" do
53
+ file "baz.txt"
54
+ end
55
+ dir "baf" do
56
+ file "baf.txt"
57
+ end
58
+ end
59
+ end
60
+
61
+ assert { File.read("#{dir}/foo/foo.txt") == "contents of foo.txt" }
62
+ assert { File.read("#{dir}/bar/bar.txt") == "contents of bar.txt" }
63
+ assert { File.read("#{dir}/bar/baz/baz.txt") == "contents of baz.txt" }
64
+ assert { File.read("#{dir}/bar/baf/baf.txt") == "contents of baf.txt" }
65
+
66
+
67
+ dir = Files()
68
+ assert { File.exist? dir and File.directory? dir}
69
+
70
+ dir = Files do
71
+ dir "a"
72
+ end
73
+ assert { File.exist? "#{dir}/a" and File.directory? "#{dir}/a"}
74
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: files
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -45,7 +45,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
45
45
  version: '0'
46
46
  segments:
47
47
  - 0
48
- hash: 3051429508033972705
48
+ hash: 2467574349782494440
49
49
  required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
@@ -54,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  version: '0'
55
55
  segments:
56
56
  - 0
57
- hash: 3051429508033972705
57
+ hash: 2467574349782494440
58
58
  requirements: []
59
59
  rubyforge_project: files
60
60
  rubygems_version: 1.8.10