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
-
#name ⇒ Object
Node name.
Instance Method Summary collapse
-
#abspath ⇒ Object
Absolute path.
-
#dir(basedir = self) ⇒ String
Relative path of parenting directory.
-
#files ⇒ Object
Return all file entries in hierarchy.
-
#initialize(name) ⇒ Entry
constructor
Set name for Entry (Dir/File).
-
#parts(basedir = self) ⇒ Array
(also: #pathArray)
Return path components as Array.
-
#path ⇒ Object
Relative path.
-
#relative? ⇒ Boolean
Relative path Entry.
-
#rename(name) ⇒ Object
Rename node.
-
#select_level(level) ⇒ Array
Select all siblings from given node depth.
-
#stat ⇒ Object
File.stat data for the Entry.
-
#subpath(level = 1) ⇒ Object
Relative path under root.
-
#tip ⇒ Object
Top directory name (usually same as name).
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
#name ⇒ Object
Node name.
51 52 53 |
# File 'lib/dirtravel.rb', line 51 def name @name end |
Instance Method Details
#abspath ⇒ Object
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.
115 116 117 |
# File 'lib/dirtravel.rb', line 115 def dir( basedir = self ) parts( basedir.parent ).join( '/' ) end |
#files ⇒ Object
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.
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 |
#path ⇒ Object
Relative path.
78 79 80 |
# File 'lib/dirtravel.rb', line 78 def path parts.join( '/' ) end |
#relative? ⇒ Boolean
Relative path DirTravel::Entry.
143 144 145 |
# File 'lib/dirtravel.rb', line 143 def relative? @name[0] != '/' end |
#rename(name) ⇒ Object
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 |
#select_level(level) ⇒ Array
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 |
#stat ⇒ Object
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.
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 |
#tip ⇒ Object
Top directory name (usually same as name).
106 107 108 |
# File 'lib/dirtravel.rb', line 106 def tip @name.split( "/" )[-1] end |