Sha256: d1b790f4e55d04e6b03b90320c9cc6d95973766f5f3afa8e579f33dd922179bd

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 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)

        Apartment::Tenant.switch! database if database

        @app.call(env)
      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

3 entries across 3 versions & 1 rubygems

Version Path
apartment-1.0.2 lib/apartment/elevators/generic.rb
apartment-1.0.1 lib/apartment/elevators/generic.rb
apartment-1.0.0 lib/apartment/elevators/generic.rb