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

VERSION =
"0.7.0"

Class Method Summary collapse

Class Method Details

.configurationPenthouse::Configuration

Returns the current configuration of Penthouse



93
94
95
96
97
98
# File 'lib/penthouse.rb', line 93

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



84
85
86
87
88
89
# File 'lib/penthouse.rb', line 84

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: configuration.runner, **options) ⇒ void

This method returns an undefined value.

Loads the tenant and creates their data store

Parameters:

  • tenant_identifier (String, Symbol)

    the identifier for the tenant

See Also:



65
66
67
68
69
# File 'lib/penthouse.rb', line 65

def create(tenant_identifier, runner: configuration.runner, **options)
  switch(tenant_identifier, runner: runner) do |tenant|
    tenant.create(**options)
  end
end

.delete(tenant_identifier, runner: configuration.runner, **options) ⇒ void

This method returns an undefined value.

Loads the tenant and deletes their data store

Parameters:

  • tenant_identifier (String, Symbol)

    the identifier for the tenant

See Also:



75
76
77
78
79
# File 'lib/penthouse.rb', line 75

def delete(tenant_identifier, runner: configuration.runner, **options)
  switch(tenant_identifier, runner: runner) do |tenant|
    tenant.delete(**options)
  end
end

.each_tenant(tenant_identifiers: nil, default_tenant: tenant, runner: 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

Parameters:

  • tenant_identifiers (Array<String, Symbol>, nil) (defaults to: nil)

    the array of tenants to loop through

  • default_tenant (String, Symbol) (defaults to: tenant)

    the identifier for the tenant to return to

  • block (Block)

    the code to execute

Yields:

  • (String, Symbol)

    the identifier for the tenant



45
46
47
48
49
# File 'lib/penthouse.rb', line 45

def each_tenant(tenant_identifiers: nil, default_tenant: tenant, runner: configuration.runner, &block)
  (tenant_identifiers || self.tenant_identifiers).each do |tenant_identifier|
    switch(tenant_identifier, runner: runner, &block)
  end
end

.switch(tenant_identifier, runner: configuration.runner, &block) {|Penthouse::Tenants::BaseTenant| ... } ⇒ void

This method returns an undefined value.

Executes the given block of code within a given tenant

Parameters:

  • tenant_identifier (String, Symbol)

    the identifier for the tenant

  • runner (Penthouse::Runners::BaseRunner) (defaults to: configuration.runner)

    an optional runner to use, defaults to the one configured

  • block (Block)

    the code to execute

Yields:



57
58
59
# File 'lib/penthouse.rb', line 57

def switch(tenant_identifier, runner: configuration.runner, &block)
  runner.call(tenant_identifier, &block)
end

.tenantString, Symbol

Retrieves the currently active tenant identifier

Returns:

  • (String, Symbol)

    the current tenant name



13
14
15
# File 'lib/penthouse.rb', line 13

def tenant
  Thread.current[:tenant]
end

.tenant=(tenant_identifier) ⇒ void

This method returns an undefined value.

Sets the currently active tenant identifier

Parameters:

  • tenant_identifier (String, Symbol)

    the identifier for the tenant



20
21
22
# File 'lib/penthouse.rb', line 20

def tenant=(tenant_identifier)
  Thread.current[:tenant] = tenant_identifier
end

.tenant_identifiersArray<String, Symbol>

Returns a array of tenant identifiers based on the configured setting

Returns:

  • (Array<String, Symbol>)

    the list of tenant identifiers



102
103
104
# File 'lib/penthouse.rb', line 102

def tenant_identifiers
  configuration.tenant_identifiers.call
end

.with_tenant(tenant_identifier, default_tenant: 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

Parameters:

  • tenant_identifier (String, Symbol)

    the identifier for the tenant

  • default_tenant (String, Symbol) (defaults to: tenant)

    the identifier for the tenant to return to

  • block (Block)

    the code to execute

Yields:

  • (String, Symbol)

    the identifier for the tenant



31
32
33
34
35
36
# File 'lib/penthouse.rb', line 31

def with_tenant(tenant_identifier, default_tenant: tenant, &block)
  self.tenant = tenant_identifier
  block.yield(tenant_identifier)
ensure
  self.tenant = default_tenant
end