Class: GitXplorer::GitObject::Directory

Inherits:
GitXplorer::GitObject show all
Defined in:
lib/git_xplorer/git_object/directory.rb

Instance Attribute Summary

Attributes inherited from GitXplorer::GitObject

#name, #parent

Instance Method Summary collapse

Methods inherited from GitXplorer::GitObject

#exist?, #get, #get_completions, #has_child?

Constructor Details

#initialize(name, parent) ⇒ Directory

Returns a new instance of Directory.



53
54
55
56
57
# File 'lib/git_xplorer/git_object/directory.rb', line 53

def initialize(name, parent)
    super(name.gsub(%r{/+$}, ""), parent)
    @directories = Hash.new
    @files = Hash.new
end

Instance Method Details

#absolute_pathObject



2
3
4
5
# File 'lib/git_xplorer/git_object/directory.rb', line 2

def absolute_path
    return "" if (@parent.nil?)
    return "#{super}/"
end

#add(path) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/git_xplorer/git_object/directory.rb', line 7

def add(path)
    dir, _, file = path.partition("/")
    if (file.nil? || file.empty?)
        # Add file
        file = GitXplorer::GitObject::File.new(dir, self)
        @files[file.name] = file
    else
        # Add subdirectory
        if (@directories[dir].nil?)
            directory = GitXplorer::GitObject::Directory.new(
                dir,
                self
            )
            @directories[dir] = directory
        end
        @directories[dir].add(file)
    end
end

#childrenObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/git_xplorer/git_object/directory.rb', line 26

def children
    if (@kids.nil?)
        # Initialize
        @kids = Array.new

        # Get child directories first
        @kids.concat(
            @directories.values.sort do |a, b|
                a.name.downcase <=> b.name.downcase
            end
        )

        # Get child files
        @kids.concat(
            @files.values.sort do |a, b|
                a.name.downcase <=> b.name.downcase
            end
        )
    end

    return @kids
end

#descObject



49
50
51
# File 'lib/git_xplorer/git_object/directory.rb', line 49

def desc
    return "Directory"
end

#tab_complete(color = false) ⇒ Object



59
60
61
62
# File 'lib/git_xplorer/git_object/directory.rb', line 59

def tab_complete(color = false)
    color ||= false
    return {"#{@name}/" => desc}
end

#to_sObject



64
65
66
# File 'lib/git_xplorer/git_object/directory.rb', line 64

def to_s
    return "#{@name}/"
end