You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.markdown
+48-43
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ USAGE
33
33
34
34
To use Construct, you need to include the Construct module in your class like so:
35
35
36
-
include Construct
36
+
include Construct::Helpers
37
37
38
38
Using construct is as simple as calling `within_construct` and providing a block. All files and directories that are created within that block are created within a temporary directory. The temporary directory is always deleted before `within_construct` finishes.
39
39
@@ -44,76 +44,81 @@ Creating files
44
44
45
45
The most basic use of Construct is creating an empty file with the:
46
46
47
-
within_construct do |construct|
48
-
construct.file('foo.txt')
49
-
end
47
+
within_construct do |construct|
48
+
construct.file('foo.txt')
49
+
end
50
50
51
51
Note that the working directory is, by default, automatically change to the temporary directory created by Construct, so the following assertion will pass:
52
52
53
-
within_construct do |construct|
54
-
construct.file('foo.txt')
55
-
assert File.exist?('foo.txt')
56
-
end
53
+
within_construct do |construct|
54
+
construct.file('foo.txt')
55
+
assert File.exist?('foo.txt')
56
+
end
57
57
58
58
You can also provide content for the file, either with an optional argument or using the return value of a supplied block:
59
59
60
-
within_construct do |construct|
61
-
construct.file('foo.txt','Here is some content')
62
-
construct.file('bar.txt') do
63
-
<<-EOS
64
-
The block will return this string, which will be used as the content.
65
-
EOS
66
-
end
67
-
end
60
+
within_construct do |construct|
61
+
construct.file('foo.txt','Here is some content')
62
+
construct.file('bar.txt') do
63
+
<<-EOS
64
+
The block will return this string, which will be used as the content.
65
+
EOS
66
+
end
67
+
end
68
68
69
69
If you provide block that accepts a parameter, construct will pass you the IO object. In this case, you are responsible for writing content to the file yourself - the return value of the block will not be used:
70
70
71
-
within_construct do |construct|
72
-
construct.file('foo.txt') do |file|
73
-
file << "Some content\n"
74
-
file << "Some more content"
75
-
end
76
-
end
71
+
within_construct do |construct|
72
+
construct.file('foo.txt') do |file|
73
+
file << "Some content\n"
74
+
file << "Some more content"
75
+
end
76
+
end
77
77
78
78
Finally, you can provide the entire path to a file and the parent directories will be created automatically:
79
79
80
-
within_construct do |construct|
81
-
construct.file('foo/bar/baz.txt')
82
-
end
80
+
within_construct do |construct|
81
+
construct.file('foo/bar/baz.txt')
82
+
end
83
83
84
84
Creating directories
85
85
--------------
86
86
87
87
It is easy to create a directory:
88
88
89
-
within_construct do |construct|
90
-
construct.directory('foo')
91
-
end
89
+
within_construct do |construct|
90
+
construct.directory('foo')
91
+
end
92
+
93
+
You can also provide a block. The object passed to the block can be used to create nested files and directories (it's just a [Pathname](http://www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.html) instance with some extra functionality, so you can use it to get the path of the current directory).
92
94
93
-
You can also provide a block. The object passed to the block can be used to create nested files and directories. Again, note that the working directory is automatically changed while in the block:
95
+
Again, note that the working directory is automatically changed while in the block:
94
96
95
-
within_construct do |construct|
96
-
construct.directory('foo') do |dir|
97
-
dir.file('bar.txt')
98
-
assert File.exist?('bar.txt') # This assertion will pass
99
-
end
100
-
end
97
+
within_construct do |construct|
98
+
construct.directory('foo') do |dir|
99
+
dir.file('bar.txt')
100
+
assert File.exist?('bar.txt') # This assertion will pass
101
+
end
102
+
end
101
103
102
104
Again, you can provide paths and the necessary directories will be automatically created:
103
105
104
-
within_construct do |construct|
105
-
construct.directory('foo/bar/') do |dir|
106
-
dir.directory('baz')
107
-
dir.directory('bazz')
108
-
end
109
-
end
106
+
within_construct do |construct|
107
+
construct.directory('foo/bar/') do |dir|
108
+
dir.directory('baz')
109
+
dir.directory('bazz')
110
+
end
111
+
end
110
112
113
+
Please read test/construct_test.rb for more examples.
0 commit comments