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.



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

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

Class Attribute Details

.alternatesObject (readonly)

Returns the value of attribute alternates.



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

def alternates
  @alternates
end

Instance Attribute Details

#applied_configObject (readonly)

Returns the value of attribute applied_config.



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

def applied_config
  @applied_config
end

Class Method Details

.choose_transport(klass, config) ⇒ Object



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

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

.default_modulesObject



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

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

.ipv6_address?(config) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



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

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



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

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

#validate_keys(config) ⇒ Object



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

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