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
-
#add(table_name) ⇒ Object
:nodoc:.
-
#add_model_name(table_name, model) ⇒ Object
A way to manually add models name so it doesn’t need the lookup method.
-
#associations(table_name) ⇒ Object
Get the list of all tables that are associated (direct or indirect inheritance) with the provided one.
-
#clear! ⇒ Object
:nodoc:.
-
#clear_data_source_cache!(name) ⇒ Object
:nodoc:.
-
#dependencies(table_name) ⇒ Object
Get all the tables that the given one inherits from.
-
#encode_with(coder) ⇒ Object
:nodoc:.
-
#init_with(coder) ⇒ Object
:nodoc:.
-
#initialize ⇒ Object
:nodoc:.
-
#initialize_dup ⇒ Object
:nodoc:.
-
#lookup_model(table_name, scoped_class = '') ⇒ Object
Try to find a model based on a given table.
-
#marshal_dump ⇒ Object
:nodoc:.
-
#marshal_load(array) ⇒ Object
:nodoc:.
-
#size ⇒ Object
:nodoc:.
Instance Method Details
#add(table_name) ⇒ Object
:nodoc:
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/torque/postgresql/schema_cache.rb', line 40 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
92 93 94 95 |
# File 'lib/torque/postgresql/schema_cache.rb', line 92 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
105 106 107 108 |
# File 'lib/torque/postgresql/schema_cache.rb', line 105 def associations(table_name) reload_inheritance_data! @inheritance_associations[table_name] end |
#clear! ⇒ Object
:nodoc:
51 52 53 54 55 56 57 |
# File 'lib/torque/postgresql/schema_cache.rb', line 51 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:
67 68 69 70 71 72 |
# File 'lib/torque/postgresql/schema_cache.rb', line 67 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
98 99 100 101 |
# File 'lib/torque/postgresql/schema_cache.rb', line 98 def dependencies(table_name) reload_inheritance_data! @inheritance_dependencies[table_name] end |
#encode_with(coder) ⇒ Object
:nodoc:
26 27 28 29 30 31 |
# File 'lib/torque/postgresql/schema_cache.rb', line 26 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:
33 34 35 36 37 38 |
# File 'lib/torque/postgresql/schema_cache.rb', line 33 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:
10 11 12 13 14 15 16 17 |
# File 'lib/torque/postgresql/schema_cache.rb', line 10 def initialize(*) # :nodoc: super @data_sources_model_names = {} @inheritance_dependencies = {} @inheritance_associations = {} @inheritance_loaded = false end |
#initialize_dup ⇒ Object
:nodoc:
19 20 21 22 23 24 |
# File 'lib/torque/postgresql/schema_cache.rb', line 19 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
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 136 137 138 |
# File 'lib/torque/postgresql/schema_cache.rb', line 111 def lookup_model(table_name, scoped_class = '') # byebug if table_name == 'activities' 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.chomp('::').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_dump ⇒ Object
:nodoc:
74 75 76 77 78 79 80 81 |
# File 'lib/torque/postgresql/schema_cache.rb', line 74 def marshal_dump # :nodoc: super + [ @inheritance_dependencies, @inheritance_associations, @data_sources_model_names, @inheritance_loaded, ] end |
#marshal_load(array) ⇒ Object
:nodoc:
83 84 85 86 87 88 89 |
# File 'lib/torque/postgresql/schema_cache.rb', line 83 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 ⇒ Object
:nodoc:
59 60 61 62 63 64 65 |
# File 'lib/torque/postgresql/schema_cache.rb', line 59 def size # :nodoc: super + [ @data_sources_model_names, @inheritance_dependencies, @inheritance_associations, ].map(&:size).inject(:+) end |