Module: NanDoc::RegexpEnhance

Defined in:
lib/nandoc/support/regexp-enhance.rb

Defined Under Namespace

Modules: MatchData

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.names(re, *list) ⇒ Object



14
15
16
17
18
19
# File 'lib/nandoc/support/regexp-enhance.rb', line 14

def names re, *list
  to(re) do |re|
    re.names(*list)
  end
  nil
end

.to(re, &block) ⇒ Object



20
21
22
23
24
25
# File 'lib/nandoc/support/regexp-enhance.rb', line 20

def to re, &block
  re.extend(self)
  re.regexp_enhance_init
  block.call(re)
  nil
end

Instance Method Details

#match_assert(str, name = nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/nandoc/support/regexp-enhance.rb', line 38

def match_assert str, name=nil
  match(str) or begin
    use_name = name || "/#{source}/"
    fail("#{use_name} failed to match #{str.inspect}")
  end
end

#names(*list) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/nandoc/support/regexp-enhance.rb', line 44

def names *list
  if list.any?
    @names = list
  else
    @names
  end
end

#regexp_enhance_initObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nandoc/support/regexp-enhance.rb', line 27

def regexp_enhance_init
  @names ||= []
  class << self
    alias_method :orig_match, :match
    def match str
      md = super
      MatchData.to(md, self) if md
      md
    end
  end
end