Class: PG::TextEncoder::Inet
- Inherits:
-
SimpleEncoder
- Object
- Coder
- SimpleCoder
- SimpleEncoder
- PG::TextEncoder::Inet
- 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
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
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
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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 |