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

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



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

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



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

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

#clear!Object

:nodoc:



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

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

#clear_data_source_cache!(name) ⇒ Object

:nodoc:



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

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

#dependencies(table_name) ⇒ Object

Get all the tables that the given one inherits from



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

def dependencies(table_name)
  reload_inheritance_data!
  @inheritance_dependencies[table_name]
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 = {}
  @cached_data_sources_size = 0
end

#initialize_dupObject

:nodoc:



17
18
19
20
21
22
23
# 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
  @cached_data_sources_size = @cached_data_sources_size.dup
end

#lookup_model(table_name, scopred_class = '') ⇒ Object

Try to find a model based on a given table

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/torque/postgresql/schema_cache.rb', line 86

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:



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

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

#marshal_load(array) ⇒ Object

:nodoc:



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

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

#sizeObject

:nodoc:



33
34
35
36
37
38
39
# File 'lib/torque/postgresql/schema_cache.rb', line 33

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