Class: GitXplorer::GitObject

Inherits:
Object
  • Object
show all
Defined in:
lib/git_xplorer/git_object.rb

Direct Known Subclasses

Directory, File, Revision

Defined Under Namespace

Classes: Directory, File, Revision

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent) ⇒ GitObject

Returns a new instance of GitObject.



94
95
96
97
98
# File 'lib/git_xplorer/git_object.rb', line 94

def initialize(name, parent)
    @kids = nil
    @name = name
    @parent = parent
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#absolute_pathObject



5
6
7
8
# File 'lib/git_xplorer/git_object.rb', line 5

def absolute_path
    return "" if (@parent.nil?)
    return [@parent.absolute_path, @name].join
end

#childrenObject



10
11
12
13
# File 'lib/git_xplorer/git_object.rb', line 10

def children
    @kids ||= Array.new
    return @kids
end

#descObject



15
16
17
# File 'lib/git_xplorer/git_object.rb', line 15

def desc
    return ""
end

#exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/git_xplorer/git_object.rb', line 19

def exist?(path)
    return !get(path).nil?
end

#get(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/git_xplorer/git_object.rb', line 23

def get(path)
    # Return self if path is nil or empty
    return self if (path.nil? || path.empty?)

    # Split path on /
    child, recurse, grandchild = path.partition("/")
    child, _, grandchild = path.partition(":") if (recurse.empty?)

    case child
    when "."
        # Same directory
        return self.get(grandchild)
    when ".."
        # Check parent if it exists
        return @parent.get(grandchild) if (@parent)

        # Otherwise we reached top-level so just keep going
        return self.get(grandchild)
    end

    # Check child if it exists
    if (has_child?(child))
        kids = children.select do |kid|
            kid.name == child
        end
        return kids[0].get(grandchild) if (!kids.empty?)
    end

    # Not found
    return nil
end

#get_completions(path) ⇒ Object



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
# File 'lib/git_xplorer/git_object.rb', line 55

def get_completions(path)
    # Return all children if path is nil or empty
    return children if (path.nil? || path.empty?)

    # Split path on / or :
    child, recurse, grandchild = path.partition("/")
    if (recurse.empty?)
        child, recurse, grandchild = path.partition(":")
    end

    case child
    when "."
        # Same directory
        return get_completions(grandchild)
    when ".."
        # Check parent if it exists
        return @parent.get_completions(grandchild) if (@parent)

        # Otherwise we reached top-level so just keep going
        return get_completions(grandchild)
    end

    # Check child if it exists
    if (!recurse.empty? && has_child?(child))
        return get(child).get_completions(grandchild)
    end

    # Return any children that may match
    return children.select do |kid|
        kid.name.downcase.start_with?(child.downcase)
    end
end

#has_child?(name) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/git_xplorer/git_object.rb', line 88

def has_child?(name)
    return children.any? do |child|
        child.name == name
    end
end

#tab_complete(color = false) ⇒ Object



100
101
102
103
# File 'lib/git_xplorer/git_object.rb', line 100

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

#to_sObject



105
106
107
# File 'lib/git_xplorer/git_object.rb', line 105

def to_s
    return @name
end