Class: Files::Files
- Inherits:
-
Object
- Object
- Files::Files
- Defined in:
- lib/files.rb
Overview
concrete class for creating files and dirs under a temporary directory
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#dir(name, options = {}, &block) ⇒ Object
only 1 option supported: ‘src’.
- #file(name, contents = "contents of #{name}") ⇒ Object
-
#initialize(path, block, options) ⇒ Files
constructor
A new instance of Files.
- #remove ⇒ Object
Constructor Details
#initialize(path, block, options) ⇒ Files
Returns a new instance of Files.
55 56 57 58 59 60 |
# File 'lib/files.rb', line 55 def initialize path, block, @root = path @dirs = [] dir nil, &block at_exit {remove} if [:remove] end |
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
53 54 55 |
# File 'lib/files.rb', line 53 def root @root end |
Instance Method Details
#dir(name, options = {}, &block) ⇒ Object
only 1 option supported: ‘src’. if specified, is copied into ‘name’
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/files.rb', line 63 def dir name, ={}, &block if name.nil? path = current else path = File.join(current, name) end FileUtils.mkdir_p path @dirs << name if name if [:src] # copy over remote dir to this one FileUtils.cp_r([:src], path) end Dir.chdir(path) do instance_eval &block if block end @dirs.pop path end |
#file(name, contents = "contents of #{name}") ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/files.rb', line 84 def file name, contents = "contents of #{name}" if name.is_a? File FileUtils.cp name.path, current # todo: return path else path = File.join(current, name) if contents.is_a? File FileUtils.cp contents.path, path else file_path = File.open(path, "w") do |f| f.write contents end end path end end |
#remove ⇒ Object
101 102 103 |
# File 'lib/files.rb', line 101 def remove FileUtils.rm_rf(@root) if File.exists?(@root) end |