Class: Torque::PostgreSQL::Adapter::OID::Composite
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- Torque::PostgreSQL::Adapter::OID::Composite
- Defined in:
- lib/torque/postgresql/adapter/oid/composite.rb,
sig/torque/postgresql/adapter/oid/composite.rbs
Constant Summary collapse
- ARRAY =
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array
- DATA =
ARRAY::Data
- ENCODER =
PG::TextEncoder::Record.new
- DECODER =
PG::TextDecoder::Record.new
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .create(row, type_map) ⇒ Object
-
.from(type) ⇒ Composite?
The composite type behind the given type, if there is one, wrapped or not by an array or a delegator.
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
- #cast(value) ⇒ Object
- #changed_in_place?(raw_old_value, new_value) ⇒ Boolean
-
#columns ⇒ Hash[String, type_caster]
The ordered list of columns of the composite type, as a hash of name and type.
-
#deserialize(value) ⇒ Attributes::Composite?
A record that was already serialized comes back as the encoder and its values, which is how it is kept around for dirty tracking.
- #hash ⇒ Integer
-
#initialize(name) ⇒ Composite
constructor
A new instance of Composite.
-
#klass ⇒ singleton(Attributes::Composite)
The class that represents the composite type, loaded in a lazy way.
-
#serialize(value) ⇒ Object
The record is handed over to the adapter as an encoder and its values, the same way arrays are, so that columns are type casted by the connection right before the record literal is built.
- #type ⇒ :composite
Constructor Details
#initialize(name) ⇒ Composite
Returns a new instance of Composite.
29 30 31 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 29 def initialize(name) @name = name.to_s.freeze end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 13 def name @name end |
Class Method Details
.create(row, type_map) ⇒ Object
23 24 25 26 27 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 23 def self.create(row, type_map) oid_klass = new(row['typname']) type_map.register_type(row['oid'].to_i, oid_klass) type_map.register_type(row['typarray'].to_i, ARRAY.new(oid_klass, row['typdelim'])) end |
.from(type) ⇒ Composite?
The composite type behind the given type, if there is one, wrapped or not by an array or a delegator
17 18 19 20 21 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 17 def self.from(type) type = OID.unwrap(type) type = type.subtype if type.is_a?(ARRAY) type if type.is_a?(self) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
95 96 97 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 95 def ==(other) other.class == self.class && other.name == name end |
#cast(value) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 50 def cast(value) case value when klass then value when ::Hash then build(value) when DATA then deserialize(value) when ::String then deserialize(value) when ::Array then build(columns.keys.zip(value).to_h) else value end end |
#changed_in_place?(raw_old_value, new_value) ⇒ Boolean
91 92 93 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 91 def changed_in_place?(raw_old_value, new_value) deserialize(raw_old_value) != new_value end |
#columns ⇒ Hash[String, type_caster]
The ordered list of columns of the composite type, as a hash of name and type
42 43 44 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 42 def columns klass.columns end |
#deserialize(value) ⇒ Attributes::Composite?
A record that was already serialized comes back as the encoder and its values, which is how it is kept around for dirty tracking
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 63 def deserialize(value) return if value.nil? fields = value.is_a?(DATA) ? value.values : DECODER.decode(value) instance = klass.new set = attributes_of(instance) columns.each_key.with_index do |attr_name, idx| set.write_from_database(attr_name, fields[idx]) end instance end |
#hash ⇒ Integer
100 101 102 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 100 def hash [self.class, name].hash end |
#klass ⇒ singleton(Attributes::Composite)
The class that represents the composite type, loaded in a lazy way. It cannot be memoized here because Rails freezes the types it resolves through the type map, so the lookup owns the cache
36 37 38 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 36 def klass Attributes::Composite.lookup(name) end |
#serialize(value) ⇒ Object
The record is handed over to the adapter as an encoder and its values, the same way arrays are, so that columns are type casted by the connection right before the record literal is built. Values come from the record itself, so that types declared by the class, like encrypted ones, are the ones being applied
82 83 84 85 86 87 88 89 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 82 def serialize(value) return if value.nil? set = attributes_of(cast(value)) values = columns.each_key.map { |attr_name| set[attr_name].value_for_database } DATA.new(ENCODER, values) end |
#type ⇒ :composite
46 47 48 |
# File 'lib/torque/postgresql/adapter/oid/composite.rb', line 46 def type :composite end |