Sha256: d5c1d657c0c2793bef40d44123c6b98fc3c525d7cdc09ff2e577743efd96d762

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

require 'dm-core'

require 'ipaddr'

module DataMapper
  class Property
    module Legacy
      class NumericIPAddr < Integer

        #
        # Loads a numeric IP Address.
        #
        # @param [Integer, nil] value
        #   The numeric IP Address.
        #
        # @return [IPAddr, nil]
        #   The IP Address.
        #
        def load(value)
          load_integer(value) unless value.nil?
        end

        #
        # Typecasts an IP Address.
        #
        # @param [IPAddr, String, Integer, nil] value
        #   The IP Address.
        #
        # @return [IPAddr, nil]
        #   The typecasted IP Address.
        #
        def typecast(value)
          if value.kind_of?(::IPAddr)
            value
          elsif value.kind_of?(::String)
            ::IPAddr.new(value) unless value.empty?
          elsif value.kind_of?(::Integer)
            load_integer(value)
          end
        end

        #
        # Dumps an IP Address to a numeric value.
        #
        # @param [IPAddr, nil] value
        #   The IP Address.
        #
        # @return [Integer, nil]
        #   The numeric IP Address.
        #
        def dump(value)
          value.to_i unless value.nil?
        end

        protected

        #
        # Loads an IPv4 or IPv6 address from an integer.
        #
        # @param [Integer] value
        #   The numeric IP Address.
        #
        # @return [IPAddr]
        #   The IPv4 or IPv6 address.
        #
        def load_integer(value)
          if value > 4294967295 # (2 ** 32) - 1
            ::IPAddr.new(value,Socket::AF_INET6)
          elsif value >= 0
            ::IPAddr.new(value,Socket::AF_INET)
          end
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dm-types-legacy-0.1.1 lib/dm-core/property/legacy/numeric_ip_addr.rb