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



92
93
94
# File 'lib/ripper-tags/data_reader.rb', line 92

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

#each_file(&block) ⇒ Object



96
97
98
99
100
101
# File 'lib/ripper-tags/data_reader.rb', line 96

def each_file(&block)
  return to_enum(__method__) unless block_given?
  each_input_file do |file|
    resolve_file(file, &block)
  end
end

#each_in_directory(directory) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ripper-tags/data_reader.rb', line 75

def each_in_directory(directory)
  begin
    entries = Dir.entries(directory)
  rescue Errno::EACCES
    $stderr.puts "%s: skipping unreadable directory `%s'" % [
      File.basename($0),
      directory
    ]
  else
    entries.each do |name|
      if name != DIR_CURRENT && name != DIR_PARENT
        yield File.join(directory, name)
      end
    end
  end
end

#each_input_file(&block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ripper-tags/data_reader.rb', line 103

def each_input_file(&block)
  if options.input_file
    io = options.input_file == "-" ? $stdin : File.new(options.input_file, DataReader::READ_MODE)
    begin
      io.each_line { |line| yield line.chomp }
    ensure
      io.close
    end
  else
    options.files.each(&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, depth) ⇒ Boolean

Returns:

  • (Boolean)


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

def include_file?(file, depth)
  (depth == 0 || 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
# 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)
      each_in_directory(file) do |subfile|
        subfile = clean_path(subfile) if depth == 0
        resolve_file(subfile, depth + 1, &block)
      end
    end
  elsif depth > 0 || File.exist?(file)
    file = clean_path(file) if depth == 0
    yield file if include_file?(file, depth)
  else
    $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