Class: DirTravel::Entry
- Inherits:
-
Tree::TreeNode
- Object
- Tree::TreeNode
- DirTravel::Entry
- Defined in:
- lib/dirtravel.rb
Overview
Extend RubyTree base class with file and directory features.
Instance Attribute Summary (collapse)
-
- (Object) name
Node name.
Instance Method Summary (collapse)
-
- (Object) abspath
Absolute path.
-
- (String) dir(basedir = self)
Relative path of parenting directory.
-
- (Object) files
Return all file entries in hierarchy.
-
- (Entry) initialize(name)
constructor
Set name for Entry (Dir/File).
-
- (Array) parts(basedir = self)
(also: #pathArray)
Return path components as Array.
-
- (Object) path
Relative path.
-
- (Boolean) relative?
Relative path Entry.
-
- (Object) rename(name)
Rename node.
-
- (Array) select_level(level)
Select all siblings from given node depth.
-
- (Object) stat
File.stat data for the Entry.
-
- (Object) subpath(level = 1)
Relative path under root.
-
- (Object) tip
Top directory name (usually same as name).
Constructor Details
- (Entry) initialize(name)
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
- (Object) name
Node name.
51 52 53 |
# File 'lib/dirtravel.rb', line 51 def name @name end |
Instance Method Details
- (Object) abspath
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 |
- (String) dir(basedir = self)
Relative path of parenting directory.
115 116 117 |
# File 'lib/dirtravel.rb', line 115 def dir( basedir = self ) parts( basedir.parent ).join( '/' ) end |
- (Object) files
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 |
- (Array) parts(basedir = self) Also known as: pathArray
Return path components as Array.
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 |
- (Object) path
Relative path.
78 79 80 |
# File 'lib/dirtravel.rb', line 78 def path parts.join( '/' ) end |
- (Boolean) relative?
Relative path DirTravel::Entry.
143 144 145 |
# File 'lib/dirtravel.rb', line 143 def relative? @name[0] != '/' end |
- (Object) rename(name)
Rename node.
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 |
- (Array) select_level(level)
Select all siblings from given node depth.
125 126 127 |
# File 'lib/dirtravel.rb', line 125 def select_level( level ) select do |i| i.node_depth == level; end end |
- (Object) stat
File.stat data for the DirTravel::Entry.
137 138 139 |
# File 'lib/dirtravel.rb', line 137 def stat File.stat( path ) end |
- (Object) subpath(level = 1)
Relative path under 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 |
- (Object) tip
Top directory name (usually same as name).
106 107 108 |
# File 'lib/dirtravel.rb', line 106 def tip @name.split( "/" )[-1] end |