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.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/marcdouane/rules/rule.rb', line 21

def initialize(file, options)
  @file = file
  @options = options
  @markdown = Inkmark.new(
    File.read(file),
    options: {
      frontmatter: true
    }
  )

  register_event("rule.error")
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



19
20
21
# File 'lib/marcdouane/rules/rule.rb', line 19

def file
  @file
end

#markdownObject (readonly)

Returns the value of attribute markdown.



19
20
21
# File 'lib/marcdouane/rules/rule.rb', line 19

def markdown
  @markdown
end

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/marcdouane/rules/rule.rb', line 19

def options
  @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, message = nil)
  msg = message || self.class.const_get("ERROR_MESSAGE")

  publish("rule.error", msg: msg, line_number: machine_line_number + 1)
end

#identifierObject



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