Module: FileTree

Extended by:
FileTree
Included in:
FileTree
Defined in:
lib/file_tree.rb,
lib/file_tree/version.rb,
lib/file_tree/defaults.rb,
lib/file_tree/content_generator.rb

Defined Under Namespace

Modules: ContentGenerator, Defaults

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dir_stackObject



17
18
19
# File 'lib/file_tree.rb', line 17

def self.dir_stack
  Thread.current[:file_tree_dir_stack] ||= []
end

.with_context_dir(dir, &block) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/file_tree.rb', line 9

def self.with_context_dir(dir, &block)
  dir_stack.push(dir)
  yield dir if block_given?
  dir
ensure
  dir_stack.pop
end

Instance Method Details

#dir(name, &block) ⇒ Object



26
27
28
29
30
# File 'lib/file_tree.rb', line 26

def dir(name, &block)
  dir = File.join(FileTree.dir_stack[-1], name)
  Dir.mkdir(dir)
  FileTree.with_context_dir(dir, &block)
end

#file(name, content = Defaults.content_type) ⇒ Object



32
33
34
35
36
37
# File 'lib/file_tree.rb', line 32

def file(name, content = Defaults.content_type)
  content = ContentGenerator.create(content) unless content.is_a?(String)
  path = File.join(FileTree.dir_stack[-1], name)
  File.open(path, "wb"){|stream| stream << content}
  path
end

#root_dir(&block) ⇒ Object



21
22
23
24
# File 'lib/file_tree.rb', line 21

def root_dir(&block)
  dir = Dir.mktmpdir
  FileTree.with_context_dir(dir, &block)
end