Class: Marcdouane::Rules::Rule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options)
  @file = file
  @options = options
  @markdown = Inkmark.new(
    File.read(file),
    options: {
      frontmatter: true
    }
  )
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



17
18
19
# File 'lib/marcdouane/rules/rule.rb', line 17

def file
  @file
end

#markdownObject (readonly)

Returns the value of attribute markdown.



17
18
19
# File 'lib/marcdouane/rules/rule.rb', line 17

def markdown
  @markdown
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/marcdouane/rules/rule.rb', line 17

def options
  @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.

Raises:



42
43
44
45
46
47
48
49
# File 'lib/marcdouane/rules/rule.rb', line 42

def error!(machine_line_number, message = nil)
  msg = message || self.class.const_get("ERROR_MESSAGE")

  raise Marcdouane::Error.new(
    "[#{identifier}] #{msg}",
    machine_line_number + 1
  )
end

#identifierObject



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