aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2012-12-18 16:29:00 +0100
committerDavid Heinemeier Hansson <david@loudthinking.com>2012-12-18 16:29:00 +0100
commit09aeee7247682178868241d446051696570e6e12 (patch)
tree51f4af1494145068345806ab21d2c38578bf6947 /activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
parentf6bbc3f582bfc16da3acc152c702b04102fcab81 (diff)
parentf6d0eda918756bf56e2c05afcaaabda723487017 (diff)
downloadrails-09aeee7247682178868241d446051696570e6e12.tar.gz
rails-09aeee7247682178868241d446051696570e6e12.tar.bz2
rails-09aeee7247682178868241d446051696570e6e12.zip
Merge
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql/cast.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/cast.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
index c04a799b8d..f7d734a2f1 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
@@ -92,6 +92,36 @@ module ActiveRecord
parse_pg_array(string).map{|val| oid.type_cast val}
end
+ def string_to_intrange(string)
+ if string.nil?
+ nil
+ elsif "empty" == string
+ (nil..nil)
+ elsif String === string && (matches = /^(\(|\[)([0-9]+),(\s?)([0-9]+)(\)|\])$/i.match(string))
+ lower_bound = ("(" == matches[1] ? (matches[2].to_i + 1) : matches[2].to_i)
+ upper_bound = (")" == matches[5] ? (matches[4].to_i - 1) : matches[4].to_i)
+ (lower_bound..upper_bound)
+ else
+ string
+ end
+ end
+
+ def intrange_to_string(object)
+ if object.nil?
+ nil
+ elsif Range === object
+ if [object.first, object.last].all? { |el| Integer === el }
+ "[#{object.first.to_i},#{object.exclude_end? ? object.last.to_i : object.last.to_i + 1})"
+ elsif [object.first, object.last].all? { |el| NilClass === el }
+ "empty"
+ else
+ nil
+ end
+ else
+ object
+ end
+ end
+
private
HstorePair = begin