class Tenantify::Middleware::Strategies

Responsible for finding the tenant for the given env.

It iterates the strategies given to the constructor until it finds one that returns a tenant.

Default strategy

When there is no matching strategy for the current environment a NotMatchingStrategyError is raised. To avoid this behaviour and use a particular tenant by default a Default strategy is provided.

@example Configuring a tenant by default:

Tenantify.configure do |config|
  # your strategies

  config.strategy :default, :tenant => :my_default_tenant
end

Constants

NotMatchingStrategyError

None strategy found a valid tenant for the given environment

Attributes

strategies[R]

Public Class Methods

new(strategies) click to toggle source

Constructor. It receives all strategies in order of precedence.

@param [<#tenant_for>] enumerable of strategies

# File lib/tenantify/middleware/strategies.rb, line 26
def initialize strategies
  @strategies = strategies
end

Public Instance Methods

tenant_for(env) click to toggle source

Find a tenant for the current env.

@param [rack_environment] current env. @return [Symbol] returns the matching tenant.

# File lib/tenantify/middleware/strategies.rb, line 34
def tenant_for env
  find_tenant_for(env) or raise_error(env)
end

Private Instance Methods

find_tenant_for(env) click to toggle source
# File lib/tenantify/middleware/strategies.rb, line 42
def find_tenant_for env
  lazy_strategies.
    map { |strategy| strategy.tenant_for(env) }.
    find { |tenant| tenant }
end
lazy_strategies() click to toggle source
# File lib/tenantify/middleware/strategies.rb, line 48
def lazy_strategies
  @lazy_strategies ||= strategies.lazy
end
raise_error(env) click to toggle source
# File lib/tenantify/middleware/strategies.rb, line 52
def raise_error env
  raise NotMatchingStrategyError.new(env.inspect)
end