Class: Slick::Web::Dir

Inherits:
Node show all
Defined in:
lib/slick/web/dir.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#name, #parent

Instance Method Summary collapse

Methods inherited from Node

#dir?, #file?, #relative_path, #root

Constructor Details

#initialize(*args) ⇒ Dir

Returns a new instance of Dir.



8
9
10
11
# File 'lib/slick/web/dir.rb', line 8

def initialize(*args)
    super
    @children = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



55
56
57
58
59
60
61
# File 'lib/slick/web/dir.rb', line 55

def method_missing(name, *args, &block)
    if args.length == 0
        self[name]
    else
        super
    end
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/slick/web/dir.rb', line 6

def children
  @children
end

Instance Method Details

#[](path) ⇒ Object



38
39
40
# File 'lib/slick/web/dir.rb', line 38

def [](path)
    _index[_normalize_path(path)]
end

#find_file(path) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/slick/web/dir.rb', line 27

def find_file(path)
    if self[path]
        if self[path].dir?
            file = self["#{path}/index"]
            file if file && file.file?
        else
            self[path]
        end
    end
end

#inspectObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/slick/web/dir.rb', line 42

def inspect
    out = []
    out << ""
    children.each do |name, node|
        out << " * #{name}" if node.dir?
    end
    children.each do |name, node|
        out << " * #{name}" if node.file?
    end
    out << ""
    out.join("\n")
end

#merge_dir(path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/slick/web/dir.rb', line 13

def merge_dir(path)
    Dir.open(path).each do |name|
        next if name.match(/\A\.{1,2}\z/)
        current_path = "#{path}/#{name}"
        if File.file?(current_path)
            children[name] ||= Slick::Web::File.new(self, name)
            children[name].paths << current_path
        else
            children[name] ||= Slick::Web::Dir.new(self, name)
            children[name].merge_dir(current_path)
        end
    end
end