Sha256: 120b1d499bab441cc4715a519e6a188fcaf2331338726ff21b5b50192547c612

Contents?: true

Size: 1.11 KB

Versions: 31

Compression:

Stored size: 1.11 KB

Contents

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)
            subnet_mask = value.instance_variable_get(:@mask_addr)

            # If the subnet mask is equal to /32, don't output it
            if subnet_mask == (2**32 - 1)
              "\"#{value}\""
            else
              "\"#{value}/#{subnet_mask.to_s(2).count('1')}\""
            end
          end

          def serialize(value)
            if IPAddr === value
              "#{value}/#{value.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
            else
              value
            end
          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

31 entries across 31 versions & 4 rubygems

Version Path
activerecord-5.0.7.2 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.7.1 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.7 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.6 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.6.rc1 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.5 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.5.rc2 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.5.rc1 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.4 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.4.rc1 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.3 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-5.0.2/lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.2 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
activerecord-5.0.2.rc1 lib/active_record/connection_adapters/postgresql/oid/cidr.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/oid/cidr.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/oid/cidr.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/oid/cidr.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/oid/cidr.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/oid/cidr.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/oid/cidr.rb