Class: TextFSM::Table
- Inherits:
-
Object
- Object
- TextFSM::Table
- Includes:
- Enumerable
- Defined in:
- lib/textfsm/table.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(header = [], rows = []) ⇒ Table
constructor
A new instance of Table.
-
#merge(other, keys: []) ⇒ Object
Keep left rows, add new columns, and use the first matching right row.
- #merge!(other, keys: []) ⇒ Object
- #size ⇒ Object
- #sort! ⇒ Object
- #sort_by! ⇒ Object
- #to_a ⇒ Object
- #to_hashes ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(header = [], rows = []) ⇒ Table
Returns a new instance of Table.
11 12 13 14 15 16 17 18 |
# File 'lib/textfsm/table.rb', line 11 def initialize(header = [], rows = []) raise ArgumentError, "Header and rows must be Arrays" unless header.is_a?(Array) && rows.is_a?(Array) && rows.all?(Array) raise ArgumentError, "Duplicate table columns" unless header.uniq == header raise ArgumentError, "Rows must have one value per column" unless rows.all? { |row| row.size == header.size } @header = Data.copy(header, immutable: true) @rows = Data.copy(rows, immutable: true) end |
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
9 10 11 |
# File 'lib/textfsm/table.rb', line 9 def header @header end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
9 10 11 |
# File 'lib/textfsm/table.rb', line 9 def rows @rows end |
Instance Method Details
#[](index) ⇒ Object
35 36 37 |
# File 'lib/textfsm/table.rb', line 35 def [](index) @rows[index] end |
#each ⇒ Object
20 21 22 23 24 25 |
# File 'lib/textfsm/table.rb', line 20 def each(&) return enum_for(__method__) { size } unless block_given? @rows.each(&) self end |
#empty? ⇒ Boolean
31 32 33 |
# File 'lib/textfsm/table.rb', line 31 def empty? @rows.empty? end |
#merge(other, keys: []) ⇒ Object
Keep left rows, add new columns, and use the first matching right row. Without keys, align by position. Missing matches receive empty values.
53 54 55 |
# File 'lib/textfsm/table.rb', line 53 def merge(other, keys: []) dup.merge!(other, keys: keys) end |
#merge!(other, keys: []) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/textfsm/table.rb', line 57 def merge!(other, keys: []) missing = (keys - @header) | (keys - other.header) raise KeyError, "Unknown key columns: #{missing.join(', ')}" unless missing.empty? columns = other.header - @header return self if columns.empty? right_columns = columns.map { |column| other.header.index(column) } left_keys = keys.map { |key| @header.index(key) } right_keys = keys.map { |key| other.header.index(key) } lookup = {} unless keys.empty? other.each do |row| lookup[row.values_at(*right_keys)] ||= row end end rows = @rows.each_with_index.map do |row, index| right = keys.empty? ? other[index] : lookup[row.values_at(*left_keys)] row + (right ? right.values_at(*right_columns) : Array.new(columns.size, "")) end header = Data.copy(@header + columns, immutable: true) rows = Data.copy(rows, immutable: true) @header = header @rows = rows self end |
#size ⇒ Object
27 28 29 |
# File 'lib/textfsm/table.rb', line 27 def size @rows.size end |
#sort! ⇒ Object
84 85 86 87 |
# File 'lib/textfsm/table.rb', line 84 def sort!(&) @rows = @rows.sort(&).freeze self end |
#sort_by! ⇒ Object
89 90 91 92 93 94 |
# File 'lib/textfsm/table.rb', line 89 def sort_by!(&) return enum_for(__method__) { size } unless block_given? @rows = @rows.sort_by(&).freeze self end |
#to_a ⇒ Object
39 40 41 |
# File 'lib/textfsm/table.rb', line 39 def to_a Data.copy(@rows) end |
#to_hashes ⇒ Object
43 44 45 |
# File 'lib/textfsm/table.rb', line 43 def to_hashes to_a.map { |row| @header.zip(row).to_h } end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/textfsm/table.rb', line 47 def to_s ([@header] + @rows).map { |row| "#{row.join(', ')}\n" }.join end |