Class: Lumin::Rule
- Inherits:
-
Object
show all
- Defined in:
- lib/lumin/rule.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config = {}) ⇒ Rule
Returns a new instance of Rule.
64
65
66
67
68
|
# File 'lib/lumin/rule.rb', line 64
def initialize(config = {})
@config = self.class.default_config.merge(config.transform_keys(&:to_sym)).freeze
@offenses = []
@source = nil
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
62
63
64
|
# File 'lib/lumin/rule.rb', line 62
def config
@config
end
|
#offenses ⇒ Object
Returns the value of attribute offenses.
62
63
64
|
# File 'lib/lumin/rule.rb', line 62
def offenses
@offenses
end
|
#source ⇒ Object
Returns the value of attribute source.
62
63
64
|
# File 'lib/lumin/rule.rb', line 62
def source
@source
end
|
Class Method Details
.configuration_error(settings) ⇒ Object
50
51
52
53
|
# File 'lib/lumin/rule.rb', line 50
def configuration_error(settings)
validator = validate_config
validator&.call(settings)
end
|
.default_config(values = nil) ⇒ Object
35
36
37
38
|
# File 'lib/lumin/rule.rb', line 35
def default_config(values = nil)
@default_config = symbolize(values).freeze if values
@default_config ||= {}.freeze
end
|
.inherited(subclass) ⇒ Object
10
11
12
13
|
# File 'lib/lumin/rule.rb', line 10
def inherited(subclass)
super
Registry.register(subclass)
end
|
.on(node_type, &block) ⇒ Object
15
16
17
18
19
|
# File 'lib/lumin/rule.rb', line 15
def on(node_type, &block)
raise ArgumentError, "callback is required" unless block
subscriptions << [node_type.to_sym, block]
end
|
.on_new_source(&block) ⇒ Object
21
22
23
24
25
|
# File 'lib/lumin/rule.rb', line 21
def on_new_source(&block)
raise ArgumentError, "callback is required" unless block
source_callbacks << block
end
|
.safe(value = nil) ⇒ Object
40
41
42
43
|
# File 'lib/lumin/rule.rb', line 40
def safe(value = nil)
@safe = value unless value.nil?
@safe.nil? ? true : @safe
end
|
.source_callbacks ⇒ Object
31
32
33
|
# File 'lib/lumin/rule.rb', line 31
def source_callbacks
@source_callbacks ||= []
end
|
.subscriptions ⇒ Object
27
28
29
|
# File 'lib/lumin/rule.rb', line 27
def subscriptions
@subscriptions ||= []
end
|
.validate_config(&block) ⇒ Object
45
46
47
48
|
# File 'lib/lumin/rule.rb', line 45
def validate_config(&block)
@config_validator = block if block
@config_validator
end
|
Instance Method Details
#add_offense(location, message:, &correction) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/lumin/rule.rb', line 83
def add_offense(location, message:, &correction)
correction = nil if config[:autocorrect] == false
normalized = Astel::Location.from(location)
offenses << Offense.new(
path: source.path,
location: normalized,
rule_name: Registry.name_for(self.class),
message: message,
correctable: !correction.nil?,
safe: self.class.safe,
correction: correction
)
end
|
#begin_source(source) ⇒ Object
70
71
72
73
74
75
|
# File 'lib/lumin/rule.rb', line 70
def begin_source(source)
@source = source
@offenses = []
self.class.source_callbacks.each { |callback| instance_exec(source, &callback) }
self
end
|
#dispatch(node_type, node) ⇒ Object
77
78
79
80
81
|
# File 'lib/lumin/rule.rb', line 77
def dispatch(node_type, node)
self.class.subscriptions.each do |subscribed_type, callback|
instance_exec(node, &callback) if subscribed_type == node_type
end
end
|