Sha256: 471d1531d02296cda755c1bbb70857ce316670a3a5c5c013f286106276dbc656

Contents?: true

Size: 680 Bytes

Versions: 4

Compression:

Stored size: 680 Bytes

Contents

# frozen_string_literal: true

require 'ipaddr'

module ROM
  module SQL
    module Postgres
      module Types
        IPAddress = Type('inet') do
          read = SQL::Types.Constructor(IPAddr) { |ip| ::IPAddr.new(ip.to_s) }

          SQL::Types.Constructor(::IPAddr, &:to_s).meta(read: read)
        end

        IPNetwork = Type('cidr') do
          read = SQL::Types.Constructor(IPAddr) do |ip|
            case ip
            when ::IPAddr
              ip
            when String
              ::IPAddr.new(ip)
            end
          end

          SQL::Types.Constructor(::IPAddr) { |ip| "#{ip}/#{ip.prefix}" }.meta(read: read)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rom-sql-3.6.4 lib/rom/sql/extensions/postgres/types/network.rb
rom-sql-3.6.3 lib/rom/sql/extensions/postgres/types/network.rb
rom-sql-3.6.2 lib/rom/sql/extensions/postgres/types/network.rb
rom-sql-3.6.1 lib/rom/sql/extensions/postgres/types/network.rb