Class: Kotoshu::Grammar::Rule
- Inherits:
-
Object
- Object
- Kotoshu::Grammar::Rule
- Defined in:
- lib/kotoshu/grammar/rule.rb
Overview
Base class for grammar rules.
All grammar rules inherit from this class and implement the #check method to validate tokens.
Instance Attribute Summary collapse
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#exceptions ⇒ Object
readonly
Returns the value of attribute exceptions.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#severity ⇒ Object
readonly
Returns the value of attribute severity.
-
#suggestion ⇒ Object
readonly
Returns the value of attribute suggestion.
Class Method Summary collapse
-
.from_yaml(config) ⇒ Rule
Factory method to create Rule from YAML configuration.
Instance Method Summary collapse
-
#check(tokens) ⇒ Array<Hash>
Check tokens against this rule.
-
#initialize(id:, name:, category:, severity:, description:, patterns:, exceptions: {}, message:, suggestion:) ⇒ Rule
constructor
A new instance of Rule.
Constructor Details
#initialize(id:, name:, category:, severity:, description:, patterns:, exceptions: {}, message:, suggestion:) ⇒ Rule
Returns a new instance of Rule.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/kotoshu/grammar/rule.rb', line 13 def initialize(id:, name:, category:, severity:, description:, patterns:, exceptions: {}, message:, suggestion:) @id = id @name = name @category = category @severity = severity @description = description @patterns = patterns @exceptions = exceptions @message = @suggestion = suggestion end |
Instance Attribute Details
#category ⇒ Object (readonly)
Returns the value of attribute category.
10 11 12 |
# File 'lib/kotoshu/grammar/rule.rb', line 10 def category @category end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
10 11 12 |
# File 'lib/kotoshu/grammar/rule.rb', line 10 def description @description end |
#exceptions ⇒ Object (readonly)
Returns the value of attribute exceptions.
10 11 12 |
# File 'lib/kotoshu/grammar/rule.rb', line 10 def exceptions @exceptions end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/kotoshu/grammar/rule.rb', line 10 def id @id end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
10 11 12 |
# File 'lib/kotoshu/grammar/rule.rb', line 10 def @message end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/kotoshu/grammar/rule.rb', line 10 def name @name end |
#severity ⇒ Object (readonly)
Returns the value of attribute severity.
10 11 12 |
# File 'lib/kotoshu/grammar/rule.rb', line 10 def severity @severity end |
#suggestion ⇒ Object (readonly)
Returns the value of attribute suggestion.
10 11 12 |
# File 'lib/kotoshu/grammar/rule.rb', line 10 def suggestion @suggestion end |
Class Method Details
.from_yaml(config) ⇒ Rule
Factory method to create Rule from YAML configuration.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/kotoshu/grammar/rule.rb', line 30 def self.from_yaml(config) new( id: config['id'], name: config['name'], category: config['category'], severity: config['severity'], description: config['description'], patterns: config['patterns'], exceptions: config['exceptions'] || {}, message: config['message'], suggestion: config['suggestion'] ) end |
Instance Method Details
#check(tokens) ⇒ Array<Hash>
Check tokens against this rule.
48 49 50 51 52 53 54 55 |
# File 'lib/kotoshu/grammar/rule.rb', line 48 def check(tokens) errors = [] @patterns.each do |pattern| pattern_errors = check_pattern(tokens, pattern) errors.concat(pattern_errors) end errors end |