Sha256: bfc753b768b660b38096c67dd90ed22bdf25bd1d6fce6bfba2bf6cf2b1228f0a

Contents?: true

Size: 568 Bytes

Versions: 1

Compression:

Stored size: 568 Bytes

Contents

require 'active_model/type'
require_relative 'ip_net'

module IpNetActiveRecordType
  class IpNetType < ActiveModel::Type::Value

    def cast_value(value)
      case value
      when IpNetActiveRecordType::IpNet
        value
      when IPAddr
        safe_typecast { IpNetActiveRecordType::IpNet.from_ipaddr(value) }
      else
        safe_typecast { IpNetActiveRecordType::IpNet.new(value) }
      end
    end

    def serialize(value)
      value&.to_s
    end

    private

    def safe_typecast
      yield
    rescue ArgumentError
      nil
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ip_net_active_record_type-2.1.2 lib/ip_net_active_record_type/ip_net_type.rb