Skip to content

Commit

Permalink
AR supporting new int4range and int8range data type on PostgreSQL >= …
Browse files Browse the repository at this point in the history
…9.2. Fix realization
  • Loading branch information
le0pard committed Dec 17, 2012
1 parent 9a4a095 commit 49182b8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ def string_to_intrange(string)
nil
elsif "empty" == string
(nil..nil)
elsif String === string
matches = /^(\(|\[)([0-9]+),(\s?)([0-9]+)(\)|\])$/i.match(string)
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)
Expand All @@ -108,8 +107,16 @@ def string_to_intrange(string)
end

def intrange_to_string(object)
if Range === object
"[#{object.first},#{object.exclude_end? ? object.last : object.last.to_i + 1})"
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
Expand Down
19 changes: 18 additions & 1 deletion activerecord/test/cases/adapters/postgresql/intrange_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,21 @@ def test_rewrite_to_nil
assert_equal(nil, x.int_range)
end

end
def test_invalid_intrange
assert IntRangeDataType.create!(int_range: ('a'..'d'))
x = IntRangeDataType.first
assert_equal(nil, x.int_range)
end

def test_save_empty_range
assert IntRangeDataType.create!(int_range: (nil..nil))
x = IntRangeDataType.first
assert_equal((nil..nil), x.int_range)
end

def test_save_invalid_data
assert_raises(ActiveRecord::StatementInvalid) do
IntRangeDataType.create!(int_range: "empty1")
end
end
end

0 comments on commit 49182b8

Please sign in to comment.