-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,102 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,57 @@ | ||
require "tenantify/version" | ||
|
||
require "tenantify/configuration" | ||
require "tenantify/tenant" | ||
require "tenantify/resource" | ||
require "tenantify/middleware" | ||
|
||
module Tenantify | ||
# Tenantify configuration | ||
# | ||
# @return [Configuration] the current configuration | ||
def self.configuration | ||
@configuration ||= Configuration.new | ||
end | ||
|
||
# A helper to configure Tenantify | ||
# | ||
# @yield [configuration] Configures tenantify | ||
def self.configure | ||
yield configuration | ||
end | ||
|
||
# An alias to {Tenant::using} | ||
# | ||
# @example Run some code on a particular tenant | ||
# Tenantify.using :a_tenant do | ||
# # some code... | ||
# end | ||
# @see Tenant.using | ||
def self.using tenant, &block | ||
Tenant.using(tenant, &block) | ||
end | ||
|
||
# An alias to {Tenant::use!} | ||
# | ||
# @example Change the current tenant | ||
# Tenanfify.use! :a_tenant | ||
# # using :a_tenant from now on | ||
# @see Tenant.use! | ||
def self.use! tenant | ||
Tenant.use!(tenant) | ||
end | ||
|
||
# An alias to {Tenant::current} | ||
# | ||
# @see Tenant.current | ||
def self.current | ||
Tenant.current | ||
end | ||
|
||
# An alias to {Resource::new} | ||
# | ||
# @see Resource | ||
def self.resource correspondence | ||
Resource.new(correspondence) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Tenantify | ||
# It stores a configuration for {Tenantify::Middleware}. | ||
class Configuration | ||
# All configured strategies in order of priority. | ||
# | ||
# @return [Array<strategy_config>] a collection of strategy configurations. | ||
attr_reader :strategies | ||
|
||
# Constructor. | ||
def initialize | ||
@strategies = [] | ||
end | ||
|
||
# Adds a new strategy for the Tenantify middleware. The order the strategies | ||
# are added is the priority order they have to match the tenant. | ||
# | ||
# @param [Symbol, Class] the name of a known strategy or a custom strategy | ||
# class. | ||
# @param [Hash] strategy configuration. | ||
# @return [Array<strategy_config>] a collection of strategy configurations. | ||
def strategy name_or_class, strategy_config = {} | ||
strategies << [name_or_class, strategy_config] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'tenantify/tenant' | ||
require 'tenantify/middleware/builder' | ||
|
||
module Tenantify | ||
# Rack middleware responsible for setting the tenant during the http request. | ||
# | ||
# This middleware builds a set of strategies from the given configuration, and | ||
# sets the tenant returned from those strategies. | ||
class Middleware | ||
# Constructor. | ||
# | ||
# @param [#call] the Rack application | ||
# @param [Tenantify::Configuration] the Rack application | ||
def initialize app, config = Tenantify.configuration | ||
@app = app | ||
@config = config | ||
end | ||
|
||
# Calls the rack middleware. | ||
# | ||
# @param [rack_environment] the Rack environment | ||
# @return [rack_response] the Rack response | ||
def call env | ||
tenant = strategies.tenant_for(env) | ||
|
||
Tenant.using(tenant) { app.call(env) } | ||
end | ||
|
||
private | ||
|
||
attr_reader :app, :config | ||
|
||
def strategies | ||
@strategies ||= begin | ||
builder = Builder.new(config) | ||
builder.call | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.