Class: TextFSM::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/textfsm/pattern.rb

Overview

Compile Python-style template patterns into immutable Ruby matchers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Pattern

Returns a new instance of Pattern.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/textfsm/pattern.rb', line 11

def initialize(source)
  raise ArgumentError, "pattern must be a String" unless source.is_a?(String)

  @source = source.dup.freeze
  expression, @group_names = Compiler.new(@source).compile
  @regexp = Regexp.new("\\A(?:#{expression})").freeze
  @names = @group_names.keys.freeze
  freeze
rescue RegexpError, ArgumentError, RangeError => e
  raise TemplateError, "Invalid regular expression #{source.inspect}: #{e.message}"
end

Instance Attribute Details

#names ⇒ Object (readonly)

Returns the value of attribute names.



9
10
11
# File 'lib/textfsm/pattern.rb', line 9

def names
  @names
end

#regexp ⇒ Object (readonly)

Returns the value of attribute regexp.



9
10
11
# File 'lib/textfsm/pattern.rb', line 9

def regexp
  @regexp
end

#source ⇒ Object (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/textfsm/pattern.rb', line 9

def source
  @source
end

Instance Method Details

#match(text) ⇒ Object



23
24
25
# File 'lib/textfsm/pattern.rb', line 23

def match(text)
  @regexp.match(text)
end

#match?(text) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/textfsm/pattern.rb', line 27

def match?(text)
  @regexp.match?(text)
end

#named_captures(match) ⇒ Object



31
32
33
# File 'lib/textfsm/pattern.rb', line 31

def named_captures(match)
  @group_names.transform_values { |internal_name| match[internal_name] }
end