Class: Sequel::Postgres::PGRow::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/extensions/pg_row.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h = OPTS) ⇒ Parser

Sets each of the parser’s attributes, using options with the same name (e.g. :columns sets the columns attribute).



306
307
308
309
310
311
312
313
# File 'lib/sequel/extensions/pg_row.rb', line 306

def initialize(h=OPTS)
  @columns = h[:columns]
  @column_converters = h[:column_converters]
  @column_oids = h[:column_oids]
  @converter = h[:converter]
  @typecaster = h[:typecaster]
  @oid = h[:oid]
end

Instance Attribute Details

#column_convertersObject (readonly)

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.



282
283
284
# File 'lib/sequel/extensions/pg_row.rb', line 282

def column_converters
  @column_converters
end

#column_oidsObject (readonly)

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



286
287
288
# File 'lib/sequel/extensions/pg_row.rb', line 286

def column_oids
  @column_oids
end

#columnsObject (readonly)

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.



276
277
278
# File 'lib/sequel/extensions/pg_row.rb', line 276

def columns
  @columns
end

#converterObject (readonly)

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.



291
292
293
# File 'lib/sequel/extensions/pg_row.rb', line 291

def converter
  @converter
end

#oidObject (readonly)

The oid for the composite type itself.



294
295
296
# File 'lib/sequel/extensions/pg_row.rb', line 294

def oid
  @oid
end

#typecasterObject (readonly)

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.



302
303
304
# File 'lib/sequel/extensions/pg_row.rb', line 302

def typecaster
  @typecaster
end

Instance Method Details

#call(s) ⇒ Object

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



317
318
319
# File 'lib/sequel/extensions/pg_row.rb', line 317

def call(s)
  convert(convert_format(convert_columns(Splitter.new(s).parse)))
end

#typecast(obj) ⇒ Object

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.



325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/sequel/extensions/pg_row.rb', line 325

def typecast(obj)
  case obj 
  when Array
    _typecast(convert_format(obj))
  when Hash
    unless @columns
      raise Error, 'PGRow::Parser without columns cannot typecast from a hash'
    end
    _typecast(obj)
  else
    raise Error, 'PGRow::Parser can only typecast arrays and hashes'
  end
end