Class: Torque::PostgreSQL::Attributes::Composite

Inherits:
Base
  • Object
show all
Defined in:
lib/torque/postgresql/attributes/composite.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Base

#==, #[], #[]=, columns_hash, #empty?, #hash, #inspect, #read_attribute_before_type_cast, #read_attribute_for_database, #to_h, #type_for_attribute

Class Attribute Details

.type_nameObject

The name of the composite type on the database



32
33
34
# File 'lib/torque/postgresql/attributes/composite.rb', line 32

def type_name
  @type_name ||= name.demodulize.underscore
end

Class Method Details

.columnsObject

The ordered list of columns of the composite type, as a hash of name and type, loaded in a lazy way



38
39
40
41
# File 'lib/torque/postgresql/attributes/composite.rb', line 38

def columns
  load_columns
  @columns
end

.encrypts(*names, **options) ⇒ Object

Columns have to be in place before any of them can be decorated



74
75
76
77
# File 'lib/torque/postgresql/attributes/composite.rb', line 74

def encrypts(*names, **options)
  load_columns
  super
end

.enum(*args, **xargs) ⇒ Object



79
80
81
82
# File 'lib/torque/postgresql/attributes/composite.rb', line 79

def enum(*args, **xargs)
  load_columns
  super
end

.inherited(subclass) ⇒ Object



26
27
28
29
# File 'lib/torque/postgresql/attributes/composite.rb', line 26

def inherited(subclass)
  super
  subclass.instance_variable_set(:@load_columns_monitor, ::Monitor.new)
end

.load_columnsObject

Load the columns from the database and define any attribute that was not manually declared



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/torque/postgresql/attributes/composite.rb', line 45

def load_columns
  return if self == Composite || defined?(@columns)

  @load_columns_monitor.synchronize do
    next if defined?(@columns)

    columns = connection.composite_column_types(type_name)
    columns.each do |attr_name, attr_type|
      attribute(attr_name, attr_type) unless attribute_names.include?(attr_name)
    end

    @columns = columns
  end
end

.lookup(name) ⇒ Object

Find or create the class that will handle the composite type. It first checks the irregular types mapping, then the namespace



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/torque/postgresql/attributes/composite.rb', line 14

def lookup(name)
  name = name.to_s.underscore
  mapping = PostgreSQL.config.composite.irregular_types
  return resolve(mapping[name].constantize, name) if mapping.key?(name)

  const     = name.camelize
  namespace = PostgreSQL.config.composite.namespace

  return resolve(namespace.const_get(const), name) if namespace.const_defined?(const)
  resolve(namespace.const_set(const, Class.new(Composite)), name)
end

.new(*args, **xargs, &block) ⇒ Object



68
69
70
71
# File 'lib/torque/postgresql/attributes/composite.rb', line 68

def new(*args, **xargs, &block)
  load_columns
  super
end

.reset_columns!Object

Drop the loaded columns, so that they are collected again on the next use, which happens after the type is changed



62
63
64
65
66
# File 'lib/torque/postgresql/attributes/composite.rb', line 62

def reset_columns!
  @load_columns_monitor.synchronize do
    remove_instance_variable(:@columns) if defined?(@columns)
  end
end