Class: Tins::Find::Finder

Inherits:
Object show all
Defined in:
lib/tins/find.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Finder

Returns a new instance of Finder.



12
13
14
15
16
# File 'lib/tins/find.rb', line 12

def initialize(opts = {})
  @show_hidden     = opts.fetch(:show_hidden)     { true }
  @raise_errors    = opts.fetch(:raise_errors)    { false }
  @follow_symlinks = opts.fetch(:follow_symlinks) { true }
end

Instance Attribute Details

Returns the value of attribute follow_symlinks.



22
23
24
# File 'lib/tins/find.rb', line 22

def follow_symlinks
  @follow_symlinks
end

#raise_errorsObject (readonly)

Returns the value of attribute raise_errors.



20
21
22
# File 'lib/tins/find.rb', line 20

def raise_errors
  @raise_errors
end

#show_hiddenObject (readonly)

Returns the value of attribute show_hidden.



18
19
20
# File 'lib/tins/find.rb', line 18

def show_hidden
  @show_hidden
end

Instance Method Details

#find(*paths) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tins/find.rb', line 24

def find(*paths)
  block_given? or return enum_for(__method__, *paths)
  paths.collect! { |d| d.dup }
  while path = paths.shift
    path = prepare_path(path)
    catch(:prune) do
      path.stat or next
      yield path
      if path.stat.directory?
        begin
          ps = Dir.entries(path)
        rescue EXPECTED_STANDARD_ERRORS
          @raise_errors ? raise : next
        end
        ps.sort!
        ps.reverse_each do |p|
          next if p == "." or p == ".."
          next if !@show_hidden && p.start_with?('.')
          p = File.join(path, p)
          paths.unshift p.untaint
        end
      end
    end
  end
end