diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-06-02 08:58:12 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-06-02 08:58:12 +0200 |
commit | f17fa8ef6f2abffeea5398cc450172c0975cad27 (patch) | |
tree | d6ec8cd97ef8dddf52472a5251d1fc90cfe0ac13 /activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb | |
parent | 4c66ab2b2181a2c1d7163c24ce8aec0dbb3bef7b (diff) | |
download | rails-f17fa8ef6f2abffeea5398cc450172c0975cad27.tar.gz rails-f17fa8ef6f2abffeea5398cc450172c0975cad27.tar.bz2 rails-f17fa8ef6f2abffeea5398cc450172c0975cad27.zip |
pg, inline casting methods into `OID::Type` objects.
This inlines casting for the most obvious types. The rest will
follow eventually. I need to put some tests in place, to make sure
that the inlining is not causing regressions.
/cc @sgrif
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb index 158468fe5b..534961a414 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb @@ -18,8 +18,26 @@ module ActiveRecord end end + def type_cast_for_database(value) + if IPAddr === value + "#{value.to_s}/#{value.instance_variable_get(:@mask_addr).to_s(2).count('1')}" + else + value + end + end + def cast_value(value) - ConnectionAdapters::PostgreSQLColumn.string_to_cidr value + if value.nil? + nil + elsif String === value + begin + IPAddr.new(value) + rescue ArgumentError + nil + end + else + value + end end end end |