class DirTravel::Entry
Extend RubyTree base class with file and directory features.
Attributes
Node name.
Public Class Methods
Set name for {Entry} (Dir/File). Initialize abspath.
# File lib/dirtravel.rb, line 55 def initialize( name ) super( name, nil ) @abspath = nil end
Public Instance Methods
Absolute path.
# File lib/dirtravel.rb, line 96 def abspath if @abspath @abspath else root.abspath + '/' + subpath end end
Relative path of parenting directory.
@param basedir [Entry] Starting level for the hierarchy. @return [String] Containing directory.
# File lib/dirtravel.rb, line 115 def dir( basedir = self ) parts( basedir.parent ).join( '/' ) end
Return all file entries in hierarchy.
# File lib/dirtravel.rb, line 131 def files select do |i| i.kind_of?( FileEntry ) end end
Return path components as Array.
@param basedir [Entry] Starting level for the hierarchy. @return [Array] Array of names in hierarchy.
# File lib/dirtravel.rb, line 65 def parts( basedir = self ) parents = [] while basedir parents.push basedir.tip basedir = basedir.parent end parents.reverse end
Relative path.
# File lib/dirtravel.rb, line 78 def path parts.join( '/' ) end
Relative path {Entry}.
# File lib/dirtravel.rb, line 143 def relative? @name[0] != '/' end
Rename node.
@param name [String] If name is abspath then {Entry} becomes
abspath.
# 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 all siblings from given node depth.
@param level [Integer] Selected level in hierachy. Level is
number of steps down in hierarhcy.
@return [Array] Array of siblings in selected hierarchy.
# File lib/dirtravel.rb, line 125 def select_level( level ) select do |i| i.node_depth == level; end end
File.stat data for the {Entry}.
# File lib/dirtravel.rb, line 137 def stat File.stat( path ) end
Relative path under root.
@param level [Integer] Level down from root.
# 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
Top directory name (usually same as name).
# File lib/dirtravel.rb, line 106 def tip @name.split( "/" )[-1] end