Class: Sequel::Postgres::PGRange
- Includes:
- Enumerable, SQL::AliasMethods
- Defined in:
- lib/sequel/extensions/pg_range.rb,
lib/sequel/extensions/pg_range_ops.rb
Defined Under Namespace
Modules: DatabaseMethods, DatasetMethods Classes: Parser
Instance Attribute Summary collapse
-
#begin ⇒ Object
readonly
The beginning of the range.
-
#db_type ⇒ Object
readonly
The PostgreSQL database type for the range (e.g. ‘int4range’).
-
#end ⇒ Object
readonly
The end of the range.
Class Method Summary collapse
-
.empty(db_type = nil) ⇒ Object
Create an empty PGRange with the given database type.
-
.from_range(range, db_type = nil) ⇒ Object
Create a new PGRange instance using the beginning and ending of the ruby Range, with the given db_type.
Instance Method Summary collapse
-
#===(other) ⇒ Object
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.
-
#cover?(value) ⇒ Boolean
Return whether the value is inside the range.
-
#empty? ⇒ Boolean
Whether this range is empty (has no points).
-
#eql?(other) ⇒ Boolean
(also: #==)
Consider the receiver equal to other PGRange instances with the same beginning, ending, exclusions, and database type.
-
#exclude_begin? ⇒ Boolean
Whether the beginning element is excluded from the range.
-
#exclude_end? ⇒ Boolean
Whether the ending element is excluded from the range.
-
#initialize(beg, en, opts = OPTS) ⇒ PGRange
constructor
Initialize a new PGRange instance.
-
#op ⇒ Object
Wrap the PGRange instance in an RangeOp, allowing you to easily use the PostgreSQL range functions and operators with literal ranges.
-
#sql_literal_append(ds, sql) ⇒ Object
Append a literalize version of the receiver to the sql.
-
#to_range ⇒ Object
Return a ruby Range object for this instance, if one can be created.
-
#unbounded_begin? ⇒ Boolean
Whether the beginning of the range is unbounded.
-
#unbounded_end? ⇒ Boolean
Whether the end of the range is unbounded.
-
#unquoted_literal(ds) ⇒ Object
Return a string containing the unescaped version of the range.
-
#valid_ruby_range? ⇒ Boolean
Whether or not this PGRange is a valid ruby range.
Methods included from SQL::AliasMethods
Constructor Details
#initialize(beg, en, opts = OPTS) ⇒ PGRange
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.
347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/sequel/extensions/pg_range.rb', line 347 def initialize(beg, en, opts=OPTS) @begin = beg @end = en @empty = !!opts[:empty] @exclude_begin = !!opts[:exclude_begin] @exclude_end = !!opts[:exclude_end] @db_type = opts[:db_type] if @empty 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? end end |
Instance Attribute Details
#begin ⇒ Object (readonly)
The beginning of the range. If nil, the range has an unbounded beginning.
322 323 324 |
# File 'lib/sequel/extensions/pg_range.rb', line 322 def begin @begin end |
#db_type ⇒ Object (readonly)
The PostgreSQL database type for the range (e.g. ‘int4range’).
328 329 330 |
# File 'lib/sequel/extensions/pg_range.rb', line 328 def db_type @db_type end |
#end ⇒ Object (readonly)
The end of the range. If nil, the range has an unbounded ending.
325 326 327 |
# File 'lib/sequel/extensions/pg_range.rb', line 325 def end @end end |
Class Method Details
.empty(db_type = nil) ⇒ Object
Create an empty PGRange with the given database type.
337 338 339 |
# File 'lib/sequel/extensions/pg_range.rb', line 337 def self.empty(db_type=nil) new(nil, nil, :empty=>true, :db_type=>db_type) end |
.from_range(range, db_type = nil) ⇒ Object
Create a new PGRange instance using the beginning and ending of the ruby Range, with the given db_type.
332 333 334 |
# File 'lib/sequel/extensions/pg_range.rb', line 332 def self.from_range(range, db_type=nil) new(range.begin, range.end, :exclude_end=>range.exclude_end?, :db_type=>db_type) end |
Instance Method Details
#===(other) ⇒ Object
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.
408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/sequel/extensions/pg_range.rb', line 408 def ===(other) if eql?(other) true else if valid_ruby_range? to_range === other else false end end end |
#cover?(value) ⇒ Boolean
Return whether the value is inside the range.
366 367 368 369 370 371 372 373 |
# File 'lib/sequel/extensions/pg_range.rb', line 366 def cover?(value) return false if empty? b = self.begin return false if b && b.public_send(exclude_begin? ? :>= : :>, value) e = self.end return false if e && e.public_send(exclude_end? ? :<= : :<, value) true end |
#empty? ⇒ Boolean
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.
423 424 425 |
# File 'lib/sequel/extensions/pg_range.rb', line 423 def empty? @empty end |
#eql?(other) ⇒ Boolean Also known as: ==
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.
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/sequel/extensions/pg_range.rb', line 379 def eql?(other) case other when PGRange if db_type == other.db_type if empty? other.empty? elsif other.empty? false else [:@begin, :@end, :@exclude_begin, :@exclude_end].all?{|v| instance_variable_get(v) == other.instance_variable_get(v)} end else false end when Range if valid_ruby_range? to_range.eql?(other) else false end else false end end |
#exclude_begin? ⇒ Boolean
Whether the beginning element is excluded from the range.
428 429 430 |
# File 'lib/sequel/extensions/pg_range.rb', line 428 def exclude_begin? @exclude_begin end |
#exclude_end? ⇒ Boolean
Whether the ending element is excluded from the range.
433 434 435 |
# File 'lib/sequel/extensions/pg_range.rb', line 433 def exclude_end? @exclude_end end |
#op ⇒ Object
Wrap the PGRange instance in an RangeOp, allowing you to easily use the PostgreSQL range functions and operators with literal ranges.
123 124 125 |
# File 'lib/sequel/extensions/pg_range_ops.rb', line 123 def op RangeOp.new(self) end |
#sql_literal_append(ds, sql) ⇒ Object
Append a literalize version of the receiver to the sql.
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/sequel/extensions/pg_range.rb', line 438 def sql_literal_append(ds, sql) if (s = @db_type) && !empty? sql << s.to_s << "(" ds.literal_append(sql, self.begin) sql << ',' ds.literal_append(sql, self.end) sql << ',' ds.literal_append(sql, "#{exclude_begin? ? "(" : "["}#{exclude_end? ? ")" : "]"}") sql << ")" else ds.literal_append(sql, unquoted_literal(ds)) if s sql << '::' << s.to_s end end end |
#to_range ⇒ Object
Return a ruby Range object for this instance, if one can be created.
456 457 458 459 460 461 462 463 |
# File 'lib/sequel/extensions/pg_range.rb', line 456 def to_range return @range if @range raise(Error, "cannot create ruby range for an empty PostgreSQL range") if empty? raise(Error, "cannot create ruby range when PostgreSQL range excludes beginning element") if exclude_begin? raise(Error, "cannot create ruby range when PostgreSQL range has unbounded beginning") unless self.begin raise(Error, "cannot create ruby range when PostgreSQL range has unbounded ending") unless self.end @range = Range.new(self.begin, self.end, exclude_end?) end |
#unbounded_begin? ⇒ Boolean
Whether the beginning of the range is unbounded.
473 474 475 |
# File 'lib/sequel/extensions/pg_range.rb', line 473 def unbounded_begin? self.begin.nil? && !empty? end |
#unbounded_end? ⇒ Boolean
Whether the end of the range is unbounded.
478 479 480 |
# File 'lib/sequel/extensions/pg_range.rb', line 478 def unbounded_end? self.end.nil? && !empty? end |
#unquoted_literal(ds) ⇒ Object
Return a string containing the unescaped version of the range. Separated out for use by the bound argument code.
484 485 486 487 488 489 490 |
# File 'lib/sequel/extensions/pg_range.rb', line 484 def unquoted_literal(ds) if empty? 'empty' else "#{exclude_begin? ? "(" : "["}#{escape_value(self.begin, ds)},#{escape_value(self.end, ds)}#{exclude_end? ? ")" : "]"}" end end |
#valid_ruby_range? ⇒ Boolean
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.
468 469 470 |
# File 'lib/sequel/extensions/pg_range.rb', line 468 def valid_ruby_range? !(empty? || exclude_begin? || !self.begin || !self.end) end |