Class: Sequel::Postgres::PGRange::Parser
- Defined in:
- lib/sequel/extensions/pg_range.rb
Overview
Creates callable objects that convert strings into PGRange instances.
Constant Summary collapse
- PARSER =
Regexp that parses the full range of PostgreSQL range type output, except for empty ranges.
/\A(\[|\()("((?:\\"|[^"])*)"|[^"]*),("((?:\\"|[^"])*)"|[^"]*)(\]|\))\z/o
- REPLACE_RE =
/\\(.)/.freeze
- REPLACE_WITH =
'\1'.freeze
Instance Attribute Summary collapse
-
#converter ⇒ Object
readonly
A callable object to convert the beginning and ending of the range into the appropriate ruby type.
-
#db_type ⇒ Object
readonly
The database range type for this parser (e.g. ‘int4range’), automatically setting the db_type for the returned PGRange instances.
Instance Method Summary collapse
-
#call(string) ⇒ Object
Parse the range type input string into a PGRange value.
-
#initialize(db_type, converter = nil) ⇒ Parser
constructor
Set the db_type and converter on initialization.
Constructor Details
#initialize(db_type, converter = nil) ⇒ Parser
Set the db_type and converter on initialization.
193 194 195 196 |
# File 'lib/sequel/extensions/pg_range.rb', line 193 def initialize(db_type, converter=nil) @db_type = db_type.to_s.dup.freeze if db_type @converter = converter end |
Instance Attribute Details
#converter ⇒ Object (readonly)
A callable object to convert the beginning and ending of the range into the appropriate ruby type.
190 191 192 |
# File 'lib/sequel/extensions/pg_range.rb', line 190 def converter @converter end |
#db_type ⇒ Object (readonly)
The database range type for this parser (e.g. ‘int4range’), automatically setting the db_type for the returned PGRange instances.
186 187 188 |
# File 'lib/sequel/extensions/pg_range.rb', line 186 def db_type @db_type end |
Instance Method Details
#call(string) ⇒ Object
Parse the range type input string into a PGRange value.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/sequel/extensions/pg_range.rb', line 199 def call(string) if string == EMPTY return PGRange.empty(db_type) end raise(InvalidValue, "invalid or unhandled range format: #{string.inspect}") unless matches = PARSER.match(string) exclude_begin = matches[1] == '(' exclude_end = matches[6] == ')' # If the input is quoted, it needs to be unescaped. Also, quoted input isn't # checked for emptiness, since the empty quoted string is considered an # element that happens to be the empty string, while an unquoted empty string # is considered unbounded. # # While PostgreSQL allows pure escaping for input (without quoting), it appears # to always use the quoted output form when characters need to be escaped, so # there isn't a need to unescape unquoted output. if beg = matches[3] beg.gsub!(REPLACE_RE, REPLACE_WITH) else beg = matches[2] unless matches[2].empty? end if en = matches[5] en.gsub!(REPLACE_RE, REPLACE_WITH) else en = matches[4] unless matches[4].empty? end if c = converter beg = c.call(beg) if beg en = c.call(en) if en end PGRange.new(beg, en, :exclude_begin=>exclude_begin, :exclude_end=>exclude_end, :db_type=>db_type) end |