Class: Panache::Rule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexes, message) ⇒ Rule

Returns a new instance of Rule.



13
14
15
16
# File 'lib/panache.rb', line 13

def initialize(regexes, message)
  @regexes = regexes
  @message = message
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



12
13
14
# File 'lib/panache.rb', line 12

def message
  @message
end

Instance Method Details

#check(line) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/panache.rb', line 18

def check(line)
  offset = 0
  violations = []
  @regexes.each do |regex|
    while (match = regex.match(line, offset))
      # If the match size is 1, then the user did not provide any captures in their regex and we only know
      # that there is >= 1 violation of the rule in this line.
      if match.size == 1
        return [Violation.new(nil, line, nil, nil, self)]
      end
      offset = match.offset(1)[0]
      violations << Violation.new(nil, line, offset, nil, self)
      offset += 1
    end
  end
  violations
end