Class: TextFSM::IndexTable

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/textfsm/index_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, transform: nil, compile: nil) ⇒ IndexTable

Returns a new instance of IndexTable.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/textfsm/index_table.rb', line 15

def initialize(path, transform: nil, compile: nil)
  @header, @rows = Data.copy(read_table(path, transform), immutable: true)
  @patterns = @rows.map do |row|
    row.to_h do |key, value|
      value = compile.call(key, value) if compile
      [key, value.nil? || value.empty? ? nil : Pattern.new(value)]
    end.freeze
  end.freeze
  freeze
rescue TemplateError => e
  raise IndexError, "#{path}: #{e.message}"
end

Instance Attribute Details

#header ⇒ Object (readonly)

Returns the value of attribute header.



13
14
15
# File 'lib/textfsm/index_table.rb', line 13

def header
  @header
end

Instance Method Details

#[](index) ⇒ Object



43
44
45
# File 'lib/textfsm/index_table.rb', line 43

def [](index)
  @rows[index]
end

#each ⇒ Object



28
29
30
31
32
33
# File 'lib/textfsm/index_table.rb', line 28

def each(&)
  return enum_for(__method__) { size } unless block_given?

  @rows.each(&)
  self
end

#empty? ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/textfsm/index_table.rb', line 39

def empty?
  @rows.empty?
end

#match(attributes) ⇒ Object

Empty cells are wildcards. Attributes absent from the index are ignored.



48
49
50
51
52
53
54
55
56
# File 'lib/textfsm/index_table.rb', line 48

def match(attributes)
  index = @patterns.index do |patterns|
    attributes.all? do |key, value|
      pattern = patterns[key.to_s]
      pattern.nil? || pattern.match?(value.to_s)
    end
  end
  @rows[index] if index
end

#size ⇒ Object



35
36
37
# File 'lib/textfsm/index_table.rb', line 35

def size
  @rows.size
end