Sha256: 51deed185e0f8aa664ead003866f6737d017c46a63c1d76a81a65ca58917e84d

Contents?: true

Size: 1.28 KB

Versions: 11

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require "ipaddr"

module ActiveRecord
  module ConnectionAdapters
    module PostgreSQL
      module OID # :nodoc:
        class Cidr < Type::Value # :nodoc:
          def type
            :cidr
          end

          def type_cast_for_schema(value)
            # If the subnet mask is equal to /32, don't output it
            if value.prefix == 32
              "\"#{value}\""
            else
              "\"#{value}/#{value.prefix}\""
            end
          end

          def serialize(value)
            if IPAddr === value
              "#{value}/#{value.prefix}"
            else
              value
            end
          end

          # TODO: Remove when IPAddr#== compares IPAddr#prefix. See
          # https://github.com/ruby/ipaddr/issues/21
          def changed?(old_value, new_value, _new_value_before_type_cast)
            super || !old_value.nil? && old_value.prefix != new_value.prefix
          end

          def cast_value(value)
            if value.nil?
              nil
            elsif String === value
              begin
                IPAddr.new(value)
              rescue ArgumentError
                nil
              end
            else
              value
            end
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
activerecord-7.2.1.2 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-7.1.4.2 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-7.2.1.1 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-7.1.4.1 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-7.1.4 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-7.2.1 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-7.2.0 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-7.2.0.rc1 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-7.2.0.beta3 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-7.2.0.beta2 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-7.2.0.beta1 lib/active_record/connection_adapters/postgresql/oid/cidr.rb