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