Class: Ekylibre::MultiTenancy::TenantSwitcher
- Inherits:
-
Object
- Object
- Ekylibre::MultiTenancy::TenantSwitcher
- Defined in:
- lib/ekylibre/multi_tenancy/tenant_switcher.rb
Overview
Service allowing to switch between tenants and keeping track of what need to be cleaned through the use of a TenantStack
Instance Method Summary collapse
-
#initialize(apartment:, tenant_stack:, tenant_repository:) ⇒ TenantSwitcher
constructor
A new instance of TenantSwitcher.
- #leave! ⇒ Object
- #switch(name, &block) ⇒ Object
- #switch!(name) ⇒ Object
Constructor Details
#initialize(apartment:, tenant_stack:, tenant_repository:) ⇒ TenantSwitcher
Returns a new instance of TenantSwitcher.
18 19 20 21 22 23 24 |
# File 'lib/ekylibre/multi_tenancy/tenant_switcher.rb', line 18 def initialize(apartment:, tenant_stack:, tenant_repository:) @apartment = apartment @tenant_stack = tenant_stack @tenant_repository = tenant_repository @bang_stack = [] @bang_switched = false end |
Instance Method Details
#leave! ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/ekylibre/multi_tenancy/tenant_switcher.rb', line 61 def leave! if @bang_switched @bang_switched = false tenant_stack.pop apartment.switch!(tenant_stack.current) else raise StandardError.new('Cannot leave! if not after switch!') end end |
#switch(name, &block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ekylibre/multi_tenancy/tenant_switcher.rb', line 27 def switch(name, &block) name = name.to_s tenant = tenant_repository.get(name) return if tenant.nil? begin @bang_stack << @bang_switched @bang_switched = false tenant_stack.push(tenant) apartment.switch(name, &block) ensure # Make sure we pop the stack twice if we bang_switched leave! if @bang_switched @bang_switched = @bang_stack.pop tenant_stack.pop end end |
#switch!(name) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ekylibre/multi_tenancy/tenant_switcher.rb', line 48 def switch!(name) name = name.to_s tenant = tenant_repository.get(name) return if tenant.nil? # Some housekeeping to keep the tenant_stack up-to-date tenant_stack.pop if @bang_switched @bang_switched = true tenant_stack.push(tenant) apartment.switch!(name) end |