Class: OSPFv2::Id

Inherits:
Object show all
Defined in:
lib/ie/id.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg = 0) ⇒ Id

Returns a new instance of Id.



40
41
42
# File 'lib/ie/id.rb', line 40

def initialize(arg=0)
  self.id = arg
end

Class Method Details

.new_ntoh(s) ⇒ Object



30
31
32
33
# File 'lib/ie/id.rb', line 30

def self.new_ntoh(s)
  return unless s.is_a?(String)
  new s.unpack('N')[0]
end

.to_i(id) ⇒ Object



35
36
37
38
# File 'lib/ie/id.rb', line 35

def self.to_i(id)
  return id.to_i unless id.is_a?(String) and id.split('.').size==4
  IPAddr.new(id).to_i
end

Instance Method Details

#encodeObject Also known as: enc



61
62
63
# File 'lib/ie/id.rb', line 61

def encode
  [@id].pack('N')
end

#id=(val) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ie/id.rb', line 44

def id=(val)
  if val.is_a?(Hash)
    @id = val[:id] if val[:id]
  elsif val.is_a?(IPAddr)
    @id = val.to_i
  elsif val.is_a?(Integer)
    raise ArgumentError, "Invalid Argument #{val}" unless (0..0xffffffff).include?(val)
    @id = val
  elsif val.is_a?(String)
    @id = IPAddr.new(val).to_i
  elsif val.is_a?(Id)
    @id = val.to_i
  else
    raise ArgumentError, "Invalid Argument #{val.inspect}"
  end
end

#to_hashObject



74
75
76
# File 'lib/ie/id.rb', line 74

def to_hash
  to_s_short
end

#to_iObject



66
67
68
# File 'lib/ie/id.rb', line 66

def to_i
  @id
end

#to_ipObject



78
79
80
# File 'lib/ie/id.rb', line 78

def to_ip
  to_s(false)
end

#to_s(verbose = true) ⇒ Object



70
71
72
# File 'lib/ie/id.rb', line 70

def to_s(verbose=true)
  verbose ? to_s_verbose : to_s_short
end