Class: File::Visitor
- Inherits:
-
Object
- Object
- File::Visitor
- Defined in:
- lib/file/visitor.rb,
lib/file/visitor/filter.rb,
lib/file/visitor/version.rb,
lib/file/visitor/time_utils.rb
Defined Under Namespace
Modules: TimeUtils Classes: Filter, FilterDispatcher
Constant Summary collapse
- FILTER_NS_BASE =
File::Visitor::Filter
- DIRECTION =
Set.new [:asc, :desc]
- VERSION =
"0.2.0"
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#visit_dot_dir ⇒ Object
Returns the value of attribute visit_dot_dir.
Instance Method Summary collapse
-
#add_filter(*args, &block) ⇒ Object
3 ways to register filter.
- #dir_list(dir) ⇒ Object
- #file_list(dir) ⇒ Object
-
#initialize ⇒ Visitor
constructor
A new instance of Visitor.
- #set_direction(dir) ⇒ Object
- #target?(path) ⇒ Boolean
- #visit(dir, &handler) ⇒ Object
- #visit_dir(dir, &handler) ⇒ Object
Constructor Details
#initialize ⇒ Visitor
Returns a new instance of Visitor.
18 19 20 21 22 23 |
# File 'lib/file/visitor.rb', line 18 def initialize @filters = [] @visit_dot_dir = false @direction = :asc end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
11 12 13 |
# File 'lib/file/visitor.rb', line 11 def direction @direction end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
11 12 13 |
# File 'lib/file/visitor.rb', line 11 def filters @filters end |
#visit_dot_dir ⇒ Object
Returns the value of attribute visit_dot_dir.
12 13 14 |
# File 'lib/file/visitor.rb', line 12 def visit_dot_dir @visit_dot_dir end |
Instance Method Details
#add_filter(*args, &block) ⇒ Object
3 ways to register filter
-
built-in filter filter.add_filter(:mtime, :passed, 30, :days)
-
custom filter filter.add_filter(my_filter) (my_filter must implements match?(path) method)
-
block filter filter.add_filter do |path|
# filter operationsend
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/file/visitor.rb', line 70 def add_filter(*args, &block) # 3. block filter if block_given? filter = File::Visitor::Filter::Proc.new(block) @filters.push(filter) return true end # 2. custom filter if (1 == args.size) custom_filter = args.shift unless (custom_filter.respond_to?(:match?)) raise ArgumentError, "custom_filter must implement match?()" end @filters.push(custom_filter) return true end # 1. built-in filter filter_class = File::Visitor::FilterDispatcher.dispatch(args.shift) @filters.push(filter_class.new(*args)) true end |
#dir_list(dir) ⇒ Object
50 51 52 53 54 |
# File 'lib/file/visitor.rb', line 50 def dir_list(dir) dirs = [] visit_dir(dir) { |path| dirs << path } dirs end |
#file_list(dir) ⇒ Object
44 45 46 47 48 |
# File 'lib/file/visitor.rb', line 44 def file_list(dir) files = [] visit(dir) { |path| files << path } files end |
#set_direction(dir) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/file/visitor.rb', line 25 def set_direction(dir) if dir.nil? raise ArgumentError, "direction is nil" end if !DIRECTION.include?(dir.to_sym) raise ArgumentError, "invalid direction: " + dir.to_s end @direction = dir end |
#target?(path) ⇒ Boolean
95 96 97 98 99 100 101 102 103 |
# File 'lib/file/visitor.rb', line 95 def target?(path) # all the paths are target when no filters given return true unless @filters @filters.each do |filter| return false unless filter.match?(path) end true end |
#visit(dir, &handler) ⇒ Object
36 37 38 |
# File 'lib/file/visitor.rb', line 36 def visit(dir, &handler) visit_with_mode(dir, :file, &handler) end |
#visit_dir(dir, &handler) ⇒ Object
40 41 42 |
# File 'lib/file/visitor.rb', line 40 def visit_dir(dir, &handler) visit_with_mode(dir, :dir, &handler) end |