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
Sets each of the parser's attributes, using options with the same name (e.g. :columns sets the columns attribute).
# File lib/sequel/extensions/pg_row.rb 287 def initialize(h=OPTS) 288 @columns = h[:columns] 289 @column_converters = h[:column_converters] 290 @column_oids = h[:column_oids] 291 @converter = h[:converter] 292 @typecaster = h[:typecaster] 293 @oid = h[:oid] 294 end
Public Instance Methods
Convert the PostgreSQL composite type input format into an appropriate ruby object.
# File lib/sequel/extensions/pg_row.rb 298 def call(s) 299 convert(convert_format(convert_columns(Splitter.new(s).parse))) 300 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.
# File lib/sequel/extensions/pg_row.rb 306 def typecast(obj) 307 case obj 308 when Array 309 _typecast(convert_format(obj)) 310 when Hash 311 unless @columns 312 raise Error, 'PGRow::Parser without columns cannot typecast from a hash' 313 end 314 _typecast(obj) 315 else 316 raise Error, 'PGRow::Parser can only typecast arrays and hashes' 317 end 318 end
Private Instance Methods
If the parser has a typecaster, call it with the object, otherwise return the object as is.
# File lib/sequel/extensions/pg_row.rb 324 def _typecast(obj) 325 if t = @typecaster 326 t.call(obj) 327 else 328 obj 329 end 330 end
If the parser has a converter, call it with the object, otherwise return the object as is.
# File lib/sequel/extensions/pg_row.rb 357 def convert(obj) 358 if c = @converter 359 c.call(obj) 360 else 361 obj 362 end 363 end
If the parser has column converters, map the array of strings input to a array of appropriate ruby objects, one for each converter.
# File lib/sequel/extensions/pg_row.rb 335 def convert_columns(arr) 336 if ccs = @column_converters 337 arr.zip(ccs).map{|v, pr| (v && pr) ? pr.call(v) : v} 338 else 339 arr 340 end 341 end
If the parser has columns, return a hash assuming that the array is ordered by the columns.
# File lib/sequel/extensions/pg_row.rb 345 def convert_format(arr) 346 if cs = @columns 347 h = {} 348 arr.zip(cs).each{|v, c| h[c] = v} 349 h 350 else 351 arr 352 end 353 end