Module: Penthouse
- Defined in:
- lib/penthouse/configuration.rb,
lib/penthouse.rb,
lib/penthouse/app.rb,
lib/penthouse/sidekiq.rb,
lib/penthouse/version.rb,
lib/penthouse/migrator.rb,
lib/penthouse/migrator.rb,
lib/penthouse/tasks/enhancements.rb,
lib/penthouse/tenants/migratable.rb,
lib/penthouse/routers/base_router.rb,
lib/penthouse/runners/base_runner.rb,
lib/penthouse/tenants/base_tenant.rb,
lib/penthouse/runners/schema_runner.rb,
lib/penthouse/tenants/schema_tenant.rb,
lib/penthouse/routers/subdomain_router.rb,
lib/penthouse/tenants/octopus_shard_tenant.rb,
lib/penthouse/tenants/octopus_schema_tenant.rb
Overview
This router will load the tenant name based on a request’s sub-domain
Defined Under Namespace
Modules: Migration, Migrator, Routers, Runners, Sidekiq, Tenants Classes: App, Configuration, RakeTaskEnhancer, TenantNotFound
Constant Summary collapse
- CURRENT_TENANT_KEY =
'penthouse_tenant'.freeze
- VERSION =
"0.8.0"
Class Method Summary collapse
-
.configuration ⇒ Penthouse::Configuration
Returns the current configuration of Penthouse.
-
.configure {|Penthouse::Configuration| ... } ⇒ void
Allows you to configure the router of Penthouse.
-
.create(tenant_identifier, runner: self.configuration.runner, **options) ⇒ void
Loads the tenant and creates their data store.
-
.delete(tenant_identifier, runner: self.configuration.runner, **options) ⇒ void
Loads the tenant and deletes their data store.
-
.each_tenant(tenant_identifiers: self.tenant_identifiers, default_tenant: self.tenant, runner: self.configuration.runner, &block) {|String, Symbol| ... } ⇒ void
Wraps Penthouse.switch and simply executes the block of code for each tenant within Penthouse.tenant_identifiers.
-
.switch(tenant_identifier, runner: self.configuration.runner, &block) {|Penthouse::Tenants::BaseTenant| ... } ⇒ void
Executes the given block of code within a given tenant.
-
.tenant ⇒ String, Symbol
Retrieves the currently active tenant identifier.
-
.tenant=(tenant_identifier) ⇒ void
Sets the currently active tenant identifier.
-
.tenant_identifiers ⇒ Array<String, Symbol>
Returns a array of tenant identifiers based on the configured setting.
-
.tenants ⇒ Hash<String, Symbol => Object>
Returns a hash of tenant identifiers based on the configured setting.
-
.with_tenant(tenant_identifier, default_tenant: self.tenant, &block) {|String, Symbol| ... } ⇒ void
Similar to Penthouse.tenant=, except this will switch back after the given block has finished executing.
Class Method Details
.configuration ⇒ Penthouse::Configuration
Returns the current configuration of Penthouse
94 95 96 97 98 99 |
# File 'lib/penthouse.rb', line 94 def configuration @configuration ||= Configuration.new( router: Routers::BaseRouter, runner: Runners::BaseRunner ) end |
.configure {|Penthouse::Configuration| ... } ⇒ void
This method returns an undefined value.
Allows you to configure the router of Penthouse
85 86 87 88 89 90 |
# File 'lib/penthouse.rb', line 85 def configure(&block) # allow the configuration by the block block.yield(self.configuration) # prevent modification of configuration once set self.configuration.freeze end |
.create(tenant_identifier, runner: self.configuration.runner, **options) ⇒ void
This method returns an undefined value.
Loads the tenant and creates their data store
66 67 68 69 70 |
# File 'lib/penthouse.rb', line 66 def create(tenant_identifier, runner: self.configuration.runner, **) switch(tenant_identifier, runner: runner) do |tenant| tenant.create(**) end end |
.delete(tenant_identifier, runner: self.configuration.runner, **options) ⇒ void
This method returns an undefined value.
Loads the tenant and deletes their data store
76 77 78 79 80 |
# File 'lib/penthouse.rb', line 76 def delete(tenant_identifier, runner: self.configuration.runner, **) switch(tenant_identifier, runner: runner) do |tenant| tenant.delete(**) end end |
.each_tenant(tenant_identifiers: self.tenant_identifiers, default_tenant: self.tenant, runner: self.configuration.runner, &block) {|String, Symbol| ... } ⇒ void
This method returns an undefined value.
Wraps Penthouse.switch and simply executes the block of code for each tenant within Penthouse.tenant_identifiers
46 47 48 49 50 |
# File 'lib/penthouse.rb', line 46 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 |
.switch(tenant_identifier, runner: self.configuration.runner, &block) {|Penthouse::Tenants::BaseTenant| ... } ⇒ void
This method returns an undefined value.
Executes the given block of code within a given tenant
58 59 60 |
# File 'lib/penthouse.rb', line 58 def switch(tenant_identifier, runner: self.configuration.runner, &block) runner.call(tenant_identifier, &block) end |
.tenant ⇒ String, Symbol
Retrieves the currently active tenant identifier
14 15 16 |
# File 'lib/penthouse.rb', line 14 def tenant Thread.current[CURRENT_TENANT_KEY] end |
.tenant=(tenant_identifier) ⇒ void
This method returns an undefined value.
Sets the currently active tenant identifier
21 22 23 |
# File 'lib/penthouse.rb', line 21 def tenant=(tenant_identifier) Thread.current[CURRENT_TENANT_KEY] = tenant_identifier end |
.tenant_identifiers ⇒ Array<String, Symbol>
Returns a array of tenant identifiers based on the configured setting
109 110 111 |
# File 'lib/penthouse.rb', line 109 def tenant_identifiers tenants.keys end |
.tenants ⇒ Hash<String, Symbol => Object>
Returns a hash of tenant identifiers based on the configured setting
103 104 105 |
# File 'lib/penthouse.rb', line 103 def tenants configuration.tenants.call end |
.with_tenant(tenant_identifier, default_tenant: self.tenant, &block) {|String, Symbol| ... } ⇒ void
This method returns an undefined value.
Similar to Penthouse.tenant=, except this will switch back after the given block has finished executing
32 33 34 35 36 37 |
# File 'lib/penthouse.rb', line 32 def with_tenant(tenant_identifier, default_tenant: self.tenant, &block) self.tenant = tenant_identifier block.yield(tenant_identifier) ensure self.tenant = default_tenant end |