Class: Marcdouane::Rules::Rule
- Inherits:
-
Object
- Object
- Marcdouane::Rules::Rule
- Extended by:
- Dry::Configurable
- Defined in:
- lib/marcdouane/rules/rule.rb
Overview
Rule is the base class to regroup all rules. It is initialized with a file path and some options forwarded from the CLI invocation.
Subclasses must implement check! and provide an
ERROR_MESSAGE when the check fails.
Direct Known Subclasses
ConsistentHeaderStyle, EnsureHeadersCascade, LineLength, NoConsecutiveBlankLines, SingleTopLevelHeader, StartWithTopLevelHeader
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#markdown ⇒ Object
readonly
Returns the value of attribute markdown.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#error!(machine_line_number, message = nil) ⇒ Object
Publishes an error event, picked up by the FileChecker somewhere down the line.
- #identifier ⇒ Object
-
#initialize(file, options) ⇒ Rule
constructor
A new instance of Rule.
- #line_number_from_byte_range(range) ⇒ Object
Constructor Details
#initialize(file, options) ⇒ Rule
Returns a new instance of Rule.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/marcdouane/rules/rule.rb', line 21 def initialize(file, ) @file = file @options = @markdown = Inkmark.new( File.read(file), options: { frontmatter: true } ) register_event("rule.error") end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
19 20 21 |
# File 'lib/marcdouane/rules/rule.rb', line 19 def file @file end |
#markdown ⇒ Object (readonly)
Returns the value of attribute markdown.
19 20 21 |
# File 'lib/marcdouane/rules/rule.rb', line 19 def markdown @markdown end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/marcdouane/rules/rule.rb', line 19 def @options end |
Instance Method Details
#error!(machine_line_number, message = nil) ⇒ Object
Publishes an error event, picked up by the FileChecker
somewhere down the line. It must be called with the 0-indexed
line-number, and an optional message override instead of the
class's ERROR_MESSAGE.
46 47 48 49 50 |
# File 'lib/marcdouane/rules/rule.rb', line 46 def error!(machine_line_number, = nil) msg = || self.class.const_get("ERROR_MESSAGE") publish("rule.error", msg: msg, line_number: machine_line_number + 1) end |
#identifier ⇒ Object
38 39 40 |
# File 'lib/marcdouane/rules/rule.rb', line 38 def identifier self.class.to_s.split("::").last end |
#line_number_from_byte_range(range) ⇒ Object
34 35 36 |
# File 'lib/marcdouane/rules/rule.rb', line 34 def line_number_from_byte_range(range) File.binread(file, range.first).count("\n") end |