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 286 def initialize(h=OPTS) 287 @columns = h[:columns] 288 @column_converters = h[:column_converters] 289 @column_oids = h[:column_oids] 290 @converter = h[:converter] 291 @typecaster = h[:typecaster] 292 @oid = h[:oid] 293 end
Public Instance Methods
Convert the PostgreSQL composite type input format into an appropriate ruby object.
# File lib/sequel/extensions/pg_row.rb 297 def call(s) 298 convert(convert_format(convert_columns(Splitter.new(s).parse))) 299 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 305 def typecast(obj) 306 case obj 307 when Array 308 _typecast(convert_format(obj)) 309 when Hash 310 unless @columns 311 raise Error, 'PGRow::Parser without columns cannot typecast from a hash' 312 end 313 _typecast(obj) 314 else 315 raise Error, 'PGRow::Parser can only typecast arrays and hashes' 316 end 317 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 323 def _typecast(obj) 324 if t = @typecaster 325 t.call(obj) 326 else 327 obj 328 end 329 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 356 def convert(obj) 357 if c = @converter 358 c.call(obj) 359 else 360 obj 361 end 362 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 334 def convert_columns(arr) 335 if ccs = @column_converters 336 arr.zip(ccs).map{|v, pr| (v && pr) ? pr.call(v) : v} 337 else 338 arr 339 end 340 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 344 def convert_format(arr) 345 if cs = @columns 346 h = {} 347 arr.zip(cs).each{|v, c| h[c] = v} 348 h 349 else 350 arr 351 end 352 end