Module: Torque::PostgreSQL::SchemaCache

Includes:
SchemaCache::Inheritance, Inheritance
Included in:
ActiveRecord::ConnectionAdapters::SchemaCache
Defined in:
lib/torque/postgresql/schema_cache.rb,
lib/torque/postgresql/schema_cache/inheritance.rb,
sig/torque/postgresql/schema_cache.rbs,
sig/torque/postgresql/schema_cache/inheritance.rbs

Overview

initialize is not declared: schema_cache/inheritance.rb reopens this module before it is defined, and the runtime checker hooks it eagerly

Defined Under Namespace

Modules: Inheritance

Instance Method Summary collapse

Methods included from Inheritance

#find_model, #prepare_irregular_models

Instance Method Details

#add(connection_or_table_name, table_name = connection_or_table_name) ⇒ Object

:nodoc:

Parameters:

  • connection_or_table_name (source, String)
  • table_name (String) (defaults to: connection_or_table_name)
  • (Object)

Returns:

  • (Object)


45
46
47
48
49
50
51
52
53
54
# File 'lib/torque/postgresql/schema_cache.rb', line 45

def add(connection_or_table_name, table_name = connection_or_table_name, *) # :nodoc:
  super

  # Reset inheritance information when a table is added
  if @data_sources.key?(table_name)
    @inheritance_dependencies.clear
    @inheritance_associations.clear
    @inheritance_loaded = false
  end
end

#add_model_name(*args) ⇒ model?

A way to manually add models name so it doesn't need the lookup method

Parameters:

  • args (Object)

Returns:

  • (model, nil)


98
99
100
101
102
103
# File 'lib/torque/postgresql/schema_cache.rb', line 98

def add_model_name(*args)
  model, *source = args.reverse
  return unless data_source_exists?(*source.reverse) && model.is_a?(Class)

  @data_sources_model_names[source.first] = model
end

#associations(source, table_name = source) ⇒ Array[String]?

Get the list of all tables that are associated (direct or indirect inheritance) with the provided one

Parameters:

  • source (source, String)
  • table_name (String) (defaults to: source)

Returns:

  • (Array[String], nil)


113
114
115
116
# File 'lib/torque/postgresql/schema_cache.rb', line 113

def associations(source, table_name = source)
  reload_inheritance_data!(source == table_name ? connection : source)
  @inheritance_associations[table_name]
end

#clear! ⇒ Object

:nodoc:

Returns:

  • (Object)


56
57
58
59
60
61
62
# File 'lib/torque/postgresql/schema_cache.rb', line 56

def clear! # :nodoc:
  super
  @data_sources_model_names.clear
  @inheritance_dependencies.clear
  @inheritance_associations.clear
  @inheritance_loaded = false
end

#clear_data_source_cache!(connection_or_name, name = connection_or_name) ⇒ Object

:nodoc:

Parameters:

  • connection_or_name (source, String)
  • name (String) (defaults to: connection_or_name)

Returns:

  • (Object)


72
73
74
75
76
77
78
# File 'lib/torque/postgresql/schema_cache.rb', line 72

def clear_data_source_cache!(connection_or_name, name = connection_or_name) # :nodoc:
  super
  @data_sources_model_names.delete name
  @inheritance_dependencies.delete name
  @inheritance_associations.delete name
  @inheritance_loaded = false
end

#dependencies(source, table_name = source) ⇒ Array[String]?

Get all the tables that the given one inherits from

Parameters:

  • source (source, String)
  • table_name (String) (defaults to: source)

Returns:

  • (Array[String], nil)


106
107
108
109
# File 'lib/torque/postgresql/schema_cache.rb', line 106

def dependencies(source, table_name = source)
  reload_inheritance_data!(source == table_name ? connection : source)
  @inheritance_dependencies[table_name]
end

#encode_with(coder) ⇒ Object

:nodoc:

Parameters:

  • coder (Object)

Returns:

  • (Object)


31
32
33
34
35
36
# File 'lib/torque/postgresql/schema_cache.rb', line 31

def encode_with(coder) # :nodoc:
  super
  coder['data_sources_model_names'] = @data_sources_model_names
  coder['inheritance_dependencies'] = @inheritance_dependencies
  coder['inheritance_associations'] = @inheritance_associations
end

#init_with(coder) ⇒ Object

:nodoc:

Parameters:

  • coder (Object)

Returns:

  • (Object)


38
39
40
41
42
43
# File 'lib/torque/postgresql/schema_cache.rb', line 38

def init_with(coder) # :nodoc:
  super
  @data_sources_model_names = coder['data_sources_model_names']
  @inheritance_dependencies = coder['inheritance_dependencies']
  @inheritance_associations = coder['inheritance_associations']
end

#initialize ⇒ Object

:nodoc:



15
16
17
18
19
20
21
22
# File 'lib/torque/postgresql/schema_cache.rb', line 15

def initialize(*) # :nodoc:
  super

  @data_sources_model_names = {}
  @inheritance_dependencies = {}
  @inheritance_associations = {}
  @inheritance_loaded = false
end

#initialize_dup ⇒ Object

:nodoc:

Parameters:

  • (Object)

Returns:

  • (Object)


24
25
26
27
28
29
# File 'lib/torque/postgresql/schema_cache.rb', line 24

def initialize_dup(*) # :nodoc:
  super
  @data_sources_model_names = @data_sources_model_names.dup
  @inheritance_dependencies = @inheritance_dependencies.dup
  @inheritance_associations = @inheritance_associations.dup
end

#lookup_model(*args, **xargs) ⇒ model

Override the inheritance implementation to pass over the proper cache of the existing association between data sources and model names

Parameters:

  • table_name (String)
  • scoped_class (String, model)

Returns:

  • (model)


120
121
122
# File 'lib/torque/postgresql/schema_cache.rb', line 120

def lookup_model(*args, **xargs)
  super(*args, **xargs, source_to_model: @data_sources_model_names)
end

#marshal_dump ⇒ Array[untyped]

:nodoc:

Returns:

  • (Array[untyped])


80
81
82
83
84
85
86
87
# File 'lib/torque/postgresql/schema_cache.rb', line 80

def marshal_dump # :nodoc:
  super + [
    @inheritance_dependencies,
    @inheritance_associations,
    @data_sources_model_names,
    @inheritance_loaded,
  ]
end

#marshal_load(array) ⇒ Object

:nodoc:

Parameters:

  • array (Array[untyped])

Returns:

  • (Object)


89
90
91
92
93
94
95
# File 'lib/torque/postgresql/schema_cache.rb', line 89

def marshal_load(array) # :nodoc:
  @inheritance_loaded = array.pop
  @data_sources_model_names = array.pop
  @inheritance_associations = array.pop
  @inheritance_dependencies = array.pop
  super
end

#size ⇒ Integer

:nodoc:

Returns:

  • (Integer)


64
65
66
67
68
69
70
# File 'lib/torque/postgresql/schema_cache.rb', line 64

def size # :nodoc:
  super + [
    @data_sources_model_names,
    @inheritance_dependencies,
    @inheritance_associations,
  ].map(&:size).inject(:+)
end