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 an absolute path, the root name is the search path. If basedir is a relative path, the current directory is changed to referenced directory before search 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
-
#abspath ⇒ Object
Returns the value of attribute abspath.
-
#basedir ⇒ Object
Starting directory for Travel.
-
#defaults ⇒ Object
Default options for Travel.
- #root ⇒ Object
Class Method Summary collapse
-
.filetree(basedir = '.', options = {}) ⇒ DirEntry
Create directory recursion tree and return root.
Instance Method Summary collapse
-
#initialize(basedir, abspath, options = {}) ⇒ Travel
constructor
Create directory recursion object.
-
#travel(suffix = ) ⇒ Object
Recursively get all files with suffix.
Constructor Details
#initialize(basedir, abspath, options = {}) ⇒ Travel
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
#abspath ⇒ Object
Returns the value of attribute abspath.
232 233 234 |
# File 'lib/dirtravel.rb', line 232 def abspath @abspath end |
#basedir ⇒ Object
Starting directory for DirTravel::Travel.
231 232 233 |
# File 'lib/dirtravel.rb', line 231 def basedir @basedir end |
#defaults ⇒ Object
Default options for DirTravel::Travel.
228 229 230 |
# File 'lib/dirtravel.rb', line 228 def defaults @defaults end |
#root ⇒ Object
Root DirEntry of DirTravel::Travel.
225 226 227 |
# File 'lib/dirtravel.rb', line 225 def root @root end |
Class Method Details
.filetree(basedir = '.', options = {}) ⇒ DirEntry
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
#travel(suffix = ) ⇒ Object
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 |