Class: TreeRb::DirTreeWalker
- Inherits:
-
Object
- Object
- TreeRb::DirTreeWalker
- Defined in:
- lib/tree_rb/input_plugins/file_system/directory_walker.rb
Overview
Visit a file system directory
Instance Attribute Summary collapse
-
#visit_file ⇒ Object
it is true to visit file.
Instance Method Summary collapse
-
#ignore(pattern) ⇒ Object
Ignore a node (leaf/Tree) matching pattern.
-
#ignore_dir(pattern) ⇒ Object
Ignore a directory (Tree) matching pattern.
-
#ignore_dir?(dirname) ⇒ boolean
Test directory ignore pattern.
-
#ignore_file(pattern) ⇒ Object
Ignore a file (Leaf) matching pattern.
-
#ignore_file?(filename) ⇒ boolean
Test file ignore pattern.
-
#initialize(dirname = nil, options = nil) {|a, b, c| ... } ⇒ DirTreeWalker
constructor
Build a tree walker.
-
#match(pattern) ⇒ Object
Just the opposite of ignore directory/file matching pattern will be visited.
-
#match?(filename) ⇒ boolean
Test match pattern.
-
#run(dirname = nil, tree_node_visitor = nil, &block) ⇒ TreeNodeVisitor
Run the visitor through the directory tree.
Constructor Details
#initialize(dirname) ⇒ DirTreeWalker #initialize(dirname, options) ⇒ DirTreeWalker
Build a tree walker. Visit a director starting from dirname if dirname is missing, must be supplied when invoking run
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 33 def initialize(dirname = nil, = nil) # # arg detection # if dirname and dirname.respond_to?(:key?) = dirname dirname = nil end if dirname @dirname = dirname unless File.directory?(dirname) raise ArgumentError.new "#{dirname} is not a directory!" end end @visitor = nil # # pattern # @ignore_dir_patterns = [] @ignore_file_patterns = [] @match_file_patterns = [] if and [:ignore] unless [:ignore].respond_to?(:at) [:ignore] = [[:ignore]] end [:ignore].each { |p| ignore(p) } end if and [:ignore_dir] unless [:ignore_dir].respond_to?(:at) [:ignore_dir] = [[:ignore_dir]] end [:ignore_dir].each { |p| ignore_dir(p) } end if and [:ignore_file] unless [:ignore_file].respond_to?(:at) [:ignore_file] = [[:ignore_file]] end [:ignore_file].each { |p| ignore_file(p) } end if and [:match] unless [:match].respond_to?(:at) [:match] = [[:match]] end [:match].each { |p| match(p) } end # # options # @visit_file = true @max_level = nil if and [:max_level] @max_level = [:max_level] end end |
Instance Attribute Details
#visit_file ⇒ Object
it is true to visit file
154 155 156 |
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 154 def visit_file @visit_file end |
Instance Method Details
#ignore(pattern) ⇒ Object
Ignore a node (leaf/Tree) matching pattern
104 105 106 107 108 |
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 104 def ignore(pattern) @ignore_dir_patterns << pattern @ignore_file_patterns << pattern self end |
#ignore_dir(pattern) ⇒ Object
Ignore a directory (Tree) matching pattern
114 115 116 117 118 119 120 121 122 |
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 114 def ignore_dir(pattern) # it transforms all in regexp if pattern.kind_of? Regexp @ignore_dir_patterns << pattern else @ignore_dir_patterns << /#{pattern}/ end self end |
#ignore_dir?(dirname) ⇒ boolean
Test directory ignore pattern
164 165 166 |
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 164 def ignore_dir?(dirname) _include?(@ignore_dir_patterns, File.basename(dirname)) != nil end |
#ignore_file(pattern) ⇒ Object
Ignore a file (Leaf) matching pattern
128 129 130 131 |
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 128 def ignore_file(pattern) @ignore_file_patterns << pattern self end |
#ignore_file?(filename) ⇒ boolean
Test file ignore pattern
174 175 176 |
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 174 def ignore_file?(filename) _include?(@ignore_file_patterns, File.basename(filename)) != nil end |
#match(pattern) ⇒ Object
Just the opposite of ignore directory/file matching pattern will be visited
139 140 141 142 143 144 145 146 |
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 139 def match(pattern) if pattern.kind_of? Regexp @match_file_patterns << pattern else @match_file_patterns << /#{pattern}/ end self end |
#match?(filename) ⇒ boolean
Test match pattern
184 185 186 187 |
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 184 def match?(filename) return true if @match_file_patterns.empty? _include?(@match_file_patterns, File.basename(filename)) != nil end |
#run ⇒ TreeNodeVisitor #run(dirname) { ... } ⇒ TreeNodeVisitor #run(tree_node_visitor) { ... } ⇒ TreeNodeVisitor #run(dirname, tree_node_visitor) { ... } ⇒ TreeNodeVisitor
Run the visitor through the directory tree
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/tree_rb/input_plugins/file_system/directory_walker.rb', line 215 def run(dirname = nil, tree_node_visitor = nil, &block) # # args detection # if dirname and dirname.respond_to?(:enter_node) tree_node_visitor = dirname dirname = nil end # # check dirname # if @dirname.nil? and dirname.nil? raise 'missing starting directory' end @dirname = dirname if dirname # # check visitor # if tree_node_visitor and block raise 'cannot use block and parameter together' end if tree_node_visitor @visitor = tree_node_visitor end if block @visitor = TreeNodeVisitor.new(&block) end unless @visitor raise 'missing visitor' end # # finally starts to process # process_directory(File.(@dirname)) @visitor end |