Class: SwitchPoint::Proxy
- Inherits:
-
Object
- Object
- SwitchPoint::Proxy
- Defined in:
- lib/switch_point/proxy.rb
Instance Method Summary collapse
- #connection ⇒ Object
- #define_model(model_name) ⇒ Object
-
#initialize(name) ⇒ Proxy
constructor
A new instance of Proxy.
- #memorize_switch_point_name(name, connection) ⇒ Object
- #readonly! ⇒ Object
- #with_connection(mode, &block) ⇒ Object
- #with_readonly(&block) ⇒ Object
- #with_writable(&block) ⇒ Object
- #writable! ⇒ Object
Constructor Details
#initialize(name) ⇒ Proxy
Returns a new instance of Proxy.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/switch_point/proxy.rb', line 5 def initialize(name) @models = {} [:readonly, :writable].each do |mode| model = define_model(SwitchPoint.config.model_name(name, mode)) @models[mode] = model model.establish_connection(SwitchPoint.config.database_name(name, mode)) memorize_switch_point_name(name, model.connection) end @models[:writable].connection.extend(WritableConnectionHook) @mode = :readonly end |
Instance Method Details
#connection ⇒ Object
51 52 53 |
# File 'lib/switch_point/proxy.rb', line 51 def connection @models[@mode].connection end |
#define_model(model_name) ⇒ Object
17 18 19 20 21 |
# File 'lib/switch_point/proxy.rb', line 17 def define_model(model_name) model = Class.new(ActiveRecord::Base) Proxy.const_set(model_name, model) model end |
#memorize_switch_point_name(name, connection) ⇒ Object
23 24 25 |
# File 'lib/switch_point/proxy.rb', line 23 def memorize_switch_point_name(name, connection) connection.instance_variable_set(:@switch_point_name, name) end |
#readonly! ⇒ Object
27 28 29 |
# File 'lib/switch_point/proxy.rb', line 27 def readonly! @mode = :readonly end |
#with_connection(mode, &block) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/switch_point/proxy.rb', line 43 def with_connection(mode, &block) saved_mode = @mode @mode = mode block.call ensure @mode = saved_mode end |
#with_readonly(&block) ⇒ Object
35 36 37 |
# File 'lib/switch_point/proxy.rb', line 35 def with_readonly(&block) with_connection(:readonly, &block) end |
#with_writable(&block) ⇒ Object
39 40 41 |
# File 'lib/switch_point/proxy.rb', line 39 def with_writable(&block) with_connection(:writable, &block) end |
#writable! ⇒ Object
31 32 33 |
# File 'lib/switch_point/proxy.rb', line 31 def writable! @mode = :writable end |