Class: Milkode::IgnoreSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/milkode/common/ignore_setting.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, ignores) ⇒ IgnoreSetting

Returns a new instance of IgnoreSetting.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/milkode/common/ignore_setting.rb', line 25

def initialize(path, ignores)
  @path = path
  @ignores =  []
  @not_ignores = []
  
  ignores.each do |v|
    v = v.sub(/\/\Z/, "")

    unless v.start_with?('!')
      @ignores << v
    else
      @not_ignores << v.sub(/\A!/, "")
    end
  end

  # 除外設定 -> 正規表現
  @regexps = @ignores.map {|v| convert_regexp(v)}

  # 非除外設定 -> 展開 -> 正規表現
  new_not_ignores = []
  @not_regexps = []
  
  @not_ignores.each do |v|
    last_regexp = "(\/|\\Z)" # 自分自身は後方マッチ可

    Pathname.new(v).ascend do |pathname|
      txt = pathname.to_s
      new_not_ignores << txt
      @not_regexps << convert_regexp(txt, last_regexp)
      last_regexp = "\\Z"   # 親ディクレクリは後方マッチ不可
    end
  end

  @not_ignores = new_not_ignores
end

Instance Attribute Details

#ignoresObject (readonly)

Returns the value of attribute ignores.



13
14
15
# File 'lib/milkode/common/ignore_setting.rb', line 13

def ignores
  @ignores
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/milkode/common/ignore_setting.rb', line 12

def path
  @path
end

Class Method Details

.create_from_gitignore(path, str) ⇒ Object



15
16
17
# File 'lib/milkode/common/ignore_setting.rb', line 15

def self.create_from_gitignore(path, str)
  IgnoreSetting.new(path, parse_gitignore(str))
end

.parse_gitignore(str) ⇒ Object



19
20
21
22
23
# File 'lib/milkode/common/ignore_setting.rb', line 19

def self.parse_gitignore(str)
  ignores = str.split($/)
  ignores.delete_if{|v| v =~ /(\A#.*)|(\A\Z)/}
  ignores
end

Instance Method Details

#ignore?(path) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/milkode/common/ignore_setting.rb', line 61

def ignore?(path)
  return false unless path.start_with?(@path)

  if (path.size == @path.size)
    false
  else
    if (@path == '/')
      ignore_in?(path)
    else
      ignore_in?(path[@path.size..-1])
    end
  end
end