123456789_123456789_123456789_123456789_123456789_

Class: PG::TextEncoder::Inet

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: PG::SimpleEncoder
Defined in: lib/pg/text_encoder/inet.rb

Overview

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’.

Constant Summary

::PG::Coder - Inherited

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

Class Method Summary

::PG::Coder - Inherited

.new

Create a new coder object based on the attribute Hash.

Instance Attribute Summary

::PG::Coder - Inherited

#flags

Get current bitwise OR-ed coder flags.

#flags=

Set coder specific bitwise OR-ed flags.

#format

The format code that is sent alongside with an encoded query parameter value.

#format=

Specifies the format code that is sent alongside with an encoded query parameter value.

#name

Name of the coder or the corresponding data type.

#oid

The type OID that is sent alongside with an encoded query parameter value.

#oid=

Specifies the type OID that is sent alongside with an encoded query parameter value.

Instance Method Summary

::PG::Coder - Inherited

#==, #dup, #inspect, #inspect_short, #marshal_dump, #marshal_load,
#to_h

Returns coder attributes as Hash.

Constructor Details

This class inherits a constructor from PG::Coder

Instance Method Details

#encode(value)

[ GitHub ]

  
# File 'lib/pg/text_encoder/inet.rb', line 12

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