Class: PathList::Patterns

Inherits:
Object
  • Object
show all
Defined in:
lib/path_list/patterns.rb

Instance Method Summary collapse

Constructor Details

#initialize(*patterns, from_file: nil, format: :gitignore, root: nil) ⇒ Patterns

Returns a new instance of Patterns.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/path_list/patterns.rb', line 5

def initialize(*patterns, from_file: nil, format: :gitignore, root: nil)
  raise ArgumentError, "from_file: can't be used with patterns arguments" unless patterns.empty? || !from_file

  @format = format
  if from_file
    @root = root || ::File.dirname(from_file)
    @patterns = ::File.exist?(from_file) ? ::File.readlines(from_file) : []
  else
    @root = root || ::Dir.pwd
    @patterns = patterns.flatten.flat_map { |string| string.to_s.lines }
  end
  @root += '/' unless @root.end_with?('/')
end

Instance Method Details

#build_matchers(include: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/path_list/patterns.rb', line 19

def build_matchers(include: false)
  matchers = @patterns.flat_map { |p| ::PathList::RuleBuilder.build(p, include, @format, @root) }

  return if matchers.empty?
  return [::PathList::Matchers::WithinDir.new(matchers, @root)] unless include

  [
    ::PathList::Matchers::WithinDir.new(matchers, @root),
    ::PathList::Matchers::WithinDir.new(
      ::PathList::GitignoreIncludeRuleBuilder.new(@root).build_as_parent, '/'
    )
  ]
end