Class: FTPMVC::Directory

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Directory

Returns a new instance of Directory.



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

def initialize(name, &block)
  @name = name.to_s
  @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

Class Method Details

.build(name, &block) ⇒ Object



41
42
43
# File 'lib/ftpmvc/directory.rb', line 41

def self.build(name, &block)
  directory_class(name).new(name, &block)
end

.directory_class(name) ⇒ Object



57
58
59
# File 'lib/ftpmvc/directory.rb', line 57

def self.directory_class(name)
  ActiveSupport::Dependencies.safe_constantize("#{name.to_s.camelize}Directory") || self
end

Instance Method Details

#directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ftpmvc/directory.rb', line 45

def directory?(path)
  resolve(path).kind_of?(Directory)
end

#get(path) ⇒ Object



24
25
26
27
# File 'lib/ftpmvc/directory.rb', line 24

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

#resolve(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ftpmvc/directory.rb', line 29

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

#size(path) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/ftpmvc/directory.rb', line 14

def size(path)
  io = get(path)
  return nil if io.nil?
  total_size = 0
  while buffer = io.read(1024)
    total_size += buffer.size
  end
  total_size
end