Class: DirTravel::Entry

Inherits:
Tree::TreeNode
  • Object
show all
Defined in:
lib/dirtravel.rb

Overview

Extend RubyTree base class with file and directory features.

Direct Known Subclasses

DirEntry, FileEntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Entry

Set name for DirTravel::Entry (Dir/File). Initialize abspath.



55
56
57
58
# File 'lib/dirtravel.rb', line 55

def initialize( name )
    super( name, nil )
    @abspath = nil
end

Instance Attribute Details

#nameObject

Node name.



51
52
53
# File 'lib/dirtravel.rb', line 51

def name
  @name
end

Instance Method Details

#abspathObject

Absolute path.



96
97
98
99
100
101
102
# File 'lib/dirtravel.rb', line 96

def abspath
    if @abspath
        @abspath
    else
        root.abspath + '/' + subpath
    end
end

#dir(basedir = self) ⇒ String

Relative path of parenting directory.

Parameters:

  • basedir (Entry) (defaults to: self)

    Starting level for the hierarchy.

Returns:

  • (String)

    Containing directory.



115
116
117
# File 'lib/dirtravel.rb', line 115

def dir( basedir = self )
    parts( basedir.parent ).join( '/' )
end

#filesObject

Return all file entries in hierarchy.



131
132
133
# File 'lib/dirtravel.rb', line 131

def files
    select do |i| i.kind_of?( FileEntry ) end
end

#parts(basedir = self) ⇒ Array Also known as: pathArray

Return path components as Array.

Parameters:

  • basedir (Entry) (defaults to: self)

    Starting level for the hierarchy.

Returns:

  • (Array)

    Array of names in hierarchy.



65
66
67
68
69
70
71
72
# File 'lib/dirtravel.rb', line 65

def parts( basedir = self )
    parents = []
    while basedir
        parents.push basedir.tip
        basedir = basedir.parent
    end
    parents.reverse
end

#pathObject

Relative path.



78
79
80
# File 'lib/dirtravel.rb', line 78

def path
    parts.join( '/' )
end

#relative?Boolean

Relative path DirTravel::Entry.

Returns:

  • (Boolean)


143
144
145
# File 'lib/dirtravel.rb', line 143

def relative?
    @name[0] != '/'
end

#rename(name) ⇒ Object

Rename node.

Parameters:



152
153
154
155
156
157
158
159
160
161
# File 'lib/dirtravel.rb', line 152

def rename( name )
    @name = name

    # Absolute or relative path?
    if name[0] == "/"
        @abspath = name
    else
        @abspath = nil
    end
end

#select_level(level) ⇒ Array

Select all siblings from given node depth.

Parameters:

  • level (Integer)

    Selected level in hierachy. Level is number of steps down in hierarhcy.

Returns:

  • (Array)

    Array of siblings in selected hierarchy.



125
126
127
# File 'lib/dirtravel.rb', line 125

def select_level( level )
    select do |i| i.node_depth == level; end
end

#statObject

File.stat data for the DirTravel::Entry.



137
138
139
# File 'lib/dirtravel.rb', line 137

def stat
    File.stat( path )
end

#subpath(level = 1) ⇒ Object

Relative path under root.

Parameters:

  • level (Integer) (defaults to: 1)

    Level down from root.



86
87
88
89
90
91
92
# File 'lib/dirtravel.rb', line 86

def subpath( level = 1 )
    pa = parts
    if level < 0 || level > pa.length
        raise DirTravelError, "Invalid index for subpath level!"
    end
    pa[ level .. -1 ].join( '/' )
end

#tipObject

Top directory name (usually same as name).



106
107
108
# File 'lib/dirtravel.rb', line 106

def tip
    @name.split( "/" )[-1]
end