class Sequel::Postgres::PGRange::Parser

  1. lib/sequel/extensions/pg_range.rb
Superclass: Object

Creates callable objects that convert strings into PGRange instances.

Methods

Public Class

  1. new

Public Instance

  1. call
  2. converter
  3. db_type

Attributes

converter [R]

A callable object to convert the beginning and ending of the range into the appropriate ruby type.

db_type [R]

The database range type for this parser (e.g. ‘int4range’), automatically setting the db_type for the returned PGRange instances.

Public Class methods

new(db_type, converter=nil)

Set the db_type and converter on initialization.

[show source]
   # File lib/sequel/extensions/pg_range.rb
85 def initialize(db_type, converter=nil)
86   @db_type = db_type.to_s.dup.freeze if db_type
87   @converter = converter
88 end

Public Instance methods

call(string)

Parse the range type input string into a PGRange value.

[show source]
    # File lib/sequel/extensions/pg_range.rb
 91 def call(string)
 92   if string == 'empty'
 93     return PGRange.empty(db_type)
 94   end
 95 
 96   raise(InvalidValue, "invalid or unhandled range format: #{string.inspect}") unless matches = /\A(\[|\()("((?:\\"|[^"])*)"|[^"]*),("((?:\\"|[^"])*)"|[^"]*)(\]|\))\z/.match(string)
 97 
 98   exclude_begin = matches[1] == '('
 99   exclude_end = matches[6] == ')'
100 
101   # If the input is quoted, it needs to be unescaped.  Also, quoted input isn't
102   # checked for emptiness, since the empty quoted string is considered an
103   # element that happens to be the empty string, while an unquoted empty string
104   # is considered unbounded.
105   #
106   # While PostgreSQL allows pure escaping for input (without quoting), it appears
107   # to always use the quoted output form when characters need to be escaped, so
108   # there isn't a need to unescape unquoted output.
109   if beg = matches[3]
110     beg.gsub!(/\\(.)/, '\1')
111   else
112     beg = matches[2] unless matches[2].empty?
113   end
114   if en = matches[5]
115     en.gsub!(/\\(.)/, '\1')
116   else
117     en = matches[4] unless matches[4].empty?
118   end
119 
120   if c = converter
121     beg = c.call(beg) if beg
122     en = c.call(en) if en
123   end
124 
125   PGRange.new(beg, en, :exclude_begin=>exclude_begin, :exclude_end=>exclude_end, :db_type=>db_type)
126 end