Class: Apartment::Elevators::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/apartment/elevators/generic.rb

Overview

Provides a rack based tenant switching solution based on request

Direct Known Subclasses

Domain, HostHash, Subdomain

Instance Method Summary collapse

Constructor Details

#initialize(app, processor = nil) ⇒ Generic


11
12
13
14
# File 'lib/apartment/elevators/generic.rb', line 11

def initialize(app, processor = nil)
  @app = app
  @processor = processor || parse_method
end

Instance Method Details

#call(env) ⇒ Object


16
17
18
19
20
21
22
23
24
# File 'lib/apartment/elevators/generic.rb', line 16

def call(env)
  request = Rack::Request.new(env)

  database = @processor.call(request)

  Apartment::Tenant.switch! database if database

  @app.call(env)
end

#deprecation_warningObject


44
45
46
# File 'lib/apartment/elevators/generic.rb', line 44

def deprecation_warning
  Apartment::Deprecation.warn "[DEPRECATED::Apartment] Use #parse_tenant_name instead of #parse_database_name -> #{self.class.name}"
end

#parse_database_name(request) ⇒ Object


26
27
28
29
# File 'lib/apartment/elevators/generic.rb', line 26

def parse_database_name(request)
  deprecation_warning
  parse_tenant_name(request)
end

#parse_methodObject


35
36
37
38
39
40
41
42
# File 'lib/apartment/elevators/generic.rb', line 35

def parse_method
  if self.class.instance_methods(false).include? :parse_database_name
    deprecation_warning
    method(:parse_database_name)
  else
    method(:parse_tenant_name)
  end
end

#parse_tenant_name(request) ⇒ Object


31
32
33
# File 'lib/apartment/elevators/generic.rb', line 31

def parse_tenant_name(request)
  raise "Override"
end