Class: PG::TextEncoder::Inet

Inherits:
SimpleEncoder show all
Defined in:
lib/pg/text_encoder/inet.rb

Constant Summary

Constants inherited from Coder

Coder::FORMAT_ERROR_MASK, Coder::FORMAT_ERROR_TO_PARTIAL, Coder::FORMAT_ERROR_TO_RAISE, Coder::FORMAT_ERROR_TO_STRING, Coder::TIMESTAMP_APP_LOCAL, Coder::TIMESTAMP_APP_UTC, Coder::TIMESTAMP_DB_LOCAL, Coder::TIMESTAMP_DB_UTC

Instance Attribute Summary

Attributes inherited from Coder

#name

Instance Method Summary collapse

Methods inherited from Coder

#==, #dup, #flags, #flags=, #format, #format=, #initialize, #inspect, #inspect_short, #marshal_dump, #marshal_load, #oid, #oid=, #to_h

Constructor Details

This class inherits a constructor from PG::Coder

Instance Method Details

#encode(value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pg/text_encoder/inet.rb', line 9

def encode(value)
	case value
	when IPAddr
		default_prefix = (value.family == Socket::AF_INET ? 32 : 128)
		s = value.to_s
		if value.respond_to?(:prefix)
			prefix = value.prefix
		else
			range = value.to_range
			prefix  = default_prefix - Math.log(((range.end.to_i - range.begin.to_i) + 1), 2).to_i
		end
		s << "/" << prefix.to_s if prefix != default_prefix
		s
	else
		value
	end
end