Class | Sequel::Postgres::PGRange::Parser |
In: |
lib/sequel/extensions/pg_range.rb
|
Parent: | Object |
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 |
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. |
Set the db_type and converter on initialization.
# File lib/sequel/extensions/pg_range.rb, line 138 138: def initialize(db_type, converter=nil) 139: @db_type = db_type.to_s.dup.freeze if db_type 140: @converter = converter 141: end
Parse the range type input string into a PGRange value.
# File lib/sequel/extensions/pg_range.rb, line 144 144: def call(string) 145: if string == EMPTY 146: return PGRange.empty(db_type) 147: end 148: 149: raise(InvalidValue, "invalid or unhandled range format: #{string.inspect}") unless matches = PARSER.match(string) 150: 151: exclude_begin = matches[1] == '(' 152: exclude_end = matches[6] == ')' 153: 154: # If the input is quoted, it needs to be unescaped. Also, quoted input isn't 155: # checked for emptiness, since the empty quoted string is considered an 156: # element that happens to be the empty string, while an unquoted empty string 157: # is considered unbounded. 158: # 159: # While PostgreSQL allows pure escaping for input (without quoting), it appears 160: # to always use the quoted output form when characters need to be escaped, so 161: # there isn't a need to unescape unquoted output. 162: if beg = matches[3] 163: beg.gsub!(REPLACE_RE, REPLACE_WITH) 164: else 165: beg = matches[2] unless matches[2].empty? 166: end 167: if en = matches[5] 168: en.gsub!(REPLACE_RE, REPLACE_WITH) 169: else 170: en = matches[4] unless matches[4].empty? 171: end 172: 173: if c = converter 174: beg = c.call(beg) if beg 175: en = c.call(en) if en 176: end 177: 178: PGRange.new(beg, en, :exclude_begin=>exclude_begin, :exclude_end=>exclude_end, :db_type=>db_type) 179: end