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


61
62
63
64
# File 'lib/snmp/options.rb', line 61

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

Class Attribute Details

.alternatesObject (readonly)

Returns the value of attribute alternates


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

def alternates
  @alternates
end

Instance Attribute Details

#applied_configObject (readonly)

Returns the value of attribute applied_config


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

def applied_config
  @applied_config
end

Class Method Details

.choose_transport(klass, config) ⇒ Object


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

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

.default_modulesObject


44
45
46
# File 'lib/snmp/options.rb', line 44

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

.ipv6_address?(config) ⇒ Boolean


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

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

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


33
34
35
36
37
38
39
40
41
42
# File 'lib/snmp/options.rb', line 33

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


72
73
74
# File 'lib/snmp/options.rb', line 72

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

#validate_keys(config) ⇒ Object


66
67
68
69
70
# File 'lib/snmp/options.rb', line 66

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