aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-05-21 21:47:06 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-05-21 21:47:06 -0300
commit62bcf81431baf84f7e4c370c1ae30fe07ee1022a (patch)
tree8aea31bbe496116240944654ed4465d12bd6d381 /activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb
parentb754d9b56d437933c1a0bcc333f8b55efd87af57 (diff)
parent30bf07d172f2764b27e887ff3a122ce3c08ff5fe (diff)
downloadrails-62bcf81431baf84f7e4c370c1ae30fe07ee1022a.tar.gz
rails-62bcf81431baf84f7e4c370c1ae30fe07ee1022a.tar.bz2
rails-62bcf81431baf84f7e4c370c1ae30fe07ee1022a.zip
Merge pull request #15218 from sgrif/sg-move-oid-types
Move PG OID types to their own files
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb56
1 files changed, 56 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb
new file mode 100644
index 0000000000..c2262c1599
--- /dev/null
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb
@@ -0,0 +1,56 @@
+module ActiveRecord
+ module ConnectionAdapters
+ module PostgreSQL
+ module OID # :nodoc:
+ class Range < Type::Value
+ attr_reader :subtype, :type
+
+ def initialize(subtype, type)
+ @subtype = subtype
+ @type = type
+ end
+
+ def extract_bounds(value)
+ from, to = value[1..-2].split(',')
+ {
+ from: (value[1] == ',' || from == '-infinity') ? @subtype.infinity(negative: true) : from,
+ to: (value[-2] == ',' || to == 'infinity') ? @subtype.infinity : to,
+ exclude_start: (value[0] == '('),
+ exclude_end: (value[-1] == ')')
+ }
+ end
+
+ def infinity?(value)
+ value.respond_to?(:infinite?) && value.infinite?
+ end
+
+ def type_cast_single(value)
+ infinity?(value) ? value : @subtype.type_cast(value)
+ end
+
+ def cast_value(value)
+ return if value == 'empty'
+ return value if value.is_a?(::Range)
+
+ extracted = extract_bounds(value)
+ from = type_cast_single extracted[:from]
+ to = type_cast_single extracted[:to]
+
+ if !infinity?(from) && extracted[:exclude_start]
+ if from.respond_to?(:succ)
+ from = from.succ
+ ActiveSupport::Deprecation.warn <<-MESSAGE
+Excluding the beginning of a Range is only partialy supported through `#succ`.
+This is not reliable and will be removed in the future.
+ MESSAGE
+ else
+ raise ArgumentError, "The Ruby Range object does not support excluding the beginning of a Range. (unsupported value: '#{value}')"
+ end
+ end
+ ::Range.new(from, to, extracted[:exclude_end])
+ end
+ end
+ end
+ end
+ end
+end