Class: PG::TextEncoder::Inet

Inherits:
SimpleEncoder show all
Defined in:
lib/pg/text_encoder.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

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

Constructor Details

This class inherits a constructor from PG::Coder

Instance Method Details

#encode(value) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pg/text_encoder.rb', line 49

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