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

- (Entry) initialize(name)

A new instance of Entry



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

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

Instance Attribute Details

- (Object) name

Returns the value of attribute name



47
48
49
# File 'lib/dirtravel.rb', line 47

def name
  @name
end

Instance Method Details

- (Object) abspath

Absolute path.



90
91
92
93
94
95
96
# File 'lib/dirtravel.rb', line 90

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

- (String) dir(basedir = self)

Relative path of parenting directory.

Parameters:

  • basedir (Entry) (defaults to: self)

    Starting level for the hierarchy.

Returns:

  • (String)

    Containing directory.



109
110
111
# File 'lib/dirtravel.rb', line 109

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

- (Object) files

Return all file entries in hierarchy.



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

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

- (Array) parts(basedir = self) 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.



59
60
61
62
63
64
65
66
# File 'lib/dirtravel.rb', line 59

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

- (Object) path

Relative path.



72
73
74
# File 'lib/dirtravel.rb', line 72

def path
    parts.join( '/' )
end

- (Boolean) relative?

Relative path Entry.

Returns:

  • (Boolean)


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

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

- (Object) rename(name)

Rename node.

Parameters:

  • name (String)

    If name is abspath then Entry becomes abspath.



144
145
146
147
148
149
150
151
152
153
# File 'lib/dirtravel.rb', line 144

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

- (Array) select_level(level)

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.



119
120
121
# File 'lib/dirtravel.rb', line 119

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

- (Object) stat

File.stat data for the Entry.



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

def stat
    File.stat( path )
end

- (Object) subpath(level = 1)

Relative path under root.

Parameters:

  • level (Integer) (defaults to: 1)

    Level down from root.



80
81
82
83
84
85
86
# File 'lib/dirtravel.rb', line 80

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

- (Object) tip

Top directory name (usually same as name).



100
101
102
# File 'lib/dirtravel.rb', line 100

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