Module: Torque::PostgreSQL::SchemaCache

Defined in:
lib/torque/postgresql/schema_cache.rb

Overview

:TODO: Create the add to load inheritance info

Instance Method Summary collapse

Instance Method Details

#add(table_name) ⇒ Object

:nodoc:



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

def add(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(table_name, model) ⇒ Object

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



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

def add_model_name(table_name, model)
  return unless data_source_exists?(table_name) && model.is_a?(Class)
  @data_sources_model_names[table_name] = model
end

#associations(table_name) ⇒ Object

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



103
104
105
106
# File 'lib/torque/postgresql/schema_cache.rb', line 103

def associations(table_name)
  reload_inheritance_data!
  @inheritance_associations[table_name]
end

#clear!Object

:nodoc:



49
50
51
52
53
54
55
# File 'lib/torque/postgresql/schema_cache.rb', line 49

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

#clear_data_source_cache!(name) ⇒ Object

:nodoc:



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

def clear_data_source_cache!(name) # :nodoc:
  super
  @data_sources_model_names.delete name
  @inheritance_dependencies.delete name
  @inheritance_associations.delete name
end

#dependencies(table_name) ⇒ Object

Get all the tables that the given one inherits from



96
97
98
99
# File 'lib/torque/postgresql/schema_cache.rb', line 96

def dependencies(table_name)
  reload_inheritance_data!
  @inheritance_dependencies[table_name]
end

#encode_with(coder) ⇒ Object

:nodoc:



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

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:



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

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:



8
9
10
11
12
13
14
15
# File 'lib/torque/postgresql/schema_cache.rb', line 8

def initialize(*) # :nodoc:
  super

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

#initialize_dupObject

:nodoc:



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

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(table_name, scoped_class = '') ⇒ Object

Try to find a model based on a given table

Raises:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/torque/postgresql/schema_cache.rb', line 109

def lookup_model(table_name, scoped_class = '')
  scoped_class = scoped_class.name if scoped_class.is_a?(Class)
  return @data_sources_model_names[table_name] \
    if @data_sources_model_names.key?(table_name)

  # Get all the possible scopes
  scopes = scoped_class.scan(/(?:::)?[A-Z][a-z]+/)
  scopes.unshift('Object::')

  # Consider the maximum namespaced possible model name
  max_name = table_name.tr('_', '/').camelize.split(/(::)/)
  max_name[-1] = max_name[-1].singularize

  # Test all the possible names against all the possible scopes
  until scopes.size == 0
    scope = scopes.join.safe_constantize
    model = find_model(max_name, table_name, scope) unless scope.nil?
    return @data_sources_model_names[table_name] = model unless model.nil?
    scopes.pop
  end

  # If this part is reach, no model name was found
  raise LookupError.new(<<~MSG.squish)
    Unable to find a valid model that is associated with the '#{table_name}' table.
    Please, check if they correctly inherit from ActiveRecord::Base
  MSG
end

#marshal_dumpObject

:nodoc:



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

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

#marshal_load(array) ⇒ Object

:nodoc:



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

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:



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

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