Class: Snmp2mkr::Oid

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp2mkr/oid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, mib: nil, name: nil) ⇒ Oid

Returns a new instance of Oid.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/snmp2mkr/oid.rb', line 11

def initialize(obj, mib: nil, name: nil)
  case obj
  when Array
    @ary = obj.map(&:to_i)
  when String
    if obj.include?('::')
      @name = obj
      @ary = mib.name_to_oid(obj)
    else
      @ary = obj.split('.').map(&:to_i)
    end
  end
  @str = @ary.map(&:to_s).join('.')

  case
  when name
    @name = name
  when mib
    @name ||= mib.oid_to_name(self)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/snmp2mkr/oid.rb', line 33

def name
  @name
end

Instance Method Details

#==(o) ⇒ Object



43
44
45
# File 'lib/snmp2mkr/oid.rb', line 43

def ==(o)
  self.class == o.class && self.to_s == o.to_s
end

#index_of(o) ⇒ Object

Raises:

  • (ArgumntError)


58
59
60
61
62
# File 'lib/snmp2mkr/oid.rb', line 58

def index_of(o)
  other = Snmp2mkr::Oid(o)
  raise ArgumntError, "#{o.inspect} is not subtree of #{self.inspect} " unless other.subtree?(self)
  self.to_a[other.to_a.size..-1].map(&:to_s).join('.')
end

#inspectObject



64
65
66
# File 'lib/snmp2mkr/oid.rb', line 64

def inspect
  "#<#{self.class}: #{to_s}#{@name && " (#{@name})"}>"
end

#subtree?(o) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/snmp2mkr/oid.rb', line 47

def subtree?(o)
  other = Snmp2mkr::Oid(o)
  self.to_a.size < other.to_a.size &&
    other.to_a[0,self.to_a.size] == self.to_a
end

#subtree_of?(o) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/snmp2mkr/oid.rb', line 53

def subtree_of?(o)
  other = Snmp2mkr::Oid(o)
  other.subtree?(self)
end

#to_aObject



35
36
37
# File 'lib/snmp2mkr/oid.rb', line 35

def to_a
  @ary
end

#to_sObject



39
40
41
# File 'lib/snmp2mkr/oid.rb', line 39

def to_s
  @str
end