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

column_converters[R]

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.

column_oids[R]

The OIDs for each member in the composite type. Not currently used, but made available for user code.

columns[R]

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.

converter[R]

A converter for the object as a whole. Used to wrap the returned array/hash in another object, such as an ArrayRow or HashRow. If present, should be callable.

oid[R]

The oid for the composite type itself.

typecaster[R]

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

new(h=OPTS) click to toggle source

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
285 def initialize(h=OPTS)
286   @columns = h[:columns]
287   @column_converters = h[:column_converters]
288   @column_oids = h[:column_oids]
289   @converter = h[:converter]
290   @typecaster = h[:typecaster]
291   @oid = h[:oid]
292 end

Public Instance Methods

call(s) click to toggle source

Convert the PostgreSQL composite type input format into an appropriate ruby object.

    # File lib/sequel/extensions/pg_row.rb
296 def call(s)
297   convert(convert_format(convert_columns(Splitter.new(s).parse)))
298 end
typecast(obj) click to toggle source

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
304 def typecast(obj)
305   case obj 
306   when Array
307     _typecast(convert_format(obj))
308   when Hash
309     unless @columns
310       raise Error, 'PGRow::Parser without columns cannot typecast from a hash'
311     end
312     _typecast(obj)
313   else
314     raise Error, 'PGRow::Parser can only typecast arrays and hashes'
315   end
316 end

Private Instance Methods

_typecast(obj) click to toggle source

If the parser has a typecaster, call it with the object, otherwise return the object as is.

    # File lib/sequel/extensions/pg_row.rb
322 def _typecast(obj)
323   if t = @typecaster
324     t.call(obj)
325   else
326     obj
327   end
328 end
convert(obj) click to toggle source

If the parser has a converter, call it with the object, otherwise return the object as is.

    # File lib/sequel/extensions/pg_row.rb
355 def convert(obj)
356   if c = @converter
357     c.call(obj)
358   else
359     obj
360   end
361 end
convert_columns(arr) click to toggle source

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
333 def convert_columns(arr)
334   if ccs = @column_converters
335     arr.zip(ccs).map{|v, pr| (v && pr) ? pr.call(v) : v}
336   else
337     arr 
338   end
339 end
convert_format(arr) click to toggle source

If the parser has columns, return a hash assuming that the array is ordered by the columns.

    # File lib/sequel/extensions/pg_row.rb
343 def convert_format(arr)
344   if cs = @columns
345     h = {}
346     arr.zip(cs).each{|v, c| h[c] = v}
347     h
348   else
349     arr
350   end
351 end