Class: FastIgnore::Patterns

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Patterns.



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

def initialize(*patterns, from_file: nil, format: :gitignore, root: nil)
  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?('/')
  @expand_path_with = (@root if format == :expand_path)
end

Instance Method Details

#build_matchers(allow: false) ⇒ Object

rubocop:disable Metrics/MethodLength



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

def build_matchers(allow: false) # rubocop:disable Metrics/MethodLength
  matchers = @patterns.flat_map do |p|
    ::FastIgnore::Builders::ShebangOrGitignore.build(p, allow, expand_path_with: @expand_path_with)
  end

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

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