class Sequel::Postgres::PGRange::Parser
Creates callable objects that convert strings into PGRange
instances.
Attributes
converter[R]
A callable object to convert the beginning and ending of the range into the appropriate ruby type.
Public Class Methods
new(db_type, converter=nil)
click to toggle source
Set the db_type
and converter on initialization.
# File lib/sequel/extensions/pg_range.rb 89 def initialize(db_type, converter=nil) 90 @db_type = db_type.to_s.dup.freeze if db_type 91 @converter = converter 92 end
Public Instance Methods
call(string)
click to toggle source
Parse the range type input string into a PGRange
value.
# File lib/sequel/extensions/pg_range.rb 95 def call(string) 96 if string == 'empty' 97 return PGRange.empty(db_type) 98 end 99 100 raise(InvalidValue, "invalid or unhandled range format: #{string.inspect}") unless matches = /\A(\[|\()("((?:\\"|[^"])*)"|[^"]*),("((?:\\"|[^"])*)"|[^"]*)(\]|\))\z/.match(string) 101 102 exclude_begin = matches[1] == '(' 103 exclude_end = matches[6] == ')' 104 105 # If the input is quoted, it needs to be unescaped. Also, quoted input isn't 106 # checked for emptiness, since the empty quoted string is considered an 107 # element that happens to be the empty string, while an unquoted empty string 108 # is considered unbounded. 109 # 110 # While PostgreSQL allows pure escaping for input (without quoting), it appears 111 # to always use the quoted output form when characters need to be escaped, so 112 # there isn't a need to unescape unquoted output. 113 if beg = matches[3] 114 beg.gsub!(/\\(.)/, '\1') 115 else 116 beg = matches[2] unless matches[2].empty? 117 end 118 if en = matches[5] 119 en.gsub!(/\\(.)/, '\1') 120 else 121 en = matches[4] unless matches[4].empty? 122 end 123 124 if c = converter 125 beg = c.call(beg) if beg 126 en = c.call(en) if en 127 end 128 129 PGRange.new(beg, en, :exclude_begin=>exclude_begin, :exclude_end=>exclude_end, :db_type=>db_type) 130 end