Class Sequel::Postgres::PGRange
In: lib/sequel/extensions/pg_range_ops.rb
lib/sequel/extensions/pg_range.rb
Parent: Object

Methods

Included Modules

Sequel::SQL::AliasMethods Enumerable

Classes and Modules

Module Sequel::Postgres::PGRange::DatabaseMethods
Module Sequel::Postgres::PGRange::DatasetMethods
Class Sequel::Postgres::PGRange::Parser

Constants

RANGE_TYPES = {}   Map of string database type names to type symbols (e.g. ‘int4range’ => :int4range), used in the schema parsing.
EMPTY = 'empty'.freeze
EMPTY_STRING = ''.freeze
QUOTED_EMPTY_STRING = '""'.freeze
OPEN_PAREN = "(".freeze
CLOSE_PAREN = ")".freeze
OPEN_BRACKET = "[".freeze
CLOSE_BRACKET = "]".freeze
ESCAPE_RE = /("|,|\\|\[|\]|\(|\))/.freeze
ESCAPE_REPLACE = '\\\\\1'.freeze
CAST = '::'.freeze

Attributes

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.

Public Class methods

Create an empty PGRange with the given database type.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 324
324:       def self.empty(db_type=nil)
325:         new(nil, nil, :empty=>true, :db_type=>db_type)
326:       end

Create a new PGRange instance using the beginning and ending of the ruby Range, with the given db_type.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 319
319:       def self.from_range(range, db_type=nil)
320:         new(range.begin, range.end, :exclude_end=>range.exclude_end?, :db_type=>db_type)
321:       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.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 334
334:       def initialize(beg, en, opts=OPTS)
335:         @begin = beg
336:         @end = en
337:         @empty = !!opts[:empty]
338:         @exclude_begin = !!opts[:exclude_begin]
339:         @exclude_end = !!opts[:exclude_end]
340:         @db_type = opts[:db_type]
341:         if @empty
342:           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?
343:         end
344:       end

Registers a range type that the extension should handle. Makes a Database instance that has been extended with DatabaseMethods recognize the range type given and set up the appropriate typecasting. Also sets up automatic typecasting for the native postgres adapter, so that on retrieval, the values are automatically converted to PGRange instances. The db_type argument should be the name of the range type. Accepts the following options:

:converter :A callable object (e.g. Proc), that is called with the start or end of the range (usually a string), and should return the appropriate typecasted object.
:oid :The PostgreSQL OID for the range type. This is used by the Sequel postgres adapter to set up automatic type conversion on retrieval from the database.
:subtype_oid :Should be the PostgreSQL OID for the range‘s subtype. If given,
               automatically sets the :converter option by looking for scalar conversion
               proc.

If a block is given, it is treated as the :converter option.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 98
 98:       def self.register(db_type, opts=OPTS, &block)
 99:         db_type = db_type.to_s.dup.freeze
100: 
101:         if converter = opts[:converter]
102:           raise Error, "can't provide both a block and :converter option to register" if block
103:         else
104:           converter = block
105:         end
106: 
107:         if soid = opts[:subtype_oid]
108:           raise Error, "can't provide both a converter and :scalar_oid option to register" if converter 
109:           raise Error, "no conversion proc for :scalar_oid=>#{soid.inspect} in PG_TYPES" unless converter = PG_TYPES[soid]
110:         end
111: 
112:         parser = Parser.new(db_type, converter)
113: 
114:         RANGE_TYPES[db_type] = db_type.to_sym
115: 
116:         DatabaseMethods.define_range_typecast_method(db_type, parser)
117: 
118:         if oid = opts[:oid]
119:           Sequel::Postgres::PG_TYPES[oid] = parser
120:         end
121: 
122:         nil
123:       end

Public Instance methods

==(other)

Alias for eql?

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.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 386
386:       def ===(other)
387:         if eql?(other)
388:           true
389:         else
390:           if valid_ruby_range?
391:             to_range === other 
392:           else
393:             false
394:           end
395:         end
396:       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.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 401
401:       def empty?
402:         @empty
403:       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.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 357
357:       def eql?(other)
358:         case other
359:         when PGRange
360:           if db_type == other.db_type
361:             if empty?
362:               other.empty?
363:             elsif other.empty?
364:               false
365:             else
366:               [:@begin, :@end, :@exclude_begin, :@exclude_end].all?{|v| instance_variable_get(v) == other.instance_variable_get(v)}
367:             end
368:           else
369:             false
370:           end
371:         when Range
372:           if valid_ruby_range?
373:             to_range.eql?(other)
374:           else
375:             false
376:           end
377:         else
378:           false
379:         end
380:       end

Whether the beginning element is excluded from the range.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 406
406:       def exclude_begin?
407:         @exclude_begin
408:       end

Whether the ending element is excluded from the range.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 411
411:       def exclude_end?
412:         @exclude_end
413:       end

Wrap the PGRange instance in an RangeOp, allowing you to easily use the PostgreSQL range functions and operators with literal ranges.

[Source]

     # File lib/sequel/extensions/pg_range_ops.rb, line 120
120:         def op
121:           RangeOp.new(self)
122:         end

Append a literalize version of the receiver to the sql.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 416
416:       def sql_literal_append(ds, sql)
417:         ds.literal_append(sql, unquoted_literal(ds))
418:         if s = @db_type
419:           sql << CAST << s.to_s
420:         end
421:       end

Return a ruby Range object for this instance, if one can be created.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 424
424:       def to_range
425:         return @range if @range
426:         raise(Error, "cannot create ruby range for an empty PostgreSQL range") if empty?
427:         raise(Error, "cannot create ruby range when PostgreSQL range excludes beginning element") if exclude_begin?
428:         raise(Error, "cannot create ruby range when PostgreSQL range has unbounded beginning") unless self.begin
429:         raise(Error, "cannot create ruby range when PostgreSQL range has unbounded ending") unless self.end
430:         @range = Range.new(self.begin, self.end, exclude_end?)
431:       end

Whether the beginning of the range is unbounded.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 441
441:       def unbounded_begin?
442:         self.begin.nil? && !empty?
443:       end

Whether the end of the range is unbounded.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 446
446:       def unbounded_end?
447:         self.end.nil? && !empty?
448:       end

Return a string containing the unescaped version of the range. Separated out for use by the bound argument code.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 452
452:       def unquoted_literal(ds)
453:         if empty?
454:           EMPTY
455:         else
456:           "#{exclude_begin? ? OPEN_PAREN : OPEN_BRACKET}#{escape_value(self.begin, ds)},#{escape_value(self.end, ds)}#{exclude_end? ? CLOSE_PAREN : CLOSE_BRACKET}"
457:         end
458:       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.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 436
436:       def valid_ruby_range?
437:         !(empty? || exclude_begin? || !self.begin || !self.end)
438:       end

[Validate]