module Sequel::Postgres::InetDatabaseMethods

Methods enabling Database object integration with the inet/cidr types.

Public Class Methods

extended(db) click to toggle source

Reset the conversion procs when extending the Database object, so it will pick up the inet/cidr converter. Also, extend the datasets with support for literalizing the IPAddr types.

   # File lib/sequel/extensions/pg_inet.rb
40 def self.extended(db)
41   db.instance_exec do
42     extend_datasets(InetDatasetMethods)
43     meth = IPAddr.method(:new)
44     add_conversion_proc(869, meth)
45     add_conversion_proc(650, meth)
46     if respond_to?(:register_array_type)
47       register_array_type('inet', :oid=>1041, :scalar_oid=>869)
48       register_array_type('cidr', :oid=>651, :scalar_oid=>650)
49       register_array_type('macaddr', :oid=>1040, :scalar_oid=>829)
50     end
51     @schema_type_classes[:ipaddr] = IPAddr
52   end
53 end

Public Instance Methods

bound_variable_arg(arg, conn) click to toggle source

Convert an IPAddr arg to a string. Probably not necessary, but done for safety.

Calls superclass method
   # File lib/sequel/extensions/pg_inet.rb
57 def bound_variable_arg(arg, conn)
58   case arg
59   when IPAddr
60     "#{arg.to_s}/#{arg.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
61   else
62     super
63   end
64 end

Private Instance Methods

bound_variable_array(a) click to toggle source

Handle inet[]/cidr types in bound variables.

Calls superclass method
   # File lib/sequel/extensions/pg_inet.rb
69 def bound_variable_array(a)
70   case a
71   when IPAddr
72     "\"#{a.to_s}/#{a.instance_variable_get(:@mask_addr).to_s(2).count('1')}\""
73   else
74     super
75   end
76 end
schema_column_type(db_type) click to toggle source

Make the column type detection recognize the inet and cidr types.

Calls superclass method
   # File lib/sequel/extensions/pg_inet.rb
79 def schema_column_type(db_type)
80   case db_type
81   when 'inet', 'cidr'
82     :ipaddr
83   else
84     super
85   end
86 end
schema_post_process(_) click to toggle source

Set the :ruby_default value if the default value is recognized as an ip address.

Calls superclass method
   # File lib/sequel/extensions/pg_inet.rb
89 def schema_post_process(_)
90   super.each do |a|
91     h = a[1]
92     if h[:type] == :ipaddr && h[:default] =~ /\A'([:a-fA-F0-9\.\/]+)'::(?:inet|cidr)\z/
93       h[:ruby_default] = IPAddr.new($1)
94     end
95   end
96 end
typecast_value_ipaddr(value) click to toggle source

Typecast the given value to an IPAddr object.

    # File lib/sequel/extensions/pg_inet.rb
 99 def typecast_value_ipaddr(value)
100   case value
101   when IPAddr
102     value
103   when String
104     IPAddr.new(value)
105   else
106     raise Sequel::InvalidValue, "invalid value for inet/cidr: #{value.inspect}"
107   end
108 end