Sha256: b44c489adff074f235622c3e33ebc89306fda244862664a1ff6aa9aaaad9771c

Contents?: true

Size: 769 Bytes

Versions: 13

Compression:

Stored size: 769 Bytes

Contents

# -*- ruby -*-
# frozen_string_literal: true

require 'ipaddr'

module PG
	module TextEncoder
		# This is a encoder class for conversion of Ruby IPAddr values to PostgreSQL inet type.
		#
		# As soon as this class is used, it requires the ruby standard library 'ipaddr'.
		class Inet < SimpleEncoder
			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
		end
	end
end # module PG

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
pg-1.6.0.rc1-x86_64-linux lib/pg/text_encoder/inet.rb
pg-1.6.0.rc1-x86-mingw32 lib/pg/text_encoder/inet.rb
pg-1.6.0.rc1-x64-mingw32 lib/pg/text_encoder/inet.rb
pg-1.6.0.rc1-x64-mingw-ucrt lib/pg/text_encoder/inet.rb
pg-1.6.0.rc1 lib/pg/text_encoder/inet.rb
pg-1.5.9-x86-mingw32 lib/pg/text_encoder/inet.rb
pg-1.5.9-x64-mingw32 lib/pg/text_encoder/inet.rb
pg-1.5.9-x64-mingw-ucrt lib/pg/text_encoder/inet.rb
pg-1.5.9 lib/pg/text_encoder/inet.rb
pg-1.5.8-x64-mingw32 lib/pg/text_encoder/inet.rb
pg-1.5.8-x64-mingw-ucrt lib/pg/text_encoder/inet.rb
pg-1.5.8-x86-mingw32 lib/pg/text_encoder/inet.rb
pg-1.5.8 lib/pg/text_encoder/inet.rb