Class: RipperTags::FileFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/ripper-tags/data_reader.rb

Constant Summary collapse

RUBY_EXT =
'.rb'.freeze
DIR_CURRENT =
'.'.freeze
DIR_PARENT =
'..'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FileFinder

Returns a new instance of FileFinder.



13
14
15
# File 'lib/ripper-tags/data_reader.rb', line 13

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/ripper-tags/data_reader.rb', line 7

def options
  @options
end

Instance Method Details

#clean_path(file) ⇒ Object



78
79
80
# File 'lib/ripper-tags/data_reader.rb', line 78

def clean_path(file)
  Pathname.new(file).cleanpath.to_s
end

#each_file(&block) ⇒ Object



82
83
84
85
86
87
# File 'lib/ripper-tags/data_reader.rb', line 82

def each_file(&block)
  return to_enum(__method__) unless block_given?
  options.files.each do |file|
    resolve_file(file, &block)
  end
end

#exclude_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ripper-tags/data_reader.rb', line 27

def exclude_file?(file)
  base = File.basename(file)
  match = exclude_patterns.find { |ex|
    case ex
    when Regexp then base =~ ex
    else base == ex
    end
  } || exclude_patterns.find { |ex|
    case ex
    when Regexp then file =~ ex
    else file.include?(ex)
    end
  }

  if match && options.verbose
    $stderr.puts "Ignoring %s because of exclude rule: %p" % [file, match]
  end

  match
end

#exclude_patternsObject



17
18
19
20
21
22
23
24
25
# File 'lib/ripper-tags/data_reader.rb', line 17

def exclude_patterns
  @exclude ||= Array(options.exclude).map { |pattern|
    if pattern.index('*')
      Regexp.new(Regexp.escape(pattern).gsub('\\*', '[^/]+'))
    else
      pattern
    end
  }
end

#include_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/ripper-tags/data_reader.rb', line 52

def include_file?(file)
  (options.all_files || ruby_file?(file)) && !exclude_file?(file)
end

#resolve_file(file, depth = 0, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ripper-tags/data_reader.rb', line 56

def resolve_file(file, depth = 0, &block)
  if File.directory?(file)
    if options.recursive && !exclude_file?(file)
      Dir.entries(file).each do |name|
        if name != DIR_CURRENT && name != DIR_PARENT
          subfile = File.join(file, name)
          subfile = clean_path(subfile) if depth == 0
          resolve_file(subfile, depth + 1, &block)
        end
      end
    end
  elsif depth > 0 || File.exist?(file)
    file = clean_path(file) if depth == 0
    yield file if include_file?(file)
  elsif
    $stderr.puts "%s: %p: no such file or directory" % [
      File.basename($0),
      file
    ]
  end
end

#ruby_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ripper-tags/data_reader.rb', line 48

def ruby_file?(file)
  file.end_with?(RUBY_EXT)
end