Class: ASAConsole::Config
- Inherits:
-
Object
- Object
- ASAConsole::Config
- Defined in:
- lib/asa_console/config.rb
Overview
A parser for ASA running/startup config.
Each instance represents a single config entry which may include nested lines of config that can be queried with #select or #select_all.
Instance Attribute Summary collapse
-
#config_data ⇒ String?
readonly
Remainder of the config line following the #keystr and #config_name.
-
#config_name ⇒ String?
readonly
An identifier, such as an access list name, used to distinguish between config entries of the same type.
-
#keystr ⇒ String?
readonly
Key used to select the line of config represented by this object, or
nilif the object contains nested top-level config.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
trueif there is no nested config, orfalseotherwise. -
#initialize(opts = {}) ⇒ Config
constructor
A new instance of Config.
-
#line ⇒ String?
The selected line, or
nilif this is a top-level object. -
#names_of(keystr) ⇒ Array<String>
A unique list of config element names matched by
keystr. -
#negated? ⇒ Boolean
trueif the selected line began with "no", orfalseotherwise. -
#select(keystr = nil, config_name = nil) {|config| ... } ⇒ Config
Select the first matching line of the nested config or
yieldall matching lines if a block is given. -
#select_all {|config| ... } ⇒ Array<Config>
Select all lines of nested config.
Constructor Details
#initialize(opts = {}) ⇒ Config
Returns a new instance of Config.
60 61 62 63 64 65 66 |
# File 'lib/asa_console/config.rb', line 60 def initialize(opts = {}) @keystr = opts[:keystr] @config_name = opts[:config_name] @config_data = opts[:config_data] @negated = opts.fetch(:negated, false) @nested_config = opts.fetch(:nested_config, '') end |
Instance Attribute Details
#config_data ⇒ String? (readonly)
Remainder of the config line following the #keystr and #config_name.
47 48 49 |
# File 'lib/asa_console/config.rb', line 47 def config_data @config_data end |
#config_name ⇒ String? (readonly)
An identifier, such as an access list name, used to distinguish between config entries of the same type.
47 48 49 |
# File 'lib/asa_console/config.rb', line 47 def config_name @config_name end |
#keystr ⇒ String? (readonly)
Key used to select the line of config represented by this object, or nil
if the object contains nested top-level config.
47 48 49 |
# File 'lib/asa_console/config.rb', line 47 def keystr @keystr end |
Instance Method Details
#empty? ⇒ Boolean
Returns true if there is no nested config, or false otherwise.
87 88 89 |
# File 'lib/asa_console/config.rb', line 87 def empty? @nested_config.empty? end |
#line ⇒ String?
Returns the selected line, or nil if this is a top-level object.
70 71 72 73 74 75 76 77 |
# File 'lib/asa_console/config.rb', line 70 def line parts = [] parts << 'no' if @negated parts << @keystr if @keystr parts << @config_name if @config_name parts << @config_data if @config_data parts.empty? ? nil : parts.join(' ') end |
#names_of(keystr) ⇒ Array<String>
Returns a unique list of config element names matched by keystr.
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/asa_console/config.rb', line 159 def names_of(keystr) names = [] regex = /^(?:no )?#{Regexp.escape(keystr)} (?<name>\S+)/ @nested_config.each_line do |line| m = regex.match(line) next unless m names << m[:name] unless names.include? m[:name] end names end |
#negated? ⇒ Boolean
Returns true if the selected line began with "no", or false otherwise.
81 82 83 |
# File 'lib/asa_console/config.rb', line 81 def negated? @negated ? true : false end |
#select(keystr = nil, config_name = nil) {|config| ... } ⇒ Config
Select the first matching line of the nested config or yield all
matching lines if a block is given.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/asa_console/config.rb', line 117 def select(keystr = nil, config_name = nil) prefix = [keystr, config_name].join(' ').strip regex = /^(?<no>no )?#{Regexp.escape(prefix)} ?(?<data>.+)?/ io = StringIO.open(@nested_config) lines = io.readlines io.close loop do break if lines.empty? m = regex.match(lines.shift) next unless m nested_config = '' loop do break unless lines[0] && lines[0].start_with?(' ') nested_config << lines.shift.sub(/^ /, '') end config = Config.new( keystr: keystr, config_name: config_name, config_data: m[:data], negated: !m[:no].nil?, nested_config: nested_config ) if block_given? yield config else return config end end nil end |
#select_all {|config| ... } ⇒ Array<Config>
Select all lines of nested config. Equivalent to #select with no arguments.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/asa_console/config.rb', line 97 def select_all result = [] select do |config| if block_given? yield config else result << config end end result unless block_given? end |