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

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

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Base

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

Methods included from SimpleEnum::ClassMethods

#defined_enums, #defined_enums=, #enum

Methods included from SimpleEnum

#defined_enums

Class Attribute Details

.type_name ⇒ String

The name of the composite type on the database

Returns:

  • (String)


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

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

Class Method Details

.columns ⇒ Hash[String, type_caster]?

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

Returns:

  • (Hash[String, type_caster], nil)


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

Parameters:

  • names (column_ref)
  • options (Object)

Returns:

  • (Object)


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

Parameters:

  • name (column_ref)
  • values (SimpleEnum::values)
  • options (Object)

Returns:

  • (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

Parameters:

Returns:

  • (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_columns ⇒ Object

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

Returns:

  • (Object)


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 = ActiveRecord::Base.with_connection { |c| c.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) ⇒ singleton(Composite)

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

Parameters:

  • name (table_ref)

Returns:



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) {|arg0| ... } ⇒ instance

Parameters:

  • args (Object)
  • xargs (Object)

Yields:

Yield Parameters:

  • arg0

Yield Returns:

  • (Object)

Returns:

  • (instance)


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

Returns:

  • (Object)


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