Class: Kotoshu::Grammar::Rule

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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 = message
  @suggestion = suggestion
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



10
11
12
# File 'lib/kotoshu/grammar/rule.rb', line 10

def category
  @category
end

#descriptionObject (readonly)

Returns the value of attribute description.



10
11
12
# File 'lib/kotoshu/grammar/rule.rb', line 10

def description
  @description
end

#exceptionsObject (readonly)

Returns the value of attribute exceptions.



10
11
12
# File 'lib/kotoshu/grammar/rule.rb', line 10

def exceptions
  @exceptions
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/kotoshu/grammar/rule.rb', line 10

def id
  @id
end

#messageObject (readonly)

Returns the value of attribute message.



10
11
12
# File 'lib/kotoshu/grammar/rule.rb', line 10

def message
  @message
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/kotoshu/grammar/rule.rb', line 10

def name
  @name
end

#severityObject (readonly)

Returns the value of attribute severity.



10
11
12
# File 'lib/kotoshu/grammar/rule.rb', line 10

def severity
  @severity
end

#suggestionObject (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.

Parameters:

  • config (Hash)

    YAML configuration hash

Returns:

  • (Rule)

    A new rule instance



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.

Parameters:

  • tokens (Array<Hash>)

    Array of token hashes

Returns:

  • (Array<Hash>)

    Array of error hashes



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