Module: IPAddressSerializer

Defined in:
lib/ip_address_serializer.rb

Class Method Summary collapse

Class Method Details

.dump(value) ⇒ String

Save the value, which converts either a String, or an IP Object to the String representation for storage

Parameters:

  • String (String, IP, IP::V4, IP::V6)

    representation, or internal IP representation of an IP Address

Returns:

  • (String)

    The encoded string value representing the IP address object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ip_address_serializer.rb', line 23

def self.dump(value)
	if value.nil?
		return nil
	end
	if value.is_a?(String)
		ip = IP.new(value)
	elsif (value.is_a?(IP) || value.is_a?(IP::V4) || value.is_a?(IP::V6))
		ip = value
	end
	return [ip.proto, ip.to_hex, ip.pfxlen].join(DELIMITER)
end

.load(value) ⇒ IP

Load the value and return the IP Object.

Parameters:

  • value (String)

    String representation of the IP Object

Returns:

  • (IP)

    The IP Object (from ruby-ip)



11
12
13
14
15
16
17
18
# File 'lib/ip_address_serializer.rb', line 11

def self.load(value)
	if value.nil?
		return nil
	end
	if value.is_a?(String) && value.index(DELIMITER) > 0
		return IP.new(value.split(DELIMITER))
	end
end