require 'switch_point/proxy_repository'

module SwitchPoint
  module Model
    def self.included(model)
      model.singleton_class.class_eval do
        include ClassMethods
        alias_method_chain :connection, :switch_point
      end
    end

    module ClassMethods
      def connection_with_switch_point
        if @switch_point_name
          switch_point_proxy.connection
        else
          connection_without_switch_point
        end
      end

      def with_readonly(&block)
        switch_point_proxy.with_readonly(&block)
      end

      def with_writable(&block)
        switch_point_proxy.with_writable(&block)
      end

      private

      def switch_point(name)
        @switch_point_name = name
      end

      def switch_point_proxy
        @switch_point_proxy ||= ProxyRepository.find(@switch_point_name)
      end
    end
  end
end

require 'switch_point/proxy_repository'

module SwitchPoint
  module Model
    def self.included(model)
      model.singleton_class.class_eval do
        include ClassMethods
        prepend ConnectionHook
      end
    end

    module ConnectionHook
      def connection
        if @switch_point_name
          switch_point_proxy.connection
        else
          super
        end
      end

    end

    module ClassMethods
      def with_readonly(&block)
        switch_point_proxy.with_readonly(&block)
      end

      def with_writable(&block)
        switch_point_proxy.with_writable(&block)
      end

      private

      def switch_point(name)
        @switch_point_name = name
      end

      def switch_point_proxy
        @switch_point_proxy ||= ProxyRepository.find(@switch_point_name)
      end
    end
  end
end

Use prepend rather than alias_method_chain
Use prepend rather than alias_method_chain
