class Backto::Scanner

Attributes

exclude_patterns[R]

Public Class Methods

new(entry, exclude_patterns = [], skip_recursive = false) click to toggle source
# File lib/backto/scanner.rb, line 17
def initialize(entry, exclude_patterns = [], skip_recursive = false)
  @entry = entry
  @skip_recursive = skip_recursive
  @exclude_patterns = self.class.normalize_patterns(exclude_patterns)
end
normalize_patterns(patterns) click to toggle source
# File lib/backto/scanner.rb, line 8
def self.normalize_patterns(patterns)
  patterns.map do |pattern|
    normalized = pattern.dup
    match_dir = normalized.chomp!('/') != nil
    normalized = '/**/' + normalized unless normalized.start_with?('/')
    [normalized, match_dir]
  end
end

Public Instance Methods

each(&block) click to toggle source
# File lib/backto/scanner.rb, line 23
def each(&block)
  scan Path.new(@entry), &block
end

Private Instance Methods

exclude?(path) click to toggle source
# File lib/backto/scanner.rb, line 40
def exclude?(path)
  test_path = File.join('/', path.relative)
  exclude_patterns.any? do |pattern, match_dir|
    fnmatch?(pattern, test_path) && (match_dir ? path.directory? : true)
  end
end
fnmatch?(pattern, path_name) click to toggle source
# File lib/backto/scanner.rb, line 56
def fnmatch?(pattern, path_name)
   File.fnmatch? pattern, path_name, File::FNM_PATHNAME | File::FNM_DOTMATCH
end
recursive?(path) click to toggle source
# File lib/backto/scanner.rb, line 47
def recursive?(path)
  return false unless path.directory?
  if @skip_recursive.is_a? Array
    !@skip_recursive.include? path.relative
  else
    !@skip_recursive
  end
end
scan(parent, &block) click to toggle source
# File lib/backto/scanner.rb, line 29
def scan(parent, &block)
  Dir.foreach(parent.absolute) do |name|
    next if name == '.' || name == '..'
    path = parent.join(name)
    next if exclude? path
    is_recursive = recursive?(path)
    block.call(path, is_recursive)
    scan(path, &block) if is_recursive
  end
end