Class: FTPMVC::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/ftpmvc/directory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Directory

Returns a new instance of Directory.



8
9
10
11
12
# File 'lib/ftpmvc/directory.rb', line 8

def initialize(options={}, &block)
  @name = options[:name]
  @index = []
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



6
7
8
# File 'lib/ftpmvc/directory.rb', line 6

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/ftpmvc/directory.rb', line 6

def name
  @name
end

Instance Method Details

#get(path) ⇒ Object



14
15
16
17
# File 'lib/ftpmvc/directory.rb', line 14

def get(path)
  file = resolve(path)
  file ? file.data : nil
end

#resolve(path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ftpmvc/directory.rb', line 19

def resolve(path)
  path = path.gsub(%r{^/}, '')
  return self if path.empty?
  name, subpath = path.split('/', 2)
  child = index.find { |node| node.name == name }
  if subpath == nil or child == nil
    child
  else
    child.resolve(subpath)
  end
end