module RailsMultitenant::GlobalContextRegistry

Constants

EMPTY_ARRAY

Public Instance Methods

[](symbol)
Alias for: get
[]=(symbol, value)
Alias for: set
delete(symbol) click to toggle source

delete this global

# File lib/rails_multitenant/global_context_registry.rb, line 26
def delete(symbol)
  globals.delete(symbol)
end
disable_scoped_queries() click to toggle source

Prefer .with_unscoped_queries to the following two methods. Note: these methods are intended for use in a manner like .with_admin_registry, but in contexts where around semantics are not allowed.

# File lib/rails_multitenant/global_context_registry.rb, line 106
def disable_scoped_queries
  self[:__use_unscoped_queries] = true
end
duplicate_registry() click to toggle source

Duplicate the registry

# File lib/rails_multitenant/global_context_registry.rb, line 52
def duplicate_registry
  globals.transform_values do |value|
    value.nil? || value.is_a?(Integer) ? value : value.dup
  end
end
enable_scoped_queries() click to toggle source
# File lib/rails_multitenant/global_context_registry.rb, line 110
def enable_scoped_queries
  self[:__use_unscoped_queries] = nil
end
fetch(symbol) { || ... } click to toggle source

Pass with a generator block for the value

# File lib/rails_multitenant/global_context_registry.rb, line 31
def fetch(symbol)
  result = globals[symbol]
  unless result
    result = yield
    globals[symbol] = result
  end
  result
end
get(symbol) click to toggle source

get the global identified by the symbol

# File lib/rails_multitenant/global_context_registry.rb, line 41
def get(symbol)
  globals[symbol]
end
Also aliased as: []
merge!(values) click to toggle source

merge the given values into the registry

# File lib/rails_multitenant/global_context_registry.rb, line 47
def merge!(values)
  globals.merge!(values)
end
new_registry(registry = {}) click to toggle source

Set a new, by default empty registry, returning the previous one.

# File lib/rails_multitenant/global_context_registry.rb, line 79
def new_registry(registry = {})
  priors = globals
  self.globals = registry
  priors
end
replace_registry(registry) click to toggle source

Replace the registry with one you previously took away with .new_registry

# File lib/rails_multitenant/global_context_registry.rb, line 86
def replace_registry(registry)
  self.globals = registry
end
set(symbol, value) click to toggle source

Set this global

# File lib/rails_multitenant/global_context_registry.rb, line 20
def set(symbol, value)
  globals[symbol] = value
end
Also aliased as: []=
use_unscoped_queries?() click to toggle source
# File lib/rails_multitenant/global_context_registry.rb, line 98
def use_unscoped_queries?
  self[:__use_unscoped_queries] == true
end
with_isolated_registry(registry = {}) { || ... } click to toggle source

Run a block of code with an the given registry

# File lib/rails_multitenant/global_context_registry.rb, line 59
def with_isolated_registry(registry = {})
  prior_globals = new_registry(registry)
  yield
ensure
  self.globals = prior_globals
end
with_merged_registry(values = {}) { || ... } click to toggle source

Run a block of code with the given values merged into the current registry

# File lib/rails_multitenant/global_context_registry.rb, line 67
def with_merged_registry(values = {})
  prior_globals = new_registry(globals.merge(values))
  yield
ensure
  self.globals = prior_globals
end
with_unscoped_queries() { || ... } click to toggle source

Run a block of code that disregards scoping during read queries

# File lib/rails_multitenant/global_context_registry.rb, line 91
def with_unscoped_queries
  # disabling Style/ExplicitBlockArgument for performance reasons
  with_merged_registry(__use_unscoped_queries: true) do
    yield
  end
end

Private Instance Methods

add_dependency(parent, dependent) click to toggle source
# File lib/rails_multitenant/global_context_registry.rb, line 118
def add_dependency(parent, dependent)
  parent = parent.respond_to?(:name) ? parent.name : parent
  dependent = dependent.respond_to?(:name) ? dependent.name : dependent

  raise 'dependencies cannot be registered for anonymous classes' if parent.blank? || dependent.blank?

  ((@dependencies[parent] ||= []) << dependent).tap(&:uniq!)
end
dependencies_for(klass) click to toggle source
# File lib/rails_multitenant/global_context_registry.rb, line 127
def dependencies_for(klass)
  @dependencies[klass.name]&.map(&:safe_constantize)&.tap(&:compact!) || EMPTY_ARRAY
end
globals() click to toggle source
# File lib/rails_multitenant/global_context_registry.rb, line 131
def globals
  registry = Thread.current[:global_context_registry]
  unless registry
    registry = {}
    Thread.current[:global_context_registry] = registry
  end
  registry
end
globals=(value) click to toggle source
# File lib/rails_multitenant/global_context_registry.rb, line 140
def globals=(value)
  Thread.current[:global_context_registry] = value
end