Class | Sequel::Postgres::PGRange |
In: |
lib/sequel/extensions/pg_range_ops.rb
lib/sequel/extensions/pg_range.rb |
Parent: | Object |
begin | [R] | The beginning of the range. If nil, the range has an unbounded beginning. |
db_type | [R] | The PostgreSQL database type for the range (e.g. ‘int4range’). |
end | [R] | The end of the range. If nil, the range has an unbounded ending. |
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, line 333 333: def initialize(beg, en, opts=OPTS) 334: @begin = beg 335: @end = en 336: @empty = !!opts[:empty] 337: @exclude_begin = !!opts[:exclude_begin] 338: @exclude_end = !!opts[:exclude_end] 339: @db_type = opts[:db_type] 340: if @empty 341: 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? 342: end 343: end
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, line 394 394: def ===(other) 395: if eql?(other) 396: true 397: else 398: if valid_ruby_range? 399: to_range === other 400: else 401: false 402: end 403: end 404: end
Return whether the value is inside the range.
# File lib/sequel/extensions/pg_range.rb, line 352 352: def cover?(value) 353: return false if empty? 354: b = self.begin 355: return false if b && b.public_send(exclude_begin? ? :>= : :>, value) 356: e = self.end 357: return false if e && e.public_send(exclude_end? ? :<= : :<, value) 358: true 359: 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, line 365 365: def eql?(other) 366: case other 367: when PGRange 368: if db_type == other.db_type 369: if empty? 370: other.empty? 371: elsif other.empty? 372: false 373: else 374: [:@begin, :@end, :@exclude_begin, :@exclude_end].all?{|v| instance_variable_get(v) == other.instance_variable_get(v)} 375: end 376: else 377: false 378: end 379: when Range 380: if valid_ruby_range? 381: to_range.eql?(other) 382: else 383: false 384: end 385: else 386: false 387: end 388: end
Whether the beginning element is excluded from the range.
# File lib/sequel/extensions/pg_range.rb, line 414 414: def exclude_begin? 415: @exclude_begin 416: end
Whether the ending element is excluded from the range.
# File lib/sequel/extensions/pg_range.rb, line 419 419: def exclude_end? 420: @exclude_end 421: end
Append a literalize version of the receiver to the sql.
# File lib/sequel/extensions/pg_range.rb, line 424 424: def sql_literal_append(ds, sql) 425: if (s = @db_type) && !empty? 426: sql << s.to_s << "(" 427: ds.literal_append(sql, self.begin) 428: sql << ',' 429: ds.literal_append(sql, self.end) 430: sql << ',' 431: ds.literal_append(sql, "#{exclude_begin? ? "(" : "["}#{exclude_end? ? ")" : "]"}") 432: sql << ")" 433: else 434: ds.literal_append(sql, unquoted_literal(ds)) 435: if s 436: sql << '::' << s.to_s 437: end 438: end 439: end
Return a ruby Range object for this instance, if one can be created.
# File lib/sequel/extensions/pg_range.rb, line 442 442: def to_range 443: return @range if @range 444: raise(Error, "cannot create ruby range for an empty PostgreSQL range") if empty? 445: raise(Error, "cannot create ruby range when PostgreSQL range excludes beginning element") if exclude_begin? 446: raise(Error, "cannot create ruby range when PostgreSQL range has unbounded beginning") unless self.begin 447: raise(Error, "cannot create ruby range when PostgreSQL range has unbounded ending") unless self.end 448: @range = Range.new(self.begin, self.end, exclude_end?) 449: end
Whether the beginning of the range is unbounded.
# File lib/sequel/extensions/pg_range.rb, line 459 459: def unbounded_begin? 460: self.begin.nil? && !empty? 461: end
Whether the end of the range is unbounded.
# File lib/sequel/extensions/pg_range.rb, line 464 464: def unbounded_end? 465: self.end.nil? && !empty? 466: 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, line 470 470: def unquoted_literal(ds) 471: if empty? 472: 'empty' 473: else 474: "#{exclude_begin? ? "(" : "["}#{escape_value(self.begin, ds)},#{escape_value(self.end, ds)}#{exclude_end? ? ")" : "]"}" 475: end 476: 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, line 454 454: def valid_ruby_range? 455: !(empty? || exclude_begin? || !self.begin || !self.end) 456: end