:nocov:
Methods
Public Class
Public Instance
Included modules
- Sequel::SQL::AliasMethods
- Enumerable
Classes and Modules
Constants
ENDLESS_RANGE_NOT_SUPPORTED | = | RUBY_VERSION < '2.6' | ||
STARTLESS_RANGE_NOT_SUPPORTED | = | RUBY_VERSION < '2.7' |
Public Instance Aliases
== | -> | eql? |
Attributes
Public Class methods
Create an empty PGRange
with the given database type.
# File lib/sequel/extensions/pg_range.rb 325 def self.empty(db_type=nil) 326 new(nil, nil, :empty=>true, :db_type=>db_type) 327 end
Create a new PGRange
instance using the beginning and ending of the ruby Range
, with the given db_type.
# File lib/sequel/extensions/pg_range.rb 320 def self.from_range(range, db_type=nil) 321 new(range.begin, range.end, :exclude_end=>range.exclude_end?, :db_type=>db_type) 322 end
Initialize a new PGRange
instance. Accepts the following options:
:db_type |
The PostgreSQL database type for the range. |
:empty |
Whether the range is empty (has no points) |
:exclude_begin |
Whether the beginning element is excluded from the range. |
:exclude_end |
Whether the ending element is excluded from the range. |
# File lib/sequel/extensions/pg_range.rb 335 def initialize(beg, en, opts=OPTS) 336 @begin = beg 337 @end = en 338 @empty = !!opts[:empty] 339 @exclude_begin = !!opts[:exclude_begin] 340 @exclude_end = !!opts[:exclude_end] 341 @db_type = opts[:db_type] 342 if @empty 343 raise(Error, 'cannot have an empty range with either a beginning or ending') unless @begin.nil? && @end.nil? && opts[:exclude_begin].nil? && opts[:exclude_end].nil? 344 end 345 end
Public Instance methods
Allow PGRange
values in case statements, where they return true if they are equal to each other using eql?, or if this PGRange
can be converted to a Range
, delegating to that range.
# File lib/sequel/extensions/pg_range.rb 405 def ===(other) 406 if eql?(other) 407 true 408 else 409 if valid_ruby_range? 410 to_range === other 411 else 412 false 413 end 414 end 415 end
Return whether the value is inside the range.
# File lib/sequel/extensions/pg_range.rb 354 def cover?(value) 355 return false if empty? 356 b = self.begin 357 return false if b && b.public_send(exclude_begin? ? :>= : :>, value) 358 e = self.end 359 return false if e && e.public_send(exclude_end? ? :<= : :<, value) 360 true 361 end
Whether this range is empty (has no points). Note that for manually created ranges (ones not retrieved from the database), this will only be true if the range was created using the :empty option.
# File lib/sequel/extensions/pg_range.rb 420 def empty? 421 @empty 422 end
Consider the receiver equal to other PGRange
instances with the same beginning, ending, exclusions, and database type. Also consider it equal to Range
instances if this PGRange
can be converted to a a Range
and those ranges are equal.
# File lib/sequel/extensions/pg_range.rb 367 def eql?(other) 368 case other 369 when PGRange 370 if db_type == other.db_type 371 if empty? 372 other.empty? 373 elsif other.empty? 374 false 375 else 376 [:@begin, :@end, :@exclude_begin, :@exclude_end].all?{|v| instance_variable_get(v) == other.instance_variable_get(v)} 377 end 378 else 379 false 380 end 381 when Range 382 if valid_ruby_range? 383 to_range.eql?(other) 384 else 385 false 386 end 387 else 388 false 389 end 390 end
Whether the beginning element is excluded from the range.
# File lib/sequel/extensions/pg_range.rb 425 def exclude_begin? 426 @exclude_begin 427 end
Whether the ending element is excluded from the range.
# File lib/sequel/extensions/pg_range.rb 430 def exclude_end? 431 @exclude_end 432 end
Make sure equal ranges have the same hash.
# File lib/sequel/extensions/pg_range.rb 394 def hash 395 if @empty 396 @db_type.hash 397 else 398 [@begin, @end, @exclude_begin, @exclude_end, @db_type].hash 399 end 400 end
Wrap the PGRange
instance in an RangeOp
, allowing you to easily use the PostgreSQL range functions and operators with literal ranges.
# File lib/sequel/extensions/pg_range_ops.rb 141 def op 142 RangeOp.new(self) 143 end
Append a literalize version of the receiver to the sql.
# File lib/sequel/extensions/pg_range.rb 435 def sql_literal_append(ds, sql) 436 if (s = @db_type) && !empty? 437 sql << s.to_s << "(" 438 ds.literal_append(sql, self.begin) 439 sql << ',' 440 ds.literal_append(sql, self.end) 441 sql << ',' 442 ds.literal_append(sql, "#{exclude_begin? ? "(" : "["}#{exclude_end? ? ")" : "]"}") 443 sql << ")" 444 else 445 ds.literal_append(sql, unquoted_literal(ds)) 446 if s 447 sql << '::' << s.to_s 448 end 449 end 450 end
Return a ruby Range
object for this instance, if one can be created.
# File lib/sequel/extensions/pg_range.rb 456 def to_range 457 return @range if @range 458 raise(Error, "cannot create ruby range for an empty PostgreSQL range") if empty? 459 raise(Error, "cannot create ruby range when PostgreSQL range excludes beginning element") if exclude_begin? 460 # :nocov: 461 raise(Error, "cannot create ruby range when PostgreSQL range has unbounded beginning") if STARTLESS_RANGE_NOT_SUPPORTED && !self.begin 462 raise(Error, "cannot create ruby range when PostgreSQL range has unbounded ending") if ENDLESS_RANGE_NOT_SUPPORTED && !self.end 463 # :nocov: 464 @range = Range.new(self.begin, self.end, exclude_end?) 465 end
Whether the beginning of the range is unbounded.
# File lib/sequel/extensions/pg_range.rb 475 def unbounded_begin? 476 self.begin.nil? && !empty? 477 end
Whether the end of the range is unbounded.
# File lib/sequel/extensions/pg_range.rb 480 def unbounded_end? 481 self.end.nil? && !empty? 482 end
Return a string containing the unescaped version of the range. Separated out for use by the bound argument code.
# File lib/sequel/extensions/pg_range.rb 486 def unquoted_literal(ds) 487 if empty? 488 'empty' 489 else 490 "#{exclude_begin? ? "(" : "["}#{escape_value(self.begin, ds)},#{escape_value(self.end, ds)}#{exclude_end? ? ")" : "]"}" 491 end 492 end
Whether or not this PGRange
is a valid ruby range. In order to be a valid ruby range, it must have a beginning and an ending (no unbounded ranges), and it cannot exclude the beginning element.
# File lib/sequel/extensions/pg_range.rb 470 def valid_ruby_range? 471 !(empty? || exclude_begin? || (STARTLESS_RANGE_NOT_SUPPORTED && !self.begin) || (ENDLESS_RANGE_NOT_SUPPORTED && !self.end)) 472 end