Class: PathSpec
Instance Attribute Summary collapse
-
#specs ⇒ Object
readonly
Returns the value of attribute specs.
Class Method Summary collapse
-
.from_filename(filename, type = :git) ⇒ Object
Generate specs from a filename, such as a .gitignore.
- .from_lines(lines, type = :git) ⇒ Object
Instance Method Summary collapse
-
#add(obj, type = :git) ⇒ Object
Generate specs from lines of text.
- #drive_letter_to_path(path) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(lines = nil, type = :git) ⇒ PathSpec
constructor
A new instance of PathSpec.
-
#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.
- #match_path(path, root = '/') ⇒ Object
- #match_paths(paths, root = '/') ⇒ Object
-
#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.
- #spec_type(type) ⇒ Object
- #specs_matching(path) ⇒ Object
Constructor Details
#initialize(lines = nil, type = :git) ⇒ PathSpec
Returns a new instance of PathSpec.
9 10 11 12 13 14 15 16 17 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 9 def initialize(lines=nil, type=:git) @specs = [] if lines add(lines, type) end self end |
Instance Attribute Details
#specs ⇒ Object (readonly)
Returns the value of attribute specs.
7 8 9 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 7 def specs @specs end |
Class Method Details
.from_filename(filename, type = :git) ⇒ Object
Generate specs from a filename, such as a .gitignore
77 78 79 80 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 77 def self.from_filename(filename, type=:git) # .gitignore is a UTF-8 file self.from_lines(File.open(filename, 'r', :encoding => Encoding::UTF_8), type) end |
.from_lines(lines, type = :git) ⇒ Object
82 83 84 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 82 def self.from_lines(lines, type=:git) self.new lines, type end |
Instance Method Details
#add(obj, type = :git) ⇒ Object
Generate specs from lines of text
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 87 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) if !spec.regex.nil? && !spec.inclusive?.nil? @specs << spec end 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
72 73 74 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 72 def drive_letter_to_path(path) path.gsub(/^([a-zA-Z]):\//, '/\1/') end |
#empty? ⇒ Boolean
109 110 111 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 109 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
22 23 24 25 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 22 def match(path) matches = specs_matching(path.to_s) !matches.empty? && matches.all? {|m| m.inclusive?} end |
#match_path(path, root = '/') ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 52 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 = relpath + '/' if path[-1].chr == '/' match(relpath) end |
#match_paths(paths, root = '/') ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 60 def match_paths(paths, root='/') matching = [] paths.each do |path| if match_path(path, root) matching << path end 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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 37 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 if match(relpath) matching << path end end matching end |
#spec_type(type) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 113 def spec_type(type) if type == :git GitIgnoreSpec elsif type == :regex RegexSpec else raise "Unknown spec type #{type}" end end |
#specs_matching(path) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/puppet/vendor/pathspec/lib/pathspec.rb', line 27 def specs_matching(path) @specs.select do |spec| if spec.match(path) spec end end end |