Sha256: 6e44e2f16cee4d5275ef4fdd1f7580a6d8bd8d9422b4892a1460b40677bac232

Contents?: true

Size: 738 Bytes

Versions: 12

Compression:

Stored size: 738 Bytes

Contents

require 'ipaddr'

module DataMapper
  module Types
    class IPAddress < DataMapper::Type
      primitive String

      def self.load(value, property)
        if value.nil?
          nil
        elsif value.is_a?(String) && !value.empty?
          IPAddr.new(value)
        elsif value.is_a?(String) && value.empty?
          IPAddr.new("0.0.0.0")
        else
          raise ArgumentError.new("+value+ must be nil or a String")
        end
      end

      def self.dump(value, property)
        return nil if value.nil?
        value.to_s
      end

      def self.typecast(value, property)
        value.kind_of?(IPAddr) ? value : load(value, property)
      end
    end # class IPAddress
  end # module Types
end # module DataMapper

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
dm-types-0.9.10 lib/dm-types/ip_address.rb
dm-types-0.9.6 lib/dm-types/ip_address.rb
dm-types-0.9.4 lib/dm-types/ip_address.rb
dm-types-0.9.3 lib/dm-types/ip_address.rb
dm-types-0.9.5 lib/dm-types/ip_address.rb
dm-types-0.9.11 lib/dm-types/ip_address.rb
dm-types-0.9.7 lib/dm-types/ip_address.rb
dm-types-0.9.8 lib/dm-types/ip_address.rb
dm-types-0.9.9 lib/dm-types/ip_address.rb
mack-data_mapper-0.8.2 lib/gems/dm-types-0.9.7/lib/dm-types/ip_address.rb
mack-data_mapper-0.8.3.1 lib/gems/dm-types-0.9.9/lib/dm-types/ip_address.rb
mack-data_mapper-0.8.3 lib/gems/dm-types-0.9.9/lib/dm-types/ip_address.rb