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:



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

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
  end
end

#add_model_name(table_name, model) ⇒ Object

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



85
86
87
88
# File 'lib/torque/postgresql/schema_cache.rb', line 85

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



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

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

#clear!Object

:nodoc:



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

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

#clear_data_source_cache!(name) ⇒ Object

:nodoc:



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

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



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

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

#encode_with(coder) ⇒ Object

:nodoc:



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

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:



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

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
# File 'lib/torque/postgresql/schema_cache.rb', line 8

def initialize(*) # :nodoc:
  super

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

#initialize_dupObject

:nodoc:



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

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, scopred_class = '') ⇒ Object

Try to find a model based on a given table

Raises:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/torque/postgresql/schema_cache.rb', line 104

def lookup_model(table_name, scopred_class = '')
  scopred_class = scopred_class.name if scopred_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 = scopred_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:



69
70
71
72
73
74
75
# File 'lib/torque/postgresql/schema_cache.rb', line 69

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

#marshal_load(array) ⇒ Object

:nodoc:



77
78
79
80
81
82
# File 'lib/torque/postgresql/schema_cache.rb', line 77

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

#sizeObject

:nodoc:



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

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