lib/penthouse/runners/base_runner.rb in penthouse-0.11.0 vs lib/penthouse/runners/base_runner.rb in penthouse-0.12.0

- old
+ new

@@ -9,27 +9,46 @@ module Penthouse module Runners class BaseRunner + PENTHOUSE_RUNNER_CALL_STACK = :current_penthouse_runner_call_stack + # @param tenant_identifier [String, Symbol] The identifier for the tenant # @param block [Block] The code to execute within the tenant # @return [void] # @raise [Penthouse::TenantNotFound] if the tenant cannot be switched to - def self.call(tenant_identifier, &block) - load_tenant(tenant_identifier).call do |tenant| - Penthouse.with_tenant(tenant.identifier) do - block.yield(tenant) + def call(tenant_identifier:, &block) + previous_tenant_identifier = call_stack.last || 'public' + call_stack.push(tenant_identifier) + + result = nil + + begin + load_tenant(tenant_identifier: tenant_identifier, previous_tenant_identifier: previous_tenant_identifier).call do |tenant| + Penthouse.with_tenant(tenant_identifier: tenant.identifier) do + result = block.yield(tenant) + end end + ensure + call_stack.pop end + result end # @abstract returns the tenant object # @param tenant_identifier [String, Symbol] The identifier for the tenant # @return [Penthouse::Tenants::BaseTenant] An instance of a tenant # @raise [Penthouse::TenantNotFound] if the tenant cannot be switched to - def self.load_tenant(tenant_identifier) + def load_tenant(tenant_identifier:, previous_tenant_identifier: 'public') raise NotImplementedError end + + private + + def call_stack + Thread.current[PENTHOUSE_RUNNER_CALL_STACK] ||= [] + end + end end end