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

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 323
323:       def self.empty(db_type=nil)
324:         new(nil, nil, :empty=>true, :db_type=>db_type)
325:       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 318
318:       def self.from_range(range, db_type=nil)
319:         new(range.begin, range.end, :exclude_end=>range.exclude_end?, :db_type=>db_type)
320:       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 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

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 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.

[Source]

     # 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

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 409
409:       def empty?
410:         @empty
411:       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 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.

[Source]

     # 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.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 419
419:       def exclude_end?
420:         @exclude_end
421:       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 123
123:         def op
124:           RangeOp.new(self)
125:         end

Append a literalize version of the receiver to the sql.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

     # 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.

[Source]

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

[Validate]