Class: PostgresCompositeType
- Inherits:
-
Object
- Object
- PostgresCompositeType
- Includes:
- Comparable
- Defined in:
- lib/activerecord-postgres-composite-types/postgres_composite_type.rb
Class Attribute Summary collapse
-
.columns ⇒ Object
readonly
TODO: doc.
-
.type ⇒ Object
readonly
TODO: doc.
Class Method Summary collapse
- .connected? ⇒ Boolean
- .connection ⇒ Object
- .initialize_column_definition ⇒ Object
- .register_type(type) ⇒ Object
- .use_connection_class(active_record_class) ⇒ Object
Instance Method Summary collapse
- #<=>(another) ⇒ Object
-
#initialize(value) ⇒ PostgresCompositeType
constructor
A new instance of PostgresCompositeType.
Constructor Details
#initialize(value) ⇒ PostgresCompositeType
Returns a new instance of PostgresCompositeType.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/activerecord-postgres-composite-types/postgres_composite_type.rb', line 35 def initialize(value) self.class.initialize_column_definition case value when String ActiveRecord::ConnectionAdapters::PostgreSQLColumn.string_to_composite_type(self.class, value) when Array set_values value when Hash set_attributes value else raise "Unexpected value: #{value.inspect}" end end |
Class Attribute Details
.columns ⇒ Object (readonly)
TODO: doc
8 9 10 |
# File 'lib/activerecord-postgres-composite-types/postgres_composite_type.rb', line 8 def columns @columns end |
.type ⇒ Object (readonly)
TODO: doc
6 7 8 |
# File 'lib/activerecord-postgres-composite-types/postgres_composite_type.rb', line 6 def type @type end |
Class Method Details
.connected? ⇒ Boolean
23 24 25 |
# File 'lib/activerecord-postgres-composite-types/postgres_composite_type.rb', line 23 def connected? (@connection_class || ActiveRecord::Base).connected? end |
.connection ⇒ Object
19 20 21 |
# File 'lib/activerecord-postgres-composite-types/postgres_composite_type.rb', line 19 def connection (@connection_class || ActiveRecord::Base).connection end |
.initialize_column_definition ⇒ Object
27 28 29 30 31 32 |
# File 'lib/activerecord-postgres-composite-types/postgres_composite_type.rb', line 27 def initialize_column_definition unless @columns @columns = self.connection.columns(type) attr_accessor *@columns.map(&:name) end end |
.register_type(type) ⇒ Object
10 11 12 13 |
# File 'lib/activerecord-postgres-composite-types/postgres_composite_type.rb', line 10 def register_type(type) @type = type.to_sym ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.register_composite_type_class(self) end |
.use_connection_class(active_record_class) ⇒ Object
15 16 17 |
# File 'lib/activerecord-postgres-composite-types/postgres_composite_type.rb', line 15 def use_connection_class(active_record_class) @connection_class = active_record_class end |
Instance Method Details
#<=>(another) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/activerecord-postgres-composite-types/postgres_composite_type.rb', line 50 def <=>(another) return nil if (self.class <=> another.class) == nil self.class.columns.each do |column| v1 = self.send(column.name) v2 = another.send(column.name) return v1 <=> v2 unless v1 == v2 end 0 end |