Permalink
Browse files

Support reconfiguring and reenabling

  • Loading branch information...
1 parent 407396a commit 7181227c16990bd52a0f5ea5548856f43a715071 @mirakui mirakui committed Aug 21, 2012
Showing with 82 additions and 31 deletions.
  1. +1 −0 Gemfile
  2. +15 −0 Gemfile.lock
  3. +33 −25 lib/arproxy.rb
  4. +28 −4 lib/arproxy/proxy_chain.rb
  5. +5 −2 spec/arproxy_spec.rb
View
@@ -2,5 +2,6 @@ source :rubygems
group :test do
gem "rspec"
+ gem "activerecord", ">= 3.0.0", :require => "active_record"
end
View
@@ -1,7 +1,20 @@
GEM
remote: http://rubygems.org/
specs:
+ activemodel (3.0.10)
+ activesupport (= 3.0.10)
+ builder (~> 2.1.2)
+ i18n (~> 0.5.0)
+ activerecord (3.0.10)
+ activemodel (= 3.0.10)
+ activesupport (= 3.0.10)
+ arel (~> 2.0.10)
+ tzinfo (~> 0.3.23)
+ activesupport (3.0.10)
+ arel (2.0.10)
+ builder (2.1.2)
diff-lcs (1.1.3)
+ i18n (0.5.0)
rspec (2.9.0)
rspec-core (~> 2.9.0)
rspec-expectations (~> 2.9.0)
@@ -10,9 +23,11 @@ GEM
rspec-expectations (2.9.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.9.0)
+ tzinfo (0.3.33)
PLATFORMS
ruby
DEPENDENCIES
+ activerecord (>= 3.0.0)
rspec
View
@@ -1,21 +1,24 @@
require "logger"
require "active_record"
+require "arproxy/base"
+require "arproxy/config"
+require "arproxy/proxy_chain"
+require "arproxy/error"
module Arproxy
- autoload :Config, "arproxy/config"
- autoload :ProxyChain, "arproxy/proxy_chain"
- autoload :Error, "arproxy/error"
- autoload :Base, "arproxy/base"
module_function
+ def clear_configuration
+ @config = Config.new
+ end
+
def configure
- config = Config.new
- yield config
- @config = config
+ clear_configuration unless @config
+ yield @config
end
def enable!
- if @enabled
+ if enable?
Arproxy.logger.warn "Arproxy has been already enabled"
return
end
@@ -25,33 +28,39 @@ def enable!
end
# for lazy loading
- ActiveRecord::Base
+ ::ActiveRecord::Base
@proxy_chain = ProxyChain.new @config
+ @proxy_chain.enable!
- @config.adapter_class.class_eval do
- def execute_with_arproxy(sql, name=nil)
- ::Arproxy.proxy_chain.connection = self
- ::Arproxy.proxy_chain.head.execute sql, name
- end
- alias_method :execute_without_arproxy, :execute
- alias_method :execute, :execute_with_arproxy
- ::Arproxy.logger.debug("Arproxy: Enabled")
- end
@enabled = true
end
def disable!
- if @config
- @config.adapter_class.class_eval do
- alias_method :execute, :execute_without_arproxy
- ::Arproxy.logger.debug("Arproxy: Disabled")
- end
+ unless enable?
+ Arproxy.logger.warn "Arproxy is not enabled yet"
+ return
+ end
+
+ if @proxy_chain
+ @proxy_chain.disable!
+ @proxy_chain = nil
end
- @proxy_chain = nil
@enabled = false
end
+ def enable?
+ !!@enabled
+ end
+
+ def reenable!
+ if enable?
+ @proxy_chain.reenable!
+ else
+ enable!
+ end
+ end
+
def logger
@logger ||= begin
@config && @config.logger ||
@@ -64,4 +73,3 @@ def proxy_chain
@proxy_chain
end
end
-
View
@@ -7,20 +7,44 @@ class ProxyChain
def initialize(config)
@config = config
- setup_proxy_chain(@config)
+ setup
end
- def setup_proxy_chain(config)
+ def setup
@tail = ChainTail.new self
- @head = config.proxies.reverse.inject(@tail) do |next_proxy, proxy_config|
+ @head = @config.proxies.reverse.inject(@tail) do |next_proxy, proxy_config|
cls, options = proxy_config
proxy = cls.new *options
proxy.proxy_chain = self
proxy.next_proxy = next_proxy
proxy
end
end
- private :setup_proxy_chain
+ private :setup
+ def reenable!
+ disable!
+ setup
+ enable!
+ end
+
+ def enable!
+ @config.adapter_class.class_eval do
+ def execute_with_arproxy(sql, name=nil)
+ ::Arproxy.proxy_chain.connection = self
+ ::Arproxy.proxy_chain.head.execute sql, name
+ end
+ alias_method :execute_without_arproxy, :execute
+ alias_method :execute, :execute_with_arproxy
+ ::Arproxy.logger.debug("Arproxy: Enabled")
+ end
+ end
+
+ def disable!
+ @config.adapter_class.class_eval do
+ alias_method :execute, :execute_without_arproxy
+ ::Arproxy.logger.debug("Arproxy: Disabled")
+ end
+ end
end
end
View
@@ -35,6 +35,7 @@ def execute(sql, name = nil)
context "with a proxy" do
before do
+ Arproxy.clear_configuration
Arproxy.configure do |config|
config.adapter = "dummy"
config.use ProxyA
@@ -47,6 +48,7 @@ def execute(sql, name = nil)
context "with 2 proxies" do
before do
+ Arproxy.clear_configuration
Arproxy.configure do |config|
config.adapter = "dummy"
config.use ProxyA
@@ -60,6 +62,7 @@ def execute(sql, name = nil)
context "with 2 proxies which have an option" do
before do
+ Arproxy.clear_configuration
Arproxy.configure do |config|
config.adapter = "dummy"
config.use ProxyA
@@ -73,6 +76,7 @@ def execute(sql, name = nil)
context do
before do
+ Arproxy.clear_configuration
Arproxy.configure do |config|
config.adapter = "dummy"
config.use ProxyA
@@ -116,12 +120,11 @@ def execute(sql, name = nil)
context "re-configure" do
before do
Arproxy.configure do |config|
- config.adapter = "dummy"
config.use ProxyB
end
Arproxy.enable!
end
- it { should == {:sql => "SQL_B", :name => "NAME_B"} }
+ it { should == {:sql => "SQL_A_B", :name => "NAME_A_B"} }
end
end

0 comments on commit 7181227

Please sign in to comment.