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

Creates callable objects that convert strings into PGRange instances.

Methods

call   new  

Constants

PARSER = /\A(\[|\()("((?:\\"|[^"])*)"|[^"]*),("((?:\\"|[^"])*)"|[^"]*)(\]|\))\z/o   Regexp that parses the full range of PostgreSQL range type output, except for empty ranges.
REPLACE_RE = /\\(.)/.freeze
REPLACE_WITH = '\1'.freeze

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

Set the db_type and converter on initialization.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 143
143:         def initialize(db_type, converter=nil)
144:           @db_type = db_type.to_s.dup.freeze if db_type
145:           @converter = converter
146:         end

Public Instance methods

Parse the range type input string into a PGRange value.

[Source]

     # File lib/sequel/extensions/pg_range.rb, line 149
149:         def call(string)
150:           if string == EMPTY
151:             return PGRange.empty(db_type)
152:           end
153: 
154:           raise(InvalidValue, "invalid or unhandled range format: #{string.inspect}") unless matches = PARSER.match(string)
155: 
156:           exclude_begin = matches[1] == '('
157:           exclude_end = matches[6] == ')'
158: 
159:           # If the input is quoted, it needs to be unescaped.  Also, quoted input isn't
160:           # checked for emptiness, since the empty quoted string is considered an 
161:           # element that happens to be the empty string, while an unquoted empty string
162:           # is considered unbounded.
163:           #
164:           # While PostgreSQL allows pure escaping for input (without quoting), it appears
165:           # to always use the quoted output form when characters need to be escaped, so
166:           # there isn't a need to unescape unquoted output.
167:           if beg = matches[3]
168:             beg.gsub!(REPLACE_RE, REPLACE_WITH)
169:           else
170:             beg = matches[2] unless matches[2].empty?
171:           end
172:           if en = matches[5]
173:             en.gsub!(REPLACE_RE, REPLACE_WITH)
174:           else
175:             en = matches[4] unless matches[4].empty?
176:           end
177: 
178:           if c = converter
179:             beg = c.call(beg) if beg
180:             en = c.call(en) if en
181:           end
182: 
183:           PGRange.new(beg, en, :exclude_begin=>exclude_begin, :exclude_end=>exclude_end, :db_type=>db_type)
184:         end

[Validate]