Class: CommandReference::CmdRef
- Inherits:
-
Object
- Object
- CommandReference::CmdRef
- Defined in:
- lib/cisco_node_utils/command_reference.rb
Overview
Control a reference for an attribute.
Constant Summary collapse
- @@KEYS =
%w(default_value config_set config_set_append config_get config_get_token config_get_token_append test_config_get test_config_get_regex test_config_result)
Instance Attribute Summary collapse
-
#feature ⇒ Object
readonly
Returns the value of attribute feature.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
Instance Method Summary collapse
- #convert_to_constant(value) ⇒ Object
-
#initialize(feature, name, ref, source) ⇒ CmdRef
constructor
A new instance of CmdRef.
-
#merge(values, file) ⇒ Object
Overwrite values from more specific references.
- #method_missing(method_name, *args, &block) ⇒ Object
- #test_config_result(value) ⇒ Object
-
#to_s ⇒ Object
Print useful debugging information about the object.
-
#valid? ⇒ Boolean
Check that all necessary values have been populated.
Constructor Details
#initialize(feature, name, ref, source) ⇒ CmdRef
Returns a new instance of CmdRef.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cisco_node_utils/command_reference.rb', line 62 def initialize(feature, name, ref, source) raise ArgumentError, "'#{ref}' is not a hash." unless ref.is_a? Hash @feature = feature @name = name @hash = {} @sources = [] merge(ref, source) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/cisco_node_utils/command_reference.rb', line 119 def method_missing(method_name, *args, &block) super(method_name, *args, &block) unless @@KEYS.include?(method_name.to_s) method_name = method_name.to_s unless @hash.include?(method_name) raise IndexError, "No #{method_name} defined for #{@feature}, #{@name}" end # puts("get #{method_name}: '#{@hash[method_name]}'") @hash[method_name] end |
Instance Attribute Details
#feature ⇒ Object (readonly)
Returns the value of attribute feature.
52 53 54 |
# File 'lib/cisco_node_utils/command_reference.rb', line 52 def feature @feature end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
55 56 57 |
# File 'lib/cisco_node_utils/command_reference.rb', line 55 def hash @hash end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
53 54 55 |
# File 'lib/cisco_node_utils/command_reference.rb', line 53 def name @name end |
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
54 55 56 |
# File 'lib/cisco_node_utils/command_reference.rb', line 54 def sources @sources end |
Instance Method Details
#convert_to_constant(value) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cisco_node_utils/command_reference.rb', line 94 def convert_to_constant(value) # NOTE: This method is now deprecated and should not be used for future # development. # # If value is a string and it is empty OR the first letter is lower case # then leave value untouched. # If value is a string and the first letter is uppercase this indicates # that it could be a constant in Ruby so attempt to convert it to a Constant. if value.is_a?(String) and not value.empty? if value[0].chr == value[0].chr.upcase begin value = Object.const_get(value) if Object.const_defined?(value) rescue NameError # debug("#{value} looks like a constant but is not") end end end value end |
#merge(values, file) ⇒ Object
Overwrite values from more specific references.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/cisco_node_utils/command_reference.rb', line 74 def merge(values, file) values.each { |key, value| unless @@KEYS.include?(key) raise "Unrecognized key #{key} for #{feature}, #{name} in #{file}" end if value.nil? # Some attributes can store an explicit nil. # Others treat this as unset (allowing a platform to override common). if key == "default_value" @hash[key] = value else @hash.delete(key) end else @hash[key] = value end } @sources << file end |
#test_config_result(value) ⇒ Object
114 115 116 117 |
# File 'lib/cisco_node_utils/command_reference.rb', line 114 def test_config_result(value) result = @hash["test_config_result"][value] convert_to_constant(result) end |
#to_s ⇒ Object
Print useful debugging information about the object.
130 131 132 133 134 135 136 137 |
# File 'lib/cisco_node_utils/command_reference.rb', line 130 def to_s str = "" str << "Command: #{@feature} #{@name}\n" @hash.each { |key, value| str << " #{key}: #{value}\n" } str end |
#valid? ⇒ Boolean
Check that all necessary values have been populated.
140 141 142 143 |
# File 'lib/cisco_node_utils/command_reference.rb', line 140 def valid? return false unless @feature and @name true end |