Class: ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Cidr

Inherits:
Type::Value
  • Object
show all
Defined in:
lib/active_record/connection_adapters/postgresql/oid/cidr.rb

Overview

:nodoc:

Direct Known Subclasses

Inet

Instance Attribute Summary

Attributes inherited from Type::Value

#limit, #precision, #scale

Instance Method Summary collapse

Methods inherited from Type::Value

#==, #binary?, #changed?, #changed_in_place?, #hash, #initialize, #klass, #number?, #text?, #type_cast_from_database, #type_cast_from_user

Constructor Details

This class inherits a constructor from ActiveRecord::Type::Value

Instance Method Details

#cast_value(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_record/connection_adapters/postgresql/oid/cidr.rb', line 29

def cast_value(value)
  if value.nil?
    nil
  elsif String === value
    begin
      IPAddr.new(value)
    rescue ArgumentError
      nil
    end
  else
    value
  end
end

#typeObject



6
7
8
# File 'lib/active_record/connection_adapters/postgresql/oid/cidr.rb', line 6

def type
  :cidr
end

#type_cast_for_database(value) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/active_record/connection_adapters/postgresql/oid/cidr.rb', line 21

def type_cast_for_database(value)
  if IPAddr === value
    "#{value}/#{value.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
  else
    value
  end
end

#type_cast_for_schema(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/active_record/connection_adapters/postgresql/oid/cidr.rb', line 10

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