Module: RailsMultitenant::GlobalContextRegistry::Current::ClassMethods

Includes:
RegistryDependentOn
Defined in:
lib/rails_multitenant/global_context_registry/current.rb

Instance Method Summary collapse

Methods included from RegistryDependentOn

#global_context_dependent_on, #global_context_mutually_dependent_on

Instance Method Details

#as_current(object) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/rails_multitenant/global_context_registry/current.rb', line 42

def as_current(object)
  old_object = current if current?
  self.current = object
  yield
ensure
  self.current = old_object
end

#clear_current!(cleared = nil) ⇒ Object



37
38
39
40
# File 'lib/rails_multitenant/global_context_registry/current.rb', line 37

def clear_current!(cleared = nil)
  GlobalContextRegistry.delete(current_registry_obj)
  __clear_dependents!(cleared)
end

#currentObject



18
19
20
# File 'lib/rails_multitenant/global_context_registry/current.rb', line 18

def current
  GlobalContextRegistry.fetch(current_registry_obj) { __current_default }
end

#current!Object



33
34
35
# File 'lib/rails_multitenant/global_context_registry/current.rb', line 33

def current!
  current || raise("No current #{name} set")
end

#current=(object) ⇒ Object



22
23
24
25
26
27
# File 'lib/rails_multitenant/global_context_registry/current.rb', line 22

def current=(object)
  raise "#{object} is not a #{self}" if object.present? && !object.is_a?(self)

  GlobalContextRegistry.set(current_registry_obj, object)
  __clear_dependents!
end

#current?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/rails_multitenant/global_context_registry/current.rb', line 29

def current?
  GlobalContextRegistry.get(current_registry_obj).present?
end

#provide_default(provider = nil, &block) ⇒ Object



52
53
54
# File 'lib/rails_multitenant/global_context_registry/current.rb', line 52

def provide_default(provider = nil, &block)
  self.default_provider = provider ? provider.to_proc : block
end