Class: Terracop::Cop::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/terracop/cop/base.rb

Overview

Base class for all cops.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name, index, attributes) ⇒ Base

Returns a new instance of Base.



75
76
77
78
79
80
81
# 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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



22
23
24
# File 'lib/terracop/cop/base.rb', line 22

def attributes
  @attributes
end

#indexObject

Returns the value of attribute index.



22
23
24
# File 'lib/terracop/cop/base.rb', line 22

def index
  @index
end

#nameObject

Returns the value of attribute name.



22
23
24
# File 'lib/terracop/cop/base.rb', line 22

def name
  @name
end

#offensesObject (readonly)

Returns the value of attribute offenses.



102
103
104
# File 'lib/terracop/cop/base.rb', line 102

def offenses
  @offenses
end

#typeObject

Returns the value of attribute type.



22
23
24
# File 'lib/terracop/cop/base.rb', line 22

def type
  @type
end

Class Method Details

.configObject



37
38
39
40
41
# 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_nameObject



33
34
35
# File 'lib/terracop/cop/base.rb', line 33

def cop_name
  name.gsub(/^Terracop::Cop::/, '').gsub('::', '/')
end

.run(type, name, index, attributes) ⇒ Object



25
26
27
28
29
30
31
# 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

Instance Method Details

#checkObject

Raises:

  • (NotImplementedError)


87
88
89
90
# 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_nameObject



83
84
85
# File 'lib/terracop/cop/base.rb', line 83

def human_name
  index ? "#{name}[#{index}]" : name
end

#offense(message, severity = :convention) ⇒ Object



92
93
94
95
96
97
98
99
100
# 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