Class: DirTravel::Travel
- Inherits:
-
Object
- Object
- DirTravel::Travel
- Defined in:
- lib/dirtravel.rb
Overview
Create directory recursion tree (with Travel.filetree). Optionally filter with suffix and modify tree building with options Hash (see below).
If basedir is absolute path, the root name is the search path. If basedir is relative path, before search the current directory is changed to referenced directory and root name is the current directory, single level.
Parameters:
- sort
-
Sort directory entries (default: false).
- suffix
-
Limit search to files with “suffix” (default: nil).
- files
-
Include files to search (default: true).
- inclusive
-
Basedirs parent becomes the basedir (default: false).
Example:
d1 = DirTravel::Travel.filetree( dir1, { :sort => true, :suffix => '.mp3' } )
Instance Attribute Summary (collapse)
-
- (Object) abspath
Returns the value of attribute abspath.
-
- (Object) basedir
Starting directory for Travel.
-
- (Object) defaults
Default options for Travel.
- - (Object) root
Class Method Summary (collapse)
-
+ (DirEntry) filetree(basedir = '.', options = {})
Create directory recursion tree and return root.
Instance Method Summary (collapse)
-
- (Travel) initialize(basedir, abspath, options = {})
constructor
Create directory recursion object.
-
- (Object) travel(suffix = @defaults[ :suffix ])
Recursively get all files with suffix.
Constructor Details
- (Travel) initialize(basedir, abspath, options = {})
Create directory recursion object. Overlay options on top of defaults. Initialize root DirEntry.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/dirtravel.rb', line 312 def initialize( basedir, abspath, = {} ) @basedir = basedir @abspath = abspath @defaults = { :suffix => nil, :sort => false, :files => true, :inclusive => false, } @defaults.merge!( ) @root = DirEntry.new( basedir, abspath ) end |
Instance Attribute Details
- (Object) abspath
Returns the value of attribute abspath
232 233 234 |
# File 'lib/dirtravel.rb', line 232 def abspath @abspath end |
- (Object) basedir
Starting directory for DirTravel::Travel.
231 232 233 |
# File 'lib/dirtravel.rb', line 231 def basedir @basedir end |
- (Object) defaults
Default options for DirTravel::Travel.
228 229 230 |
# File 'lib/dirtravel.rb', line 228 def defaults @defaults end |
- (Object) root
Root DirEntry of DirTravel::Travel.
225 226 227 |
# File 'lib/dirtravel.rb', line 225 def root @root end |
Class Method Details
+ (DirEntry) filetree(basedir = '.', options = {})
Create directory recursion tree and return root.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/dirtravel.rb', line 240 def Travel.filetree( basedir = '.', = {} ) # Non-nil if directory needs to be changed. pwd = nil if basedir[0] == '/' # Absolue path. t = Travel.new( basedir, basedir, ) t.travel else # Relative path. # Store current directory before chdir. pwd = Dir.pwd # Generate target directory for chdir. One up from # the reference. full = File.absolute_path( basedir ) base = File.basename( full ) dir = File.dirname( full ) # Goto target. Dir.chdir( dir ) t = Travel.new( base, full, ) t.travel end if t.defaults[ :inclusive ] # With inclusive the root is changed to one-up from # target. # One up from root. uppath = File.dirname( t.root.abspath ) if t.root.relative? path = File.basename( uppath ) else path = uppath end # Create the "one-up" root. newRoot = DirEntry.new( path, uppath ) # Rename old root. t.root.rename( t.root.tip ) # Add to one-up root. newRoot.add( t.root ) # Set root to one-up root. t.root = newRoot end # Return back to start directory if dir changed. Dir.chdir( pwd ) if pwd # Return root {DirEntry}. t.root end |
Instance Method Details
- (Object) travel(suffix = @defaults[ :suffix ])
Recursively get all files with suffix. Ignore suffix if suffix is nil.
331 332 333 |
# File 'lib/dirtravel.rb', line 331 def travel( suffix = @defaults[ :suffix ] ) entriesIn( @basedir, @root, suffix ) end |