Permalink
Browse files

Inet columns now deal with NetAddr::CIDR addresses

  • Loading branch information...
Dan Seaver
Dan Seaver committed May 4, 2012
1 parent 5a86f70 commit 96a7ae54cbd4066b416a8fd0997fafc44fc27e69
View
@@ -1,3 +1,5 @@
+require 'netaddr'
+
require 'postgres_ext/version'
require 'postgres_ext/active_record'
require 'postgres_ext/arel'
@@ -3,6 +3,40 @@
module ActiveRecord
module ConnectionAdapters
class PostgreSQLColumn
+ def klass
+ case type
+ when :inet, :cidr then NetAddr::CIDR
+ end
+ super
+ end
+ def type_cast(value)
+ #debugger
+ return nil if value.nil?
+ return coder.load(value) if encoded?
+
+ klass = self.class
+
+ case type
+ when :inet, :cidr then klass.string_to_cidr_address(value)
+ else super
+ end
+ end
+ def type_cast_code(var_name)
+ #debugger
+ klass = self.class.name
+
+ case type
+ when :inet then "#{klass}.string_to_cidr_address(#{var_name})"
+ else super
+ end
+ end
+
+ class << self
+ def string_to_cidr_address(string)
+ return string unless String === string
+ return NetAddr::CIDR.create(string)
+ end
+ end
private
def simplified_type_with_extended_types(field_type)
@@ -81,6 +115,16 @@ def type_to_sql_with_extended_types(type, limit = nil, precision = nil, scale =
column_type_sql
end
alias_method_chain :type_to_sql, :extended_types
+
+ def type_cast_with_extended_types(value, column)
+
+ case value
+ when NetAddr::CIDR
+ return value.to_s
+ else type_cast_without_extended_types(value,column)
@bcardarella

bcardarella May 4, 2012

Contributor

even though this is legal Ruby the method call should be indented:

else
  type_cast_without_extended_types(value, column)

And the parms should be separated by a space.

DO IT!!

+ end
+ end
+ alias_method_chain :type_cast, :extended_types
end
end
end
View
@@ -16,6 +16,7 @@ Gem::Specification.new do |gem|
gem.version = PostgresExt::VERSION
gem.add_dependency 'activerecord', '~> 3.2.0'
+ gem.add_dependency 'netaddr', '~> 1.5.0'
gem.add_development_dependency 'rails', '~> 3.2.0'
gem.add_development_dependency 'rspec-rails', '~> 2.9.0'
View
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+describe 'INET column' do
+ let!(:column) { ActiveRecord::ConnectionAdapters::PostgreSQLColumn.new 'field', nil, 'inet'}
+ let!(:adapter) { ActiveRecord::Base.connection }
+
+ describe '#type_class_code' do
+ it 'returns code for converting strings to NetAddr::CIDR' do
+ column.type_cast_code('v').should eq 'ActiveRecord::ConnectionAdapters::PostgreSQLColumn.string_to_cidr_address(v)'
+ end
+ end
+
+ describe 'inet type casting' do
+ it 'converts ip strings to NetAddr::CIDR objects' do
+ column.type_cast('127.0.0.1').should be_a_kind_of NetAddr::CIDR
+ end
+ end
+
+ describe 'inet value conversion for SQL statments' do
+ it 'returns the string value of the NetAddr::CIDR address' do
+ value = column.type_cast('127.0.0.1')
+ adapter.type_cast(value, column).should eq '127.0.0.1/32'
+ end
+ end
+end
View
@@ -21,8 +21,12 @@
t.inet_array "inet_arrayzerd"
t.string_array "str_arrayzerd", :limit => 5
t.string "test"
- t.timestamp "created_at", :null => false
- t.timestamp "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ create_table "test", :id => false, :force => true do |t|
+ t.string "test_a", :limit => nil
end
end

0 comments on commit 96a7ae5

Please sign in to comment.