Class: PostgresCompositeType

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/postgres_composite_type.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ PostgresCompositeType

Returns a new instance of PostgresCompositeType.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/postgres_composite_type.rb', line 63

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

.columnsObject (readonly)

Column definition read from db schema



10
11
12
# File 'lib/postgres_composite_type.rb', line 10

def columns
  @columns
end

.typeObject (readonly)

The PostgreSQL type name as symbol



8
9
10
# File 'lib/postgres_composite_type.rb', line 8

def type
  @type
end

Class Method Details

.connected?Boolean

:nodoc:

Returns:

  • (Boolean)


45
46
47
# File 'lib/postgres_composite_type.rb', line 45

def connected?
  (@connection_class || ActiveRecord::Base).connected?
end

.connectionObject

:nodoc:



40
41
42
# File 'lib/postgres_composite_type.rb', line 40

def connection
  (@connection_class || ActiveRecord::Base).connection
end

.initialize_column_definitionObject

:nodoc:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/postgres_composite_type.rb', line 50

def initialize_column_definition
  unless @columns
    @columns = self.connection.columns(type)
    attr_reader *@columns.map(&:name)
    @columns.each do |column|
     define_method("#{column.name}=") do |value|
      instance_variable_set "@#{column.name}", type_cast_field_from_user(column, value)
     end
    end
  end
end

.register_type(type) ⇒ Object

Link PostgreSQL type given by the name with this class. Usage:

class ComplexType < PostgresCompositeType

register_type :complex

end

Parameters:

  • :type (Symbol)

    the PostgreSQL type name



20
21
22
23
# File 'lib/postgres_composite_type.rb', line 20

def register_type(type)
  @type = type.to_sym
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.register_composite_type_class(self)
end

.use_connection_class(active_record_class) ⇒ Object

Be default the ActiveRecord::Base connection is used when reading type definition. If you want to use connection linked with another class use this method. Usage

class ComplexType < PostgresCompositeType

register_type :complex
use_connection_class MyRecordConnectedToDifferentDB

end

Parameters:

  • :active_record_class (Class)

    the ActiveRecord model class



35
36
37
# File 'lib/postgres_composite_type.rb', line 35

def use_connection_class(active_record_class)
  @connection_class = active_record_class
end

Instance Method Details

#<=>(another) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/postgres_composite_type.rb', line 77

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

#quoted_valueObject

Used for AREL eq node



88
89
90
# File 'lib/postgres_composite_type.rb', line 88

def quoted_value
 "'" + ActiveRecord::ConnectionAdapters::PostgreSQLColumn::composite_type_to_string(self, self.class.connection) + "'::#{self.class.type}"
end