Sha256: 9d2b1ddd660c04cc343c23e4f884b0c932ebb1f8afe1a3bbbb877c0678858ba1

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'rack/request'
require 'apartment/tenant'
require 'apartment/deprecation'

module Apartment
  module Elevators
    #   Provides a rack based tenant switching solution based on request
    #
    class Generic

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

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

        database = @processor.call(request)

        if database
          Apartment::Tenant.switch(database) { @app.call(env) }
        else
          @app.call(env)
        end
      end

      def parse_database_name(request)
        deprecation_warning
        parse_tenant_name(request)
      end

      def parse_tenant_name(request)
        raise "Override"
      end

      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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
apartment-1.2.0 lib/apartment/elevators/generic.rb
apartment-1.1.0 lib/apartment/elevators/generic.rb