Class: Uniword::Lint::Ruleset
- Inherits:
-
Object
- Object
- Uniword::Lint::Ruleset
- Defined in:
- lib/uniword/lint/ruleset.rb
Overview
Loads rules from a YAML ruleset or symbol registry. Each rule
has a type (which Rule subclass to instantiate) and
rule-specific options.
Example YAML:
rules:
- type: sentence_case_headings
severity: warning
- type: max_paragraph_length
severity: warning
max: 200
- type: banned_words
severity: warning
words: [really, very, basically]
Instance Attribute Summary collapse
- #rules ⇒ Array<Rule> readonly
Class Method Summary collapse
-
.build_rule(spec) ⇒ Rule
Build one rule from a spec hash by type name.
-
.from_hash(data) ⇒ Ruleset
Build a ruleset from a parsed YAML hash.
-
.load(path) ⇒ Ruleset
Load a ruleset from a YAML file.
Instance Method Summary collapse
-
#initialize(rules = []) ⇒ Ruleset
constructor
A new instance of Ruleset.
Constructor Details
#initialize(rules = []) ⇒ Ruleset
Returns a new instance of Ruleset.
24 25 26 |
# File 'lib/uniword/lint/ruleset.rb', line 24 def initialize(rules = []) @rules = rules end |
Instance Attribute Details
#rules ⇒ Array<Rule> (readonly)
29 30 31 |
# File 'lib/uniword/lint/ruleset.rb', line 29 def rules @rules end |
Class Method Details
.build_rule(spec) ⇒ Rule
Build one rule from a spec hash by type name.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/uniword/lint/ruleset.rb', line 53 def self.build_rule(spec) type = spec["type"].to_sym klass = Rule.types[type] raise ArgumentError, "unknown rule type: #{spec['type']}" unless klass kwargs = spec.transform_keys(&:to_sym) kwargs.delete(:type) kwargs[:name] = type unless kwargs.key?(:name) klass.new(**kwargs) end |
.from_hash(data) ⇒ Ruleset
Build a ruleset from a parsed YAML hash.
44 45 46 47 |
# File 'lib/uniword/lint/ruleset.rb', line 44 def self.from_hash(data) rule_list = (data["rules"] || []).map { |spec| build_rule(spec) } new(rule_list) end |
.load(path) ⇒ Ruleset
Load a ruleset from a YAML file.
35 36 37 38 |
# File 'lib/uniword/lint/ruleset.rb', line 35 def self.load(path) data = YAML.safe_load_file(path) from_hash(data || {}) end |