Class: MarkdownLint::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/mdl/ruleset.rb

Overview

defines a single rule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, description, fallback_docs: nil) ⇒ Rule

Returns a new instance of Rule.



6
7
8
9
10
11
12
13
14
15
# File 'lib/mdl/ruleset.rb', line 6

def initialize(id, description, fallback_docs: nil, &)
  @id = id
  @description = description
  @generate_docs = fallback_docs
  @docs_overridden = false
  @aliases = []
  @tags = []
  @params = {}
  instance_eval(&)
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/mdl/ruleset.rb', line 4

def description
  @description
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/mdl/ruleset.rb', line 4

def id
  @id
end

Instance Method Details

#aliases(*aliases) ⇒ Object



32
33
34
35
# File 'lib/mdl/ruleset.rb', line 32

def aliases(*aliases)
  @aliases.concat(aliases)
  @aliases
end

#check(&block) ⇒ Object



17
18
19
20
# File 'lib/mdl/ruleset.rb', line 17

def check(&block)
  @check = block unless block.nil?
  @check
end

#docs(url = nil, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/mdl/ruleset.rb', line 56

def docs(url = nil, &block)
  if block_given? != url.nil?
    raise ArgumentError, 'Give either a URL or a block, not both'
  end

  raise 'A docs url is already set within this rule' if @docs_overridden

  @generate_docs = block_given? ? block : lambda { |_, _| url }
  @docs_overridden = true
end

#docs_urlObject



67
68
69
# File 'lib/mdl/ruleset.rb', line 67

def docs_url
  @generate_docs&.call(id, description)
end

#fix(&block) ⇒ Object



22
23
24
25
# File 'lib/mdl/ruleset.rb', line 22

def fix(&block)
  @fix = block unless block.nil?
  @fix
end

#params(params = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mdl/ruleset.rb', line 37

def params(params = nil)
  unless params.nil?
    # Normalize string values to symbols where the rule's default is
    # a symbol AND the string looks like a simple identifier, so that
    # style files can use either :atx or "atx". Values like "---"
    # (MD035 hr style) are left as strings.
    params = params.each_with_object({}) do |(k, v), h|
      h[k] = if v.is_a?(String) && @params[k].is_a?(Symbol) &&
                v.match?(/\A[a-z][a-z_]*\z/)
               v.to_sym
             else
               v
             end
    end
    @params.update(params)
  end
  @params
end

#tags(*tags) ⇒ Object



27
28
29
30
# File 'lib/mdl/ruleset.rb', line 27

def tags(*tags)
  @tags = tags.flatten.map(&:to_sym) unless tags.empty?
  @tags
end