class Terracop::Cop::Base

Base class for all cops.

Attributes

attributes[RW]
index[RW]
name[RW]
offenses[R]
type[RW]

Public Class Methods

config() click to toggle source
# File lib/terracop/cop/base.rb, line 37
def config
  config = Terracop.config[cop_name] || {}
  config['Enabled'] = config['Enabled'].nil? ? true : config['Enabled']
  config
end
cop_name() click to toggle source
# File lib/terracop/cop/base.rb, line 33
def cop_name
  name.gsub(/^Terracop::Cop::/, '').gsub('::', '/')
end
new(type, name, index, attributes) click to toggle source
# File lib/terracop/cop/base.rb, line 75
def initialize(type, name, index, attributes)
  self.type = type
  self.name = name
  self.index = index
  self.attributes = attributes
  @offenses = []
end
run(type, name, index, attributes) click to toggle source
# File lib/terracop/cop/base.rb, line 25
def run(type, name, index, attributes)
  return unless applies_to?(type, name)

  cop = new(type, name, index, attributes)
  cop.check
  cop.offenses
end

Protected Class Methods

applies_to(*types) click to toggle source
# File lib/terracop/cop/base.rb, line 49
def applies_to(*types)
  @applies_to ||= []
  @applies_to += types.map(&:to_s)
end
applies_to?(type, name) click to toggle source
# File lib/terracop/cop/base.rb, line 54
def applies_to?(type, name)
  return unless enabled?
  return unless @applies_to.nil? || @applies_to.include?(type)

  !excludes?(type, name)
end
enabled?() click to toggle source
# File lib/terracop/cop/base.rb, line 61
def enabled?
  config['Enabled']
end
excludes?(type, name) click to toggle source
# File lib/terracop/cop/base.rb, line 65
def excludes?(type, name)
  excludes = config['Exclude'] || []
  excludes.each do |rule|
    return true if ["#{type}.*", "#{type}.#{name}"].include?(rule)
  end

  false
end
register() click to toggle source
# File lib/terracop/cop/base.rb, line 45
def register
  Terracop::Cop.all << self
end

Public Instance Methods

check() click to toggle source
# File lib/terracop/cop/base.rb, line 87
def check
  message = "#{self.class.name} does not implement the #check method"
  raise NotImplementedError, message
end
human_name() click to toggle source
# File lib/terracop/cop/base.rb, line 83
def human_name
  index ? "#{name}[#{index}]" : name
end
offense(message, severity = :convention) click to toggle source
# File lib/terracop/cop/base.rb, line 92
def offense(message, severity = :convention)
  @offenses << {
    cop_name: self.class.cop_name,
    severity: severity,
    type: type,
    name: human_name,
    message: message
  }
end