Class: Uniword::Lint::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/lint/rule.rb

Overview

Abstract base class for a single lint rule. Subclasses implement #check (yields findings) and declare name, severity.

Open/closed: new rule type = new subclass + (optionally) a YAML schema extension. Engine unchanged.

Constant Summary collapse

TYPES =

Registered rule subclasses by type name. Constant on Rule itself (not subclasses) so register calls always reach the same hash. Hash.new (rather than {}) avoids the Style/MutableConstant autocorrect that would .freeze it and break register.

Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, severity: :warning, **options) ⇒ Rule

Returns a new instance of Rule.

Parameters:

  • name (Symbol)

    rule identifier

  • severity (Symbol) (defaults to: :warning)

    :error, :warning, or :info

  • options (Hash)

    rule-specific options



23
24
25
26
27
# File 'lib/uniword/lint/rule.rb', line 23

def initialize(name:, severity: :warning, **options)
  @name = name
  @severity = severity
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/uniword/lint/rule.rb', line 18

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/uniword/lint/rule.rb', line 18

def options
  @options
end

#severityObject (readonly)

Returns the value of attribute severity.



18
19
20
# File 'lib/uniword/lint/rule.rb', line 18

def severity
  @severity
end

Class Method Details

.register(name, klass) ⇒ void

This method returns an undefined value.

Register a subclass under a type name for YAML ruleset lookup. Always writes to Rule::TYPES regardless of the calling subclass.

Parameters:

  • name (Symbol)
  • klass (Class<Rule>)


56
57
58
# File 'lib/uniword/lint/rule.rb', line 56

def register(name, klass)
  Rule::TYPES[name] = klass
end

.type(name) ⇒ Class<Rule>?

Lookup a registered rule subclass by type name.

Parameters:

  • name (Symbol)

Returns:

  • (Class<Rule>, nil)


64
65
66
# File 'lib/uniword/lint/rule.rb', line 64

def type(name)
  Rule::TYPES[name]
end

.typesHash{Symbol => Class<Rule>}

All registered rule types.

Returns:

  • (Hash{Symbol => Class<Rule>})


71
72
73
# File 'lib/uniword/lint/rule.rb', line 71

def types
  Rule::TYPES
end

Instance Method Details

#check(document) {|finding| ... } ⇒ void

This method returns an undefined value.

Walk the document and yield each finding.

Parameters:

Yield Parameters:

  • finding (Hash)

    { message:, path:, position: }

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/uniword/lint/rule.rb', line 34

def check(document)
  raise NotImplementedError
end

#finding(message:, path: nil) ⇒ Hash

Build a finding hash.

Parameters:

  • message (String)
  • path (String, nil) (defaults to: nil)

    document path (paragraph index, style id, etc.)

Returns:

  • (Hash)


44
45
46
# File 'lib/uniword/lint/rule.rb', line 44

def finding(message:, path: nil)
  { rule: name, severity: severity, message: message, path: path }
end