Class: TTY::Tree::PathWalker
- Inherits:
-
Object
- Object
- TTY::Tree::PathWalker
- Defined in:
- lib/tty/tree/path_walker.rb
Overview
Walk and collect nodes from directory.
Instance Attribute Summary collapse
- #dirs_count ⇒ Object readonly
- #files_count ⇒ Object readonly
- #nodes ⇒ Object readonly
Instance Method Summary collapse
- #add_filter(filter) ⇒ Object
-
#initialize(options = {}) ⇒ PathWalker
constructor
Create a PathWalker.
-
#traverse(path) ⇒ Object
Traverse given path recursively.
Constructor Details
#initialize(options = {}) ⇒ PathWalker
Create a PathWalker
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tty/tree/path_walker.rb', line 22 def initialize( = {}) @files_count = 0 @dirs_count = 0 @nodes = [] @filters = [] @level = .fetch(:level) { -1 } @file_limit = .fetch(:file_limit) { - 1 } unless [:show_hidden] add_filter(-> (p) { !p.basename.to_s.start_with?('.') }) end if [:only_dirs] add_filter(-> (p) { p.directory? }) end end |
Instance Attribute Details
#dirs_count ⇒ Object (readonly)
17 18 19 |
# File 'lib/tty/tree/path_walker.rb', line 17 def dirs_count @dirs_count end |
#files_count ⇒ Object (readonly)
15 16 17 |
# File 'lib/tty/tree/path_walker.rb', line 15 def files_count @files_count end |
#nodes ⇒ Object (readonly)
13 14 15 |
# File 'lib/tty/tree/path_walker.rb', line 13 def nodes @nodes end |
Instance Method Details
#add_filter(filter) ⇒ Object
39 40 41 |
# File 'lib/tty/tree/path_walker.rb', line 39 def add_filter(filter) @filters << filter end |
#traverse(path) ⇒ Object
Traverse given path recursively
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tty/tree/path_walker.rb', line 49 def traverse(path) root_path = Pathname.new(path) empty_path = Pathname.new('') unless root_path.directory? raise ArgumentError, "#{root_path} is not a directory path" end @nodes << Node.new(root_path, empty_path, '', 0) walk(root_path, root_path.children, '', 1) end |