Module: RailsMultitenant::GlobalContextRegistry

Extended by:
GlobalContextRegistry
Included in:
GlobalContextRegistry
Defined in:
lib/rails_multitenant/global_context_registry.rb,
lib/rails_multitenant/global_context_registry/current.rb,
lib/rails_multitenant/global_context_registry/current_instance.rb,
lib/rails_multitenant/global_context_registry/registry_dependent_on.rb

Defined Under Namespace

Modules: Current, CurrentInstance, RegistryDependentOn

Constant Summary collapse

EMPTY_ARRAY =
[].freeze

Instance Method Summary collapse

Instance Method Details

#delete(symbol) ⇒ Object

delete this global



26
27
28
# File 'lib/rails_multitenant/global_context_registry.rb', line 26

def delete(symbol)
  globals.delete(symbol)
end

#disable_scoped_queriesObject

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.



106
107
108
# File 'lib/rails_multitenant/global_context_registry.rb', line 106

def disable_scoped_queries
  self[:__use_unscoped_queries] = true
end

#duplicate_registryObject

Duplicate the registry



52
53
54
55
56
# 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_queriesObject



110
111
112
# File 'lib/rails_multitenant/global_context_registry.rb', line 110

def enable_scoped_queries
  self[:__use_unscoped_queries] = nil
end

#fetch(symbol) ⇒ Object

Pass with a generator block for the value



31
32
33
34
35
36
37
38
# 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) ⇒ Object Also known as: []

get the global identified by the symbol



41
42
43
# File 'lib/rails_multitenant/global_context_registry.rb', line 41

def get(symbol)
  globals[symbol]
end

#merge!(values) ⇒ Object

merge the given values into the registry



47
48
49
# File 'lib/rails_multitenant/global_context_registry.rb', line 47

def merge!(values)
  globals.merge!(values)
end

#new_registry(registry = {}) ⇒ Object

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



79
80
81
82
83
# File 'lib/rails_multitenant/global_context_registry.rb', line 79

def new_registry(registry = {})
  priors = globals
  self.globals = registry
  priors
end

#replace_registry(registry) ⇒ Object

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



86
87
88
# File 'lib/rails_multitenant/global_context_registry.rb', line 86

def replace_registry(registry)
  self.globals = registry
end

#set(symbol, value) ⇒ Object Also known as: []=

Set this global



20
21
22
# File 'lib/rails_multitenant/global_context_registry.rb', line 20

def set(symbol, value)
  globals[symbol] = value
end

#use_unscoped_queries?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/rails_multitenant/global_context_registry.rb', line 98

def use_unscoped_queries?
  self[:__use_unscoped_queries] == true
end

#with_isolated_registry(registry = {}) ⇒ Object

Run a block of code with an the given registry



59
60
61
62
63
64
# 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 = {}) ⇒ Object

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



67
68
69
70
71
72
# 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_queriesObject

Run a block of code that disregards scoping during read queries



91
92
93
94
95
96
# 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