Class: IpNumeric

Inherits:
Object show all
Includes:
Comparable, IpAddresslike
Defined in:
lib/gorillib/type/ip_address.rb

Overview

Stores an IP address in numeric form.

IpNumeric instances are immutable, and memoize most of their methods.

Constant Summary

Constants included from IpAddresslike

IpAddresslike::ONES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IpAddresslike

#bitness_max, #bitness_min, #to_hex, #to_s

Constructor Details

#initialize(addr) ⇒ IpNumeric

Returns a new instance of IpNumeric.



79
80
81
# File 'lib/gorillib/type/ip_address.rb', line 79

def initialize(addr)
  @packed = addr.to_int
end

Class Method Details

.from_dotted(dotted) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/gorillib/type/ip_address.rb', line 106

def self.from_dotted(dotted)
  ip_a, ip_b, ip_c, ip_d = quads = dotted.split(".", 4).map(&:to_i)
  obj = new((ip_a << 24) + (ip_b << 16) + (ip_c << 8) + (ip_d))
  obj.instance_variable_set('@dotted', dotted.freeze)
  obj.instance_variable_set('@quads',  quads.freeze)
  obj
end

.from_packed(pi) ⇒ Object

=== class methods ===



102
103
104
# File 'lib/gorillib/type/ip_address.rb', line 102

def self.from_packed(pi)
  new(pi)
end

Instance Method Details

#+(int) ⇒ Object



87
# File 'lib/gorillib/type/ip_address.rb', line 87

def +(int)     ; self.class.new(to_int + int) ; end

#<=>(other) ⇒ Object



86
# File 'lib/gorillib/type/ip_address.rb', line 86

def <=>(other) ; packed <=> other.to_int ; end

#==(other) ⇒ Object



85
# File 'lib/gorillib/type/ip_address.rb', line 85

def ==(other)  ; packed  == other.to_int ; end

#dottedObject



92
93
94
# File 'lib/gorillib/type/ip_address.rb', line 92

def dotted
  @dotted ||= quads.join('.').freeze
end

#packedObject



90
# File 'lib/gorillib/type/ip_address.rb', line 90

def packed ; @packed ; end

#quadsObject



96
97
98
# File 'lib/gorillib/type/ip_address.rb', line 96

def quads
  @quads ||= [ (@packed >> 24) & 0xFF, (@packed >> 16) & 0xFF, (@packed >>  8) & 0xFF, (@packed) & 0xFF ].freeze
end

#receive(val) ⇒ Object



75
76
77
# File 'lib/gorillib/type/ip_address.rb', line 75

def receive(val)
  new(val)
end

#to_iObject



83
# File 'lib/gorillib/type/ip_address.rb', line 83

def to_i       ; packed ; end

#to_intObject



84
# File 'lib/gorillib/type/ip_address.rb', line 84

def to_int     ; packed ; end