Class: Chirp::PathFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/chirp/path_filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PathFilter

Returns a new instance of PathFilter.



3
4
5
6
7
8
9
10
11
12
# File 'lib/chirp/path_filter.rb', line 3

def initialize(options={})
  @regexp = @glob = nil
  @regexp = options[:path] if options[:path].kind_of?(Regexp)
  @glob = options[:path] if options[:path].kind_of?(String)
  @fsexp = options[:fsexp]
  @contains = options[:contains]
  @name = options[:name]
  @dir=options[:dir]
  @type = options[:type]
end

Instance Method Details

#file_contains(path, re) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/chirp/path_filter.rb', line 44

def file_contains(path, re)
  return false unless File.file?(path)
  File.open(path) do |f|
    until f.eof?
      return true if re =~ f.readline
    end
  end
  false
end

#passes?(path) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chirp/path_filter.rb', line 14

def passes?(path)
#puts "trying filter #{self} on #{path}"
  if @type == :file
    return false unless File.file?(path)
  end
  if @type == :dir
    return false unless File.directory?(path)
  end
  if @name
    return false unless File.fnmatch(@name, File.basename(path))
  end
  if @fsexp
    return false unless @fsexp.evaluate(path)
  end
  if @glob
    return false unless File.fnmatch(@glob, path,  File::FNM_PATHNAME | File::FNM_DOTMATCH )
  end
  if @regexp
    return false unless @regexp =~ path
  end
  if @dir
    return false unless File.dirname(path) == @dir
  end
  if @contains
    return false unless file_contains(path, @contains)
  end
#puts "passed filter: #{path}"
  true
end

#to_sObject



54
55
56
# File 'lib/chirp/path_filter.rb', line 54

def to_s
  "Filter: #{@glob} #{@dir} #{@type}"
end