Class: Cisco::ConfigParser::Configuration
- Inherits:
-
Object
- Object
- Cisco::ConfigParser::Configuration
- Defined in:
- lib/cisco_node_utils/configparser_lib.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
Class Method Summary collapse
-
.build_min_config_hash(current, must, min_config = {}) ⇒ Object
build_min_config_hash.
-
.config_hash_to_str(cmd_hash, str = "") ⇒ Object
build_min_config_hash.
Instance Method Summary collapse
-
#compare_with(config) ⇒ String
Compare ConfigParser::Configuration objects.
-
#include_command?(command) ⇒ Boolean
Check command [Array] for test command.
-
#initialize(config_str) ⇒ Configuration
constructor
Constructor for Configuration.
-
#mode_configuration ⇒ Array
Containing command with leading/trailing whitespace removed.
-
#submode_config(config_line) ⇒ ConfigParser::Configuration
Fetch ConfigParser::Configuration object containing config_line.
Constructor Details
#initialize(config_str) ⇒ Configuration
Constructor for Configuration
40 41 42 43 44 45 46 47 |
# File 'lib/cisco_node_utils/configparser_lib.rb', line 40 def initialize(config_str) raise ArgumentError, "Argument is not a String." unless config_str.kind_of? String @configuration = {} @ordered_keys = [] @indent = "" parse(config_str) end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
34 35 36 |
# File 'lib/cisco_node_utils/configparser_lib.rb', line 34 def configuration @configuration end |
Class Method Details
.build_min_config_hash(current, must, min_config = {}) ⇒ Object
build_min_config_hash
Build a config hash of the minimum keys that would be needed to update current config to all of the changes in the “must” config. Each hash key is a configuration command; some keys have subconfigs which must be checked before dismissing top-level keys as present. This method is used primarily by the free-form command_config providers.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cisco_node_utils/configparser_lib.rb', line 62 def Configuration.build_min_config_hash(current, must, min_config={}) return {} if must.empty? # base case must.each { |k, v| # check each must{k} is present in current{} if current.key?(k) # if cmd is in current then compare subconfig min_config[k] = Configuration.new("") min_config[k].configuration = build_min_config_hash(current[k].configuration, v.configuration, {}) if min_config[k].configuration.empty? # no differing subconfigs, so empty hash is returned min_config.delete(k) end else # command NOT in current, apply it + all subcommands min_config[k] = v end } min_config end |
.config_hash_to_str(cmd_hash, str = "") ⇒ Object
build_min_config_hash
81 82 83 84 85 86 87 88 |
# File 'lib/cisco_node_utils/configparser_lib.rb', line 81 def Configuration.config_hash_to_str(cmd_hash, str="") return "" if cmd_hash.empty? cmd_hash.each { |k, v| str += k + "\n" str += config_hash_to_str(v.configuration, "") } str end |
Instance Method Details
#compare_with(config) ⇒ String
Compare ConfigParser::Configuration objects
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/cisco_node_utils/configparser_lib.rb', line 106 def compare_with(config) return nil if config.nil? existing = "" @ordered_keys.each { |config_line| command = config_line.strip submode = @configuration[command] raise StopIteration, "Could not find submode." if submode.nil? if special_command?(command) # match special exit/end command existing << config_line break elsif config.include_command?(command) # match whole command existing << config_line config_submode = config.submode_config(command) existing << submode.compare_with(config_submode) next end # if prefix, base = base_commands(command) if prefix != "" and !config.include_command?(base) existing << config_line next end } existing end |
#include_command?(command) ⇒ Boolean
Check command [Array] for test command
145 146 147 148 |
# File 'lib/cisco_node_utils/configparser_lib.rb', line 145 def include_command?(command) commands = mode_configuration() commands.include?(command) end |
#mode_configuration ⇒ Array
Returns containing command with leading/trailing whitespace removed.
137 138 139 |
# File 'lib/cisco_node_utils/configparser_lib.rb', line 137 def mode_configuration @ordered_keys.collect(&:strip) end |
#submode_config(config_line) ⇒ ConfigParser::Configuration
Fetch ConfigParser::Configuration object containing config_line
188 189 190 191 |
# File 'lib/cisco_node_utils/configparser_lib.rb', line 188 def submode_config(config_line) command = config_line.strip @configuration[command] end |