Class: FFFS::Directory

Inherits:
Hash
  • Object
show all
Includes:
Node
Defined in:
lib/fffs/directory.rb

Direct Known Subclasses

FileSystem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, files = [], parent = nil, filesystem = nil) ⇒ Directory

Returns a new instance of Directory.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fffs/directory.rb', line 31

def initialize (name, files=[], parent=nil, filesystem=nil) 
  @filesystem = filesystem
  @parent     = parent

  @name = name
  @mode = 0755

  files.each {|file|
    self[file.name] = file
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



65
66
67
# File 'lib/fffs/directory.rb', line 65

def method_missing (id, *args, &block)
  self[id.to_s]
end

Instance Attribute Details

#filesystemObject

Returns the value of attribute filesystem.



27
28
29
# File 'lib/fffs/directory.rb', line 27

def filesystem
  @filesystem
end

#modeObject (readonly)

Returns the value of attribute mode.



29
30
31
# File 'lib/fffs/directory.rb', line 29

def mode
  @mode
end

#nameObject

Returns the value of attribute name.



29
30
31
# File 'lib/fffs/directory.rb', line 29

def name
  @name
end

#parentObject

Returns the value of attribute parent.



27
28
29
# File 'lib/fffs/directory.rb', line 27

def parent
  @parent
end

Instance Method Details

#[](path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fffs/directory.rb', line 69

def [] (path)
  if path.include?('/')
    result = path.start_with?('/') ? filesystem : self
    last   = nil

    path.split(%r{/+}).each {|piece|
      next if piece.empty? || piece == '.'

      if !result.is_a?(Directory)
        raise RuntimeError.new "#{last} isn't a directory"
      end

      if piece == '..'
        result = result.parent
      else
        result = result[piece]
      end

      last = piece
    }

    result
  else
    super(path)
  end
end

#[]=(name, value) ⇒ Object



96
97
98
99
100
101
# File 'lib/fffs/directory.rb', line 96

def []= (name, value)
  value.parent     = self
  value.filesystem = self.filesystem

  super(name, value)
end

#__pathObject



119
# File 'lib/fffs/directory.rb', line 119

alias __path path

#chmod(mode) ⇒ Object



43
44
45
# File 'lib/fffs/directory.rb', line 43

def chmod (mode)
  @mode = mode
end

#include(dir) ⇒ Object



113
114
115
116
117
# File 'lib/fffs/directory.rb', line 113

def include (dir)
  dir.each_value {|file|
    self << file
  }
end

#inspectObject



157
158
159
160
161
162
163
164
165
# File 'lib/fffs/directory.rb', line 157

def inspect
  output = "#{self.path}\n"

  self.each_value {|file|
    output << "#{file.inspect}\n"
  }

  output[-1] = ''; output
end

#load(path) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/fffs/directory.rb', line 136

def load (path)
  require 'find'

  Find.find(path) {|f|
    next unless ::File.file?(f) || ::File.symlink?(f)

    tmp  = ::File.dirname(f[path.length + 1, f.length]) rescue next
    into = self

    tmp.split('/').each {|dir|
      into = into[dir] || (into << Directory.new(dir))
    }

    if ::File.file?(f)
      into << File.new(::File.basename(f), ::File.read(f))
    elsif ::File.symlink?(f)
      into << Link.new(::File.basename(f), ::File.readlink(f))
    end
  }
end

#pathObject



121
122
123
# File 'lib/fffs/directory.rb', line 121

def path
  "#{__path}/".sub(%r{/*/}, '/')
end

#push(file) ⇒ Object Also known as: <<



103
104
105
106
107
108
109
# File 'lib/fffs/directory.rb', line 103

def push (file)
  if self[file.name].is_a?(Directory) && file.is_a?(Directory)
    self[file.name].include(file)
  else
    self[file.name] = file
  end
end

#save(path, mode = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/fffs/directory.rb', line 125

def save (path, mode=nil)
  require 'fileutils'

  FileUtils.mkpath(path)
  ::File.chmod(mode || @mode, path)

  map {|(_, f)|
    f.save("#{path}/#{f.name}", mode)
  }
end

#to_aObject



61
62
63
# File 'lib/fffs/directory.rb', line 61

def to_a
  self.map {|(name, file)| file}
end