Class: PathList::GitignoreIncludeRuleBuilder

Inherits:
GitignoreRuleBuilder show all
Defined in:
lib/path_list/gitignore_include_rule_builder.rb

Instance Method Summary collapse

Methods inherited from GitignoreRuleBuilder

#anchored!, #blank!, #break!, #build, #dir_only!, #emit_any_dir, #emit_dir, #never_anchored!, #nothing_emitted?, #prefix, #process_backslash, #process_character_class, #process_end, #process_slash, #process_star_end_after_slash, #process_two_stars

Constructor Details

#initialize(rule, expand_path_with: nil) ⇒ GitignoreIncludeRuleBuilder

:nocov:



9
10
11
12
13
14
# File 'lib/path_list/gitignore_include_rule_builder.rb', line 9

def initialize(rule, expand_path_with: nil)
  super(rule)

  @negation = true
  @expand_path_from = expand_path_with
end

Instance Method Details

#build_as_parentObject



74
75
76
77
78
79
80
81
82
# File 'lib/path_list/gitignore_include_rule_builder.rb', line 74

def build_as_parent
  anchored!
  dir_only!

  catch :abort_build do
    process_rule
    build_rule(child_file_rule: false)
  end
end

#build_child_file_ruleObject

rubocop:disable Metrics/MethodLength



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/path_list/gitignore_include_rule_builder.rb', line 58

def build_child_file_rule # rubocop:disable Metrics/MethodLength
  if @child_re.end_with?('/')
    @child_re.append_many_non_dir.append_dir if @dir_only
  else
    @child_re.append_dir
  end

  @child_re.prepend(prefix)

  if @negation
    ::PathList::Matchers::AllowPathRegexp.new(@child_re.to_regexp, @anchored, false)
  else
    ::PathList::Matchers::IgnorePathRegexp.new(@child_re.to_regexp, @anchored, false)
  end
end

#build_parent_dir_rulesObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/path_list/gitignore_include_rule_builder.rb', line 45

def build_parent_dir_rules
  return unless @negation

  if @anchored
    parent_pattern = @s.string.dup
    if parent_pattern.sub!(%r{/[^/]+/?\s*\z}, '/')
      ::PathList::GitignoreIncludeRuleBuilder.new(parent_pattern).build_as_parent
    end
  else
    [::PathList::Matchers::AllowAnyDir]
  end
end

#build_rule(child_file_rule: true) ⇒ Object



84
85
86
87
88
# File 'lib/path_list/gitignore_include_rule_builder.rb', line 84

def build_rule(child_file_rule: true)
  @child_re ||= @re.dup # in case emit_end wasn't called

  [super(), *build_parent_dir_rules, (build_child_file_rule if child_file_rule)].compact
end

#emit_endObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/path_list/gitignore_include_rule_builder.rb', line 34

def emit_end
  if @dir_only
    @child_re = @re.dup
    @re.append_end_anchor
  else
    @re.append_dir_or_end_anchor
  end

  break!
end

#expand_rule_pathObject



16
17
18
19
20
21
22
23
24
# File 'lib/path_list/gitignore_include_rule_builder.rb', line 16

def expand_rule_path
  anchored! unless @s.match?(/\*/)
  return unless @s.match?(%r{(?:[~/]|\.{1,2}/|.*/\.\./)})

  dir_only! if @s.match?(%r{.*/\s*\z})
  @s.string.replace(::File.expand_path(@s.rest))
  @s.string.delete_prefix!(@expand_path_from)
  @s.pos = 0
end

#negated!Object



26
27
28
# File 'lib/path_list/gitignore_include_rule_builder.rb', line 26

def negated!
  @negation = false
end

#process_ruleObject



90
91
92
93
# File 'lib/path_list/gitignore_include_rule_builder.rb', line 90

def process_rule
  expand_rule_path if @expand_path_from
  super
end

#unmatchable_rule!Object



30
31
32
# File 'lib/path_list/gitignore_include_rule_builder.rb', line 30

def unmatchable_rule!
  throw :abort_build, ::PathList::Matchers::Unmatchable
end