class Sequel::Postgres::PGRow::Parser
The Parser
is responsible for taking the input string from PostgreSQL, and returning an appropriate ruby object that the type represents, such as an ArrayRow
or HashRow
.
Attributes
Converters for each member in the composite type. If not present, no conversion will be done, so values will remain strings. If present, should be an array of callable objects.
The OIDs for each member in the composite type. Not currently used, but made available for user code.
The columns for the parser, if any. If the parser has no columns, it will treat the input as an array. If it has columns, it will treat the input as a hash. If present, should be an array of strings.
The oid for the composite type itself.
A callable object used for typecasting the object. This is similar to the converter, but it is called by the typecasting code, which has different assumptions than the converter. For instance, the converter should be called with all of the member values already typecast, but the typecaster may not be.
Public Class Methods
Source
# File lib/sequel/extensions/pg_row.rb 306 def initialize(h=OPTS) 307 @columns = h[:columns] 308 @column_converters = h[:column_converters] 309 @column_oids = h[:column_oids] 310 @converter = h[:converter] 311 @typecaster = h[:typecaster] 312 @oid = h[:oid] 313 end
Sets each of the parser’s attributes, using options with the same name (e.g. :columns sets the columns attribute).
Public Instance Methods
Source
# File lib/sequel/extensions/pg_row.rb 317 def call(s) 318 convert(convert_format(convert_columns(Splitter.new(s).parse))) 319 end
Convert the PostgreSQL composite type input format into an appropriate ruby object.
Source
# File lib/sequel/extensions/pg_row.rb 325 def typecast(obj) 326 case obj 327 when Array 328 _typecast(convert_format(obj)) 329 when Hash 330 unless @columns 331 raise Error, 'PGRow::Parser without columns cannot typecast from a hash' 332 end 333 _typecast(obj) 334 else 335 raise Error, 'PGRow::Parser can only typecast arrays and hashes' 336 end 337 end
Typecast the given object to the appropriate type using the typecaster. Note that this does not conversion for the members of the composite type, since those conversion expect strings and strings may not be provided.
Private Instance Methods
Source
# File lib/sequel/extensions/pg_row.rb 343 def _typecast(obj) 344 if t = @typecaster 345 t.call(obj) 346 else 347 obj 348 end 349 end
If the parser has a typecaster, call it with the object, otherwise return the object as is.
Source
# File lib/sequel/extensions/pg_row.rb 376 def convert(obj) 377 if c = @converter 378 c.call(obj) 379 else 380 obj 381 end 382 end
If the parser has a converter, call it with the object, otherwise return the object as is.
Source
# File lib/sequel/extensions/pg_row.rb 354 def convert_columns(arr) 355 if ccs = @column_converters 356 arr.zip(ccs).map{|v, pr| (v && pr) ? pr.call(v) : v} 357 else 358 arr 359 end 360 end
If the parser has column converters, map the array of strings input to a array of appropriate ruby objects, one for each converter.
Source
# File lib/sequel/extensions/pg_row.rb 364 def convert_format(arr) 365 if cs = @columns 366 h = {} 367 arr.zip(cs).each{|v, c| h[c] = v} 368 h 369 else 370 arr 371 end 372 end
If the parser has columns, return a hash assuming that the array is ordered by the columns.