Sha256: 2ee8703646512dcb7a987bb057f1a91fcceac807452a25fce316af29e26a81a1

Contents?: true

Size: 833 Bytes

Versions: 1

Compression:

Stored size: 833 Bytes

Contents

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 use_switch_point(name)
        @switch_point_name = name
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
switch_point-0.1.0 lib/switch_point/model.rb