Sha256: b9616136ad2305bb022921a3914d04c31b63cee822cb8c60006a42a16945a5ef

Contents?: true

Size: 824 Bytes

Versions: 9

Compression:

Stored size: 824 Bytes

Contents

require 'ipaddr'
require 'dm-core'

module DataMapper
  class Property
    class IPAddress < String

      length 39

      def primitive?(value)
        value.kind_of?(IPAddr)
      end

      def valid?(value, negated = false)
        super || dump(value).kind_of?(::String)
      end

      def load(value)
        if value.nil?
          nil
        elsif value.is_a?(::String)
          unless value.empty?
            IPAddr.new(value)
          else
            IPAddr.new("0.0.0.0")
          end
        else
          raise ArgumentError.new("+value+ must be nil or a String")
        end
      end

      def dump(value)
        value.to_s unless value.nil?
      end

      def typecast_to_primitive(value)
        load(value)
      end

    end # class IPAddress
  end # module Property
end # module DataMapper

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
ardm-types-1.2.3 lib/dm-types/ip_address.rb
ardm-types-1.2.2 lib/dm-types/ip_address.rb
dm-types-1.2.2 lib/dm-types/ip_address.rb
dm-types-1.2.1 lib/dm-types/ip_address.rb
dm-types-1.2.0 lib/dm-types/ip_address.rb
dm-types-1.2.0.rc2 lib/dm-types/ip_address.rb
dm-types-1.2.0.rc1 lib/dm-types/ip_address.rb
dm-types-1.1.0 lib/dm-types/ip_address.rb
dm-types-1.1.0.rc3 lib/dm-types/ip_address.rb