Class: SNMP::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp/options.rb

Overview

Helper class for processing options Hash.

Direct Known Subclasses

Manager::Config, TrapListener::Config

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Options

Returns a new instance of Options.



48
49
50
51
# File 'lib/snmp/options.rb', line 48

def initialize(config)
  @config = validate_keys(config)
  @applied_config = {}
end

Class Attribute Details

.alternatesObject (readonly)

Returns the value of attribute alternates.



18
19
20
# File 'lib/snmp/options.rb', line 18

def alternates
  @alternates
end

Instance Attribute Details

#applied_configObject (readonly)

Returns the value of attribute applied_config.



46
47
48
# File 'lib/snmp/options.rb', line 46

def applied_config
  @applied_config
end

Class Method Details

.choose_transport(klass, config) ⇒ Object



35
36
37
38
# File 'lib/snmp/options.rb', line 35

def choose_transport(klass, config)
  address_family = config.use_IPv6 ? Socket::AF_INET6 : Socket::AF_INET
  klass.new(address_family)
end

.default_modulesObject



31
32
33
# File 'lib/snmp/options.rb', line 31

def default_modules
  ["SNMPv2-SMI", "SNMPv2-MIB", "IF-MIB", "IP-MIB", "TCP-MIB", "UDP-MIB"]
end

.ipv6_address?(config) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/snmp/options.rb', line 40

def ipv6_address?(config)
  hostname = config.host.to_s
  hostname.include?("::") || hostname.split(":").size == 8
end

.option(symbol, alternate, defaulter = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/snmp/options.rb', line 20

def option(symbol, alternate, defaulter=nil)
  @alternates ||= {}
  @alternates[symbol] = alternate
  define_method(symbol) do
    alternate_symbol = self.class.alternates[symbol]
    option_value = @config[symbol] || @config[alternate_symbol] ||
      (defaulter.kind_of?(Proc) ? defaulter.call(self) : defaulter)
    @applied_config[symbol] = @applied_config[alternate_symbol] = option_value
  end
end

Instance Method Details

#socket_address_familyObject



59
60
61
# File 'lib/snmp/options.rb', line 59

def socket_address_family
  use_IPv6 ? Socket::AF_INET6 : Socket::AF_INET
end

#validate_keys(config) ⇒ Object



53
54
55
56
57
# File 'lib/snmp/options.rb', line 53

def validate_keys(config)
  valid_symbols = self.class.alternates.keys + self.class.alternates.values
  config.each_key { |k| raise "Invalid option: #{k}" unless valid_symbols.include? k }
  config
end