class Directree::DirTree
Attributes
children[R]
opts[R]
path[R]
Public Class Methods
new(name, opts={})
click to toggle source
# File lib/directree.rb, line 20 def initialize name, opts={}, &block @path = name @opts = opts @children ||= [] self.instance_eval &block if block_given? end
Public Instance Methods
[](index_or_name)
click to toggle source
# File lib/directree.rb, line 40 def [](index_or_name) if index_or_name.is_a? Integer @children[index_or_name] else @children.find {|p| File.basename(p.path) == index_or_name} end end
create()
click to toggle source
# File lib/directree.rb, line 35 def create FileUtils::mkdir_p @path @children.each{ |child| child.create} end
dir(name, opts={})
click to toggle source
# File lib/directree.rb, line 27 def dir name, opts={}, &block @children << DirTree.new(join_path(@path, name), opts, &block) end
file(name, opts={})
click to toggle source
# File lib/directree.rb, line 31 def file name, opts={}, &block @children << WritableFile.new(join_path(@path, name), opts, &block) end
walk(&block)
click to toggle source
# File lib/directree.rb, line 48 def walk &block [block.call(self), children.collect {|child| if child.respond_to?(:walk) child.walk(&block) else block.call(child) end }].flatten end
Private Instance Methods
join_path(parent, child)
click to toggle source
# File lib/directree.rb, line 59 def join_path parent, child (Pathname(parent) + child).to_s end