Class: Xccoveralls::Ignorefile

Inherits:
Object
  • Object
show all
Defined in:
lib/xccoveralls/ignorefile.rb

Constant Summary collapse

COMMENT_OR_WHITESPACE =
/^\s*(?:#.*)?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Ignorefile

Returns a new instance of Ignorefile.



12
13
14
15
16
17
18
19
20
# File 'lib/xccoveralls/ignorefile.rb', line 12

def initialize(*args)
  @statements = []

  args.each do |arg|
    !arg.is_a?(Array) && File.exist?(arg) &&
      (return load_file(arg))
    push(arg)
  end
end

Instance Attribute Details

#statementsObject (readonly)

Returns the value of attribute statements.



9
10
11
# File 'lib/xccoveralls/ignorefile.rb', line 9

def statements
  @statements
end

Instance Method Details

#apply(files) ⇒ Object



55
56
57
# File 'lib/xccoveralls/ignorefile.rb', line 55

def apply(files)
  files.reject { |file| ignored?(file) }
end

#apply!(files) ⇒ Object



59
60
61
# File 'lib/xccoveralls/ignorefile.rb', line 59

def apply!(files)
  files.reject! { |file| ignored?(file) }
end

#ignored?(file) ⇒ Boolean

rubocop:disable Metrics/AbcSize

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/xccoveralls/ignorefile.rb', line 40

def ignored?(file) # rubocop:disable Metrics/AbcSize
  return true if file.to_s.strip.empty?

  path = Pathname.new file

  includes = statements.reject { |statement| statement[0] == '!' }
                       .map { |statement| "*#{statement}*" }

  excludes = statements.select { |statement| statement[0] == '!' }
                       .map { |statement| "*#{statement[1..-1]}*" }

  includes.any? { |statement| path.fnmatch?(statement) } &&
    excludes.all? { |statement| !path.fnmatch?(statement) }
end

#load_file(file) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/xccoveralls/ignorefile.rb', line 29

def load_file(file)
  file = Pathname.new(file)
  return unless file.exist?

  push(file.readlines.map(&:strip).reject do |line|
    line.empty? || line =~ COMMENT_OR_WHITESPACE
  end)

  self
end

#push(*arg) ⇒ Object Also known as: <<



22
23
24
25
26
# File 'lib/xccoveralls/ignorefile.rb', line 22

def push(*arg)
  statements.push(*arg.flatten.map(&:strip))

  self
end