Skip to content

Commit bdeb862

Browse files
committed
Simplify Postgres::PGRange#inspect output
This also simplifies Postgres::PGMultiRange#inspect output, since a multi range is implemented as a delegate class of Array containing PGRange.
1 parent 7a299a2 commit bdeb862

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== master
22

3+
* Simplify Postgres::PGRange#inspect output (jeremyevans)
4+
35
* Add deprecated_associations plugin, for warning/raising for deprecated association access (jeremyevans)
46

57
* Clear all cached schema information when using Database#drop_schema on PostgreSQL (jeremyevans)

lib/sequel/extensions/pg_range.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,17 @@ def exclude_end?
421421
@exclude_end
422422
end
423423

424+
# Support a friendly output
425+
def inspect
426+
range = if empty?
427+
"empty"
428+
else
429+
"#{@exclude_begin ? "(" : "["}#{@begin},#{@end}#{@exclude_end ? ")" : "]"}"
430+
end
431+
432+
"#<#{self.class.name} #{range}#{"::#{@db_type}" if @db_type}>"
433+
end
434+
424435
# Append a literalize version of the receiver to the sql.
425436
def sql_literal_append(ds, sql)
426437
if (s = @db_type) && !empty?

spec/extensions/pg_range_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ def default_timestamp_format
2424
endless_range_support = RUBY_VERSION >= '2.6'
2525
startless_range_support = RUBY_VERSION >= '2.7'
2626

27+
it "should have friendly #inspect output" do
28+
@R.new(1,2).inspect.must_equal "#<Sequel::Postgres::PGRange [1,2]>"
29+
@R.new(1,2, :exclude_begin=>true, :exclude_end=>true, :db_type=>'int4range').inspect.must_equal "#<Sequel::Postgres::PGRange (1,2)::int4range>"
30+
@R.new(nil, nil, :empty=>true).inspect.must_equal "#<Sequel::Postgres::PGRange empty>"
31+
@R.new(nil, nil, :empty=>true, :db_type=>'int4range').inspect.must_equal "#<Sequel::Postgres::PGRange empty::int4range>"
32+
end
33+
2734
it "should set up conversion procs correctly" do
2835
cp = @db.conversion_procs
2936
cp[3904].call("[1,2]").must_equal @R.new(1,2, :exclude_begin=>false, :exclude_end=>false, :db_type=>'int4range')

0 commit comments

Comments
 (0)