class Sequel::Postgres::PGRange
Attributes
The beginning of the range. If nil, the range has an unbounded beginning.
The PostgreSQL database type for the range (e.g. 'int4range').
The end of the range. If nil, the range has an unbounded ending.
Public Class Methods
Create an empty PGRange with the given database type.
# File lib/sequel/extensions/pg_range.rb, line 336 def self.empty(db_type=nil) new(nil, nil, :empty=>true, :db_type=>db_type) 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, line 346 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
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, line 407 def ===(other) if eql?(other) true else if valid_ruby_range? to_range === other else false end end end
Return whether the value is inside the range.
# File lib/sequel/extensions/pg_range.rb, line 365 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
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, line 422 def empty? @empty 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 378 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
Whether the beginning element is excluded from the range.
# File lib/sequel/extensions/pg_range.rb, line 427 def exclude_begin? @exclude_begin end
Whether the ending element is excluded from the range.
# File lib/sequel/extensions/pg_range.rb, line 432 def exclude_end? @exclude_end end
Append a literalize version of the receiver to the sql.
# File lib/sequel/extensions/pg_range.rb, line 437 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
Return a ruby Range object for this instance, if one can be created.
# File lib/sequel/extensions/pg_range.rb, line 455 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
Whether the beginning of the range is unbounded.
# File lib/sequel/extensions/pg_range.rb, line 472 def unbounded_begin? self.begin.nil? && !empty? end
Whether the end of the range is unbounded.
# File lib/sequel/extensions/pg_range.rb, line 477 def unbounded_end? self.end.nil? && !empty? 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 483 def unquoted_literal(ds) if empty? 'empty' else "#{exclude_begin? ? "(" : "["}#{escape_value(self.begin, ds)},#{escape_value(self.end, ds)}#{exclude_end? ? ")" : "]"}" end 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 467 def valid_ruby_range? !(empty? || exclude_begin? || !self.begin || !self.end) end
Private Instance Methods
Escape common range types. Instead of quoting, just backslash escape all special characters.
# File lib/sequel/extensions/pg_range.rb, line 495 def escape_value(k, ds) case k when nil '' when Date, Time ds.literal(k)[1...-1] when Integer, Float k.to_s when BigDecimal k.to_s('F') when LiteralString k when String if k.empty? '""' else k.gsub(/("|,|\\|\[|\]|\(|\))/, '\\\\\1') end else ds.literal(k).gsub(/("|,|\\|\[|\]|\(|\))/, '\\\\\1') end end