Class: PathSpec
- Inherits:
-
Object
- Object
- PathSpec
- 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
-
#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.
12 13 14 15 16 17 18 |
# File 'lib/pathspec.rb', line 12 def initialize(lines = nil, type = :git) @specs = [] add(lines, type) if lines self end |
Instance Attribute Details
#specs ⇒ Object (readonly)
Returns the value of attribute specs.
10 11 12 |
# File 'lib/pathspec.rb', line 10 def specs @specs end |
Class Method Details
.from_filename(filename, type = :git) ⇒ Object
Generate specs from a filename, such as a .gitignore
72 73 74 |
# File 'lib/pathspec.rb', line 72 def self.from_filename(filename, type = :git) File.open(filename, 'r') { |io| from_lines(io, type) } end |
.from_lines(lines, type = :git) ⇒ Object
76 77 78 |
# File 'lib/pathspec.rb', line 76 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/pathspec.rb', line 81 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
67 68 69 |
# File 'lib/pathspec.rb', line 67 def drive_letter_to_path(path) path.gsub(%r{^([a-zA-Z]):/}, '/\1/') end |
#empty? ⇒ Boolean
101 102 103 |
# File 'lib/pathspec.rb', line 101 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
23 24 25 26 |
# File 'lib/pathspec.rb', line 23 def match(path) matches = specs_matching(path.to_s) !matches.empty? && matches.all? {|m| m.inclusive?} end |
#match_path(path, root = '/') ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/pathspec.rb', line 49 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
57 58 59 60 61 62 63 64 65 |
# File 'lib/pathspec.rb', line 57 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
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pathspec.rb', line 36 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
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/pathspec.rb', line 105 def spec_type(type) case type when :git GitIgnoreSpec when :regex RegexSpec else raise "Unknown spec type #{type}" end end |
#specs_matching(path) ⇒ Object
28 29 30 31 32 |
# File 'lib/pathspec.rb', line 28 def specs_matching(path) @specs.select do |spec| spec if spec.match(path) end end |