Class: PathSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/pathspec.rb,
lib/pathspec/spec.rb,
lib/pathspec/regexspec.rb,
lib/pathspec/gitignorespec.rb

Overview

Main PathSpec class, provides interfaces to various spec implementations

Defined Under Namespace

Classes: GitIgnoreSpec, RegexSpec, Spec

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines = nil, type = :git) ⇒ PathSpec

Returns a new instance of PathSpec.



10
11
12
13
14
15
16
# File 'lib/pathspec.rb', line 10

def initialize(lines = nil, type = :git)
  @specs = []

  add(lines, type) if lines

  self
end

Instance Attribute Details

#specsObject (readonly)

Returns the value of attribute specs.



8
9
10
# File 'lib/pathspec.rb', line 8

def specs
  @specs
end

Class Method Details

.from_filename(filename, type = :git) ⇒ Object

Generate specs from a filename, such as a .gitignore



70
71
72
# File 'lib/pathspec.rb', line 70

def self.from_filename(filename, type = :git)
  File.open(filename, 'r') { |io| from_lines(io, type) }
end

.from_lines(lines, type = :git) ⇒ Object



74
75
76
# File 'lib/pathspec.rb', line 74

def self.from_lines(lines, type = :git)
  new lines, type
end

Instance Method Details

#add(obj, type = :git) ⇒ Object

Generate specs from lines of text



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pathspec.rb', line 79

def add(obj, type = :git)
  spec_class = spec_type(type)

  if obj.respond_to?(:each_line)
    obj.each_line do |l|
      spec = spec_class.new(l.rstrip)

      @specs << spec if !spec.regex.nil? && !spec.inclusive?.nil?
    end
  elsif obj.respond_to?(:each)
    obj.each do |l|
      add(l, type)
    end
  else
    raise 'Cannot make Pathspec from non-string/non-enumerable object.'
  end

  self
end

#drive_letter_to_path(path) ⇒ Object



65
66
67
# File 'lib/pathspec.rb', line 65

def drive_letter_to_path(path)
  path.gsub(%r{^([a-zA-Z]):/}, '/\1/')
end

#empty?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/pathspec.rb', line 99

def empty?
  @specs.empty?
end

#match(path) ⇒ Object

Check if a path matches the pathspecs described Returns true if there are matches and none are excluded Returns false if there aren’t matches or none are included



21
22
23
24
# File 'lib/pathspec.rb', line 21

def match(path)
  matches = specs_matching(path.to_s)
  !matches.empty? && matches.all? {|m| m.inclusive?}
end

#match_path(path, root = '/') ⇒ Object



47
48
49
50
51
52
53
# File 'lib/pathspec.rb', line 47

def match_path(path, root = '/')
  rootpath = Pathname.new(drive_letter_to_path(root))
  relpath = Pathname.new(drive_letter_to_path(path)).relative_path_from(rootpath).to_s
  relpath += '/' if path[-1].chr == '/'

  match(relpath)
end

#match_paths(paths, root = '/') ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/pathspec.rb', line 55

def match_paths(paths, root = '/')
  matching = []

  paths.each do |path|
    matching << path if match_path(path, root)
  end

  matching
end

#match_tree(root) ⇒ Object

Check if any files in a given directory or subdirectories match the specs Returns matched paths or nil if no paths matched



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pathspec.rb', line 34

def match_tree(root)
  rootpath = Pathname.new(root)
  matching = []

  Find.find(root) do |path|
    relpath = Pathname.new(path).relative_path_from(rootpath).to_s
    relpath += '/' if File.directory? path
    matching << path if match(relpath)
  end

  matching
end

#spec_type(type) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/pathspec.rb', line 103

def spec_type(type)
  case type
  when :git
    GitIgnoreSpec
  when :regex
    RegexSpec
  else
    raise "Unknown spec type #{type}"
  end
end

#specs_matching(path) ⇒ Object



26
27
28
29
30
# File 'lib/pathspec.rb', line 26

def specs_matching(path)
  @specs.select do |spec|
    spec if spec.match(path)
  end
end