Class: Asciidoctor::Defmastership::RegexpDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor-defmastership/regexp_dispatcher.rb

Overview

Hosts several Text replacement rules

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ RegexpDispatcher

Returns a new instance of RegexpDispatcher.

Parameters:

  • client (Object)

    the object that will receive method call on regexp matches



19
20
21
22
# File 'lib/asciidoctor-defmastership/regexp_dispatcher.rb', line 19

def initialize(client)
  @client = client
  @rules = []
end

Instance Method Details

#add_rule(regexp, method_symbol) ⇒ RegexpDispatcher

Add a rule

Parameters:

  • regexp (Regexp)

    the regexp for this rule

  • method_symbol (Symbol)

    the symbol for the method call

Returns:



29
30
31
32
# File 'lib/asciidoctor-defmastership/regexp_dispatcher.rb', line 29

def add_rule(regexp, method_symbol)
  @rules << Rule.new(regexp, method_symbol)
  self
end

#replace(line) ⇒ Array<String>

Add a rule

Parameters:

  • line (String)

    the original line

Returns:

  • (Array<String>)

    the lines to replace the original line



38
39
40
41
42
43
44
# File 'lib/asciidoctor-defmastership/regexp_dispatcher.rb', line 38

def replace(line)
  @rules.each do |rule|
    matches = rule.regexp.match(line)
    return @client.public_send(rule.method_symbol, line, matches) if matches
  end
  [line]
end