lib/penthouse.rb in penthouse-0.7.5 vs lib/penthouse.rb in penthouse-0.8.0

- old
+ new

@@ -4,33 +4,34 @@ require "penthouse/routers/base_router" require "penthouse/runners/base_runner" module Penthouse class TenantNotFound < RuntimeError; end + CURRENT_TENANT_KEY = 'penthouse_tenant'.freeze class << self # Retrieves the currently active tenant identifier # @return [String, Symbol] the current tenant name def tenant - Thread.current[:tenant] + Thread.current[CURRENT_TENANT_KEY] end # Sets the currently active tenant identifier # @param tenant_identifier [String, Symbol] the identifier for the tenant # @return [void] def tenant=(tenant_identifier) - Thread.current[:tenant] = tenant_identifier + Thread.current[CURRENT_TENANT_KEY] = tenant_identifier end # Similar to Penthouse.tenant=, except this will switch back after the given # block has finished executing # @param tenant_identifier [String, Symbol] the identifier for the tenant # @param default_tenant [String, Symbol] the identifier for the tenant to return to # @param block [Block] the code to execute # @yield [String, Symbol] the identifier for the tenant # @return [void] - def with_tenant(tenant_identifier, default_tenant: tenant, &block) + def with_tenant(tenant_identifier, default_tenant: self.tenant, &block) self.tenant = tenant_identifier block.yield(tenant_identifier) ensure self.tenant = default_tenant end @@ -40,41 +41,41 @@ # @param tenant_identifiers [Array<String, Symbol>, nil] the array of tenants to loop through # @param default_tenant [String, Symbol] the identifier for the tenant to return to # @param block [Block] the code to execute # @yield [String, Symbol] the identifier for the tenant # @return [void] - def each_tenant(tenant_identifiers: nil, default_tenant: tenant, runner: configuration.runner, &block) - (tenant_identifiers || self.tenant_identifiers).each do |tenant_identifier| + def each_tenant(tenant_identifiers: self.tenant_identifiers, default_tenant: self.tenant, runner: self.configuration.runner, &block) + tenant_identifiers.each do |tenant_identifier| switch(tenant_identifier, runner: runner, &block) end end # Executes the given block of code within a given tenant # @param tenant_identifier [String, Symbol] the identifier for the tenant # @param runner [Penthouse::Runners::BaseRunner] an optional runner to use, defaults to the one configured # @param block [Block] the code to execute # @yield [Penthouse::Tenants::BaseTenant] the tenant instance # @return [void] - def switch(tenant_identifier, runner: configuration.runner, &block) + def switch(tenant_identifier, runner: self.configuration.runner, &block) runner.call(tenant_identifier, &block) end # Loads the tenant and creates their data store # @param tenant_identifier [String, Symbol] the identifier for the tenant # @see Penthouse::Tenants::BaseTenant#delete # @return [void] - def create(tenant_identifier, runner: configuration.runner, **options) + def create(tenant_identifier, runner: self.configuration.runner, **options) switch(tenant_identifier, runner: runner) do |tenant| tenant.create(**options) end end # Loads the tenant and deletes their data store # @param tenant_identifier [String, Symbol] the identifier for the tenant # @see Penthouse::Tenants::BaseTenant#delete # @return [void] - def delete(tenant_identifier, runner: configuration.runner, **options) + def delete(tenant_identifier, runner: self.configuration.runner, **options) switch(tenant_identifier, runner: runner) do |tenant| tenant.delete(**options) end end @@ -95,12 +96,18 @@ router: Routers::BaseRouter, runner: Runners::BaseRunner ) end + # Returns a hash of tenant identifiers based on the configured setting + # @return [Hash<String, Symbol => Object>] the hash of tenants + def tenants + configuration.tenants.call + end + # Returns a array of tenant identifiers based on the configured setting # @return [Array<String, Symbol>] the list of tenant identifiers def tenant_identifiers - configuration.tenant_identifiers.call + tenants.keys end end end