Class: Flatito::TreeIterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/flatito/tree_iterator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path, options = {}) ⇒ TreeIterator

Returns a new instance of TreeIterator.



9
10
11
12
# File 'lib/flatito/tree_iterator.rb', line 9

def initialize(base_path, options = {})
  @base_path = base_path
  @skip_hidden = options[:skip_hidden] || true
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



7
8
9
# File 'lib/flatito/tree_iterator.rb', line 7

def base_path
  @base_path
end

#skip_hiddenObject (readonly)

Returns the value of attribute skip_hidden.



7
8
9
# File 'lib/flatito/tree_iterator.rb', line 7

def skip_hidden
  @skip_hidden
end

Instance Method Details

#each(&block) ⇒ Object



14
15
16
# File 'lib/flatito/tree_iterator.rb', line 14

def each(&block)
  tree(Pathname.new(base_path), &block)
end

#tree(parent, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/flatito/tree_iterator.rb', line 18

def tree(parent, &block)
  if parent.directory?
    return if parent.symlink?

    parent.each_child.each do |pathname|
      next if skip_hidden && pathname.basename.to_s[0] == "."

      tree(pathname, &block)
    end
  else
    yield(parent)
  end
rescue Errno::ELOOP => e
  warn "Error reading #{parent}, #{e.message}"

  []
end