Module: Torque::PostgreSQL::SchemaCache

Includes:
Inheritance
Defined in:
lib/torque/postgresql/schema_cache.rb,
lib/torque/postgresql/schema_cache/inheritance.rb

Overview

:TODO: Create the add to load inheritance info

Defined Under Namespace

Modules: Inheritance

Instance Method Summary collapse

Instance Method Details

#add(connection_or_table_name, table_name = connection_or_table_name) ⇒ Object

:nodoc:



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) ⇒ Object

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



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

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) ⇒ Object

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



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

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

#clear!Object

:nodoc:



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:



72
73
74
75
76
77
# 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
end

#dependencies(source, table_name = source) ⇒ Object

Get all the tables that the given one inherits from



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

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

#encode_with(coder) ⇒ Object

:nodoc:



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:



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

#initializeObject

: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_dupObject

:nodoc:



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) ⇒ Object

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



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

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

#marshal_dumpObject

:nodoc:



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

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

#marshal_load(array) ⇒ Object

:nodoc:



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

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

#sizeObject

:nodoc:



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