Class: Sequel::Postgres::PGRow::Parser
- 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
-
#column_converters ⇒ Object
readonly
Converters for each member in the composite type.
-
#column_oids ⇒ Object
readonly
The OIDs for each member in the composite type.
-
#columns ⇒ Object
readonly
The columns for the parser, if any.
-
#converter ⇒ Object
readonly
A converter for the object as a whole.
-
#oid ⇒ Object
readonly
The oid for the composite type itself.
-
#typecaster ⇒ Object
readonly
A callable object used for typecasting the object.
Instance Method Summary collapse
-
#call(s) ⇒ Object
Convert the PostgreSQL composite type input format into an appropriate ruby object.
-
#initialize(h = {}) ⇒ Parser
constructor
Sets each of the parser’s attributes, using options with the same name (e.g. :columns sets the columns attribute).
-
#typecast(obj) ⇒ Object
Typecast the given object to the appropriate type using the typecaster.
Constructor Details
#initialize(h = {}) ⇒ Parser
Sets each of the parser’s attributes, using options with the same name (e.g. :columns sets the columns attribute).
279 280 281 282 283 284 285 286 |
# File 'lib/sequel/extensions/pg_row.rb', line 279 def initialize(h={}) @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_converters ⇒ Object (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.
255 256 257 |
# File 'lib/sequel/extensions/pg_row.rb', line 255 def column_converters @column_converters end |
#column_oids ⇒ Object (readonly)
The OIDs for each member in the composite type. Not currently used, but made available for user code.
259 260 261 |
# File 'lib/sequel/extensions/pg_row.rb', line 259 def column_oids @column_oids end |
#columns ⇒ Object (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.
249 250 251 |
# File 'lib/sequel/extensions/pg_row.rb', line 249 def columns @columns end |
#converter ⇒ Object (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.
264 265 266 |
# File 'lib/sequel/extensions/pg_row.rb', line 264 def converter @converter end |
#oid ⇒ Object (readonly)
The oid for the composite type itself.
267 268 269 |
# File 'lib/sequel/extensions/pg_row.rb', line 267 def oid @oid end |
#typecaster ⇒ Object (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.
275 276 277 |
# File 'lib/sequel/extensions/pg_row.rb', line 275 def typecaster @typecaster end |
Instance Method Details
#call(s) ⇒ Object
Convert the PostgreSQL composite type input format into an appropriate ruby object.
290 291 292 |
# File 'lib/sequel/extensions/pg_row.rb', line 290 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.
298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/sequel/extensions/pg_row.rb', line 298 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 |