Class: FFFS::FileSystem

Inherits:
Directory show all
Defined in:
lib/fffs/filesystem.rb

Instance Attribute Summary

Attributes inherited from Directory

#filesystem, #mode, #name, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Directory

#[], #[]=, #__path, #chmod, #include, #inspect, #load, #method_missing, #push, #save, #to_a

Constructor Details

#initializeFileSystem

Returns a new instance of FileSystem.



43
44
45
46
47
48
# File 'lib/fffs/filesystem.rb', line 43

def initialize
  @name = '/'

  self.parent     = self
  self.filesystem = self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FFFS::Directory

Class Method Details

.load(path) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/fffs/filesystem.rb', line 35

def self.load (path)
  fs = self.new

  fs.load(path)

  return fs
end

.parse(text) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/fffs/filesystem.rb', line 27

def self.parse (text)
  fs = self.new

  fs.parse(text)

  return fs
end

Instance Method Details

#parse(text) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
95
96
97
98
99
100
101
102
103
# File 'lib/fffs/filesystem.rb', line 54

def parse (text)
  if text.is_a?(IO)
    text = text.read
  end

  separator = Regexp.escape(text.match(/^(.+)$/)[1])
  text      = text.sub(/^.*$/, '').gsub(/\r/, '')

  data = text.split(/\n\n#{separator} (.+?) #{separator}\n\n/)
  data.shift

  data.each_slice(2) {|(name, content)|
    if matches = name.match(/^(.+?) -> (.+?)$/)
      whole, name, link = matches.to_a
    elsif matches = name.match(/(.+?)\s+\((.*?)\)$/)
      whole, name, mime = matches.to_a
    end

    path = ::File.dirname(name)
    name = ::File.basename(name)

    if mime
      require 'base64'

      content = Base64.decode64(content)
    end

    into = self

    path.split('/').each {|dir|
      if dir == '.'
        next
      elsif dir == '..'
        into = into.parent
      else
        into = into[dir] || (into << Directory.new(dir))
      end
    }

    if link
      into << Link.new(name, link)
    else
      into << File.new(name, content)

      if mime
        into[name].mime = mime
      end
    end
  }
end

#path(path = nil) ⇒ Object



50
51
52
# File 'lib/fffs/filesystem.rb', line 50

def path (path=nil)
  '/'
end