arproxy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ module Arproxy
2
+ class Base
3
+ attr_accessor :proxy_chain, :next_proxy
4
+
5
+ def execute(sql, name=nil)
6
+ next_proxy.execute sql, name
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module Arproxy
2
+ class ChainTail < Base
3
+ def initialize(proxy_chain)
4
+ self.proxy_chain = proxy_chain
5
+ end
6
+
7
+ def execute(sql, name=nil)
8
+ self.proxy_chain.connection.execute_without_arproxy sql, name
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ module Arproxy
2
+ class Config
3
+ attr_accessor :adapter, :logger
4
+ attr_reader :proxies
5
+
6
+ def initialize
7
+ @proxies = []
8
+ end
9
+
10
+ def use(proxy_class, *options)
11
+ ::Arproxy.logger.debug("Arproxy: Mounting #{proxy_class.inspect} (#{options.inspect})")
12
+ @proxies << [proxy_class, options]
13
+ end
14
+
15
+ def adapter_class
16
+ raise Arproxy::Error, "config.adapter must be set" unless @adapter
17
+ camelized_adapter_name = @adapter.split("_").map(&:capitalize).join
18
+ eval "::ActiveRecord::ConnectionAdapters::#{camelized_adapter_name}Adapter"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ module Arproxy
2
+ class Error < Exception
3
+ end
4
+ end
@@ -0,0 +1,26 @@
1
+ module Arproxy
2
+ autoload :ChainTail, "arproxy/chain_tail"
3
+
4
+ class ProxyChain
5
+ attr_reader :head, :tail
6
+ attr_accessor :connection
7
+
8
+ def initialize(config)
9
+ @config = config
10
+ setup_proxy_chain(@config)
11
+ end
12
+
13
+ def setup_proxy_chain(config)
14
+ @tail = ChainTail.new self
15
+ @head = config.proxies.reverse.inject(@tail) do |next_proxy, proxy_config|
16
+ cls, options = proxy_config
17
+ proxy = cls.new *options
18
+ proxy.proxy_chain = self
19
+ proxy.next_proxy = next_proxy
20
+ proxy
21
+ end
22
+ end
23
+ private :setup_proxy_chain
24
+
25
+ end
26
+ end
data/lib/arproxy.rb ADDED
@@ -0,0 +1,51 @@
1
+ require "logger"
2
+
3
+ module Arproxy
4
+ autoload :Config, "arproxy/config"
5
+ autoload :ProxyChain, "arproxy/proxy_chain"
6
+ autoload :Error, "arproxy/error"
7
+ autoload :Base, "arproxy/base"
8
+
9
+ module_function
10
+ def configure
11
+ config = Config.new
12
+ yield config
13
+ @config = config
14
+ end
15
+
16
+ def enable!
17
+ @proxy_chain = ProxyChain.new @config
18
+
19
+ @config.adapter_class.class_eval do
20
+ def execute_with_arproxy(sql, name=nil)
21
+ ::Arproxy.proxy_chain.connection = self
22
+ ::Arproxy.proxy_chain.head.execute sql, name
23
+ end
24
+ alias_method :execute_without_arproxy, :execute
25
+ alias_method :execute, :execute_with_arproxy
26
+ ::Arproxy.logger.debug("Arproxy: Enabled")
27
+ end
28
+ end
29
+
30
+ def disable!
31
+ @config.adapter_class.class_eval do
32
+ alias_method :execute, :execute_without_arproxy
33
+ ::Arproxy.logger.debug("Arproxy: Disabled")
34
+ end
35
+ @proxy_chain = nil
36
+ @config = nil
37
+ end
38
+
39
+ def logger
40
+ @logger ||= begin
41
+ @config && @config.logger ||
42
+ defined?(::Rails) && ::Rails.logger ||
43
+ ::Logger.new(STDOUT)
44
+ end
45
+ end
46
+
47
+ def proxy_chain
48
+ @proxy_chain
49
+ end
50
+ end
51
+
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arproxy
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Issei Naruta
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-04-29 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Arproxy is a proxy between ActiveRecord and database adapter
22
+ email: naruta@cookpad.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/arproxy/base.rb
31
+ - lib/arproxy/chain_tail.rb
32
+ - lib/arproxy/config.rb
33
+ - lib/arproxy/error.rb
34
+ - lib/arproxy/proxy_chain.rb
35
+ - lib/arproxy.rb
36
+ homepage: https://github.com/mirakui/arproxy
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.6
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Proxy between ActiveRecord and DB adapter
69
+ test_files: []
70
+