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, 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
Produces an error that will be collected and output by the CLI.
- #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.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/marcdouane/rules/rule.rb', line 19 def initialize(file, ) @file = file @options = @markdown = Inkmark.new( File.read(file), options: { frontmatter: true } ) end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
17 18 19 |
# File 'lib/marcdouane/rules/rule.rb', line 17 def file @file end |
#markdown ⇒ Object (readonly)
Returns the value of attribute markdown.
17 18 19 |
# File 'lib/marcdouane/rules/rule.rb', line 17 def markdown @markdown end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/marcdouane/rules/rule.rb', line 17 def @options end |
Instance Method Details
#error!(machine_line_number, message = nil) ⇒ Object
Produces an error that will be collected and output by the
CLI. It must be called with the 0-indexed line-number, and an
optional message override instead of the class's
ERROR_MESSAGE.
42 43 44 45 46 47 48 49 |
# File 'lib/marcdouane/rules/rule.rb', line 42 def error!(machine_line_number, = nil) msg = || self.class.const_get("ERROR_MESSAGE") raise Marcdouane::Error.new( "[#{identifier}] #{msg}", machine_line_number + 1 ) end |
#identifier ⇒ Object
34 35 36 |
# File 'lib/marcdouane/rules/rule.rb', line 34 def identifier self.class.to_s.split("::").last end |
#line_number_from_byte_range(range) ⇒ Object
30 31 32 |
# File 'lib/marcdouane/rules/rule.rb', line 30 def line_number_from_byte_range(range) File.binread(file, range.first).count("\n") end |