Class: FileTree

Inherits:
Object show all
Defined in:
lib/sitefuel/extensions/FileTree.rb

Overview

File

FileTree.rb

Author

wkm

Copyright

2009, Zanoccio LLC.

License

GPL version 2.0 (see LICENSE.rb)

A FileTree is a generator designed to very quickly create large directory and file structures.

Instance Method Summary collapse

Constructor Details

#initialize(base_path = nil) ⇒ FileTree

creates a new FileTree data structure for a given path



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sitefuel/extensions/FileTree.rb', line 14

def initialize(base_path = nil)
  if base_path == nil
    @base_path = Dir.pwd
  else
    @base_path = base_path
  end

  # create the directory if it doesn't exist
  unless File.directory?(base_path)
    Dir.mkdir(base_path)
  end

  # initialize our file tree
  refresh_tree
end

Instance Method Details

#create_directory(name) ⇒ Object

creates the given directory if it doesn’t exist and returns a FileTree for it



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sitefuel/extensions/FileTree.rb', line 56

def create_directory(name)
  return if name == nil
  return if name == '.'
  return if name == '..'

  full_name = File.join(@base_path, name)
  res = @directory_hash[full_name]
  if res != nil
    return res
  else
    Dir.mkdir(full_name)
    @directory_hash[name] = FileTree.new(full_name)
  end
end

#create_file(name) ⇒ Object

creates the file at this level of the file tree if it doesn’t exist



73
74
75
76
77
78
79
80
# File 'lib/sitefuel/extensions/FileTree.rb', line 73

def create_file(name)
  full_name = File.join(@base_path, name)
  if File.exists?(full_name)
    return full_name
  else
    File.new(full_name)
  end         
end

#create_path(name) ⇒ Object

creates a path to the given file but doesn’t create the actual file



84
85
86
87
88
89
90
91
# File 'lib/sitefuel/extensions/FileTree.rb', line 84

def create_path(name)
  components = File.dirname(name).split(File::SEPARATOR)
  
  tld = self
  components.each do |part|
    tld = tld.create_directory(part)
  end
end

#get_file(name) ⇒ Object

finds the file if it exists, otherwise creates all the necessary parent directories for the file and gives back the file name



96
97
98
99
# File 'lib/sitefuel/extensions/FileTree.rb', line 96

def get_file(name)    
  create_path(name)
  File.join(@base_path, name)
end

#has_directory?(name) ⇒ Boolean

gives true if the directory exists at this level of the FileTree

Returns:

  • (Boolean)


49
50
51
# File 'lib/sitefuel/extensions/FileTree.rb', line 49

def has_directory?(name)
  @directory_hash[name] == nil
end

#refresh_treeObject

rebuilds the FileTree given the #base_path



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sitefuel/extensions/FileTree.rb', line 32

def refresh_tree
  @directory_hash = {}
  @file_hash = {}

  top_level_contents = Dir[File.join(@base_path, '*')]

  top_level_contents.each do |handle|
    if File.directory?(handle)
      @directory_hash[handle] = FileTree.new(handle)
    else
      @file_hash[handle] = true
    end
  end
end