Class: SwitchPoint::Proxy
- Inherits:
-
Object
- Object
- SwitchPoint::Proxy
- Defined in:
- lib/switch_point/proxy.rb
Instance Attribute Summary collapse
-
#initial_name ⇒ Object
readonly
Returns the value of attribute initial_name.
Instance Method Summary collapse
- #connection ⇒ Object
- #define_model(model_name) ⇒ Object
-
#initialize(name) ⇒ Proxy
constructor
A new instance of Proxy.
- #memorize_switch_point(name, mode, connection) ⇒ Object
- #readonly! ⇒ Object
- #readonly? ⇒ Boolean
- #reset_name! ⇒ Object
- #switch_name(new_name, &block) ⇒ Object
- #with_connection(mode, &block) ⇒ Object
- #with_readonly(&block) ⇒ Object
- #with_writable(&block) ⇒ Object
- #writable! ⇒ Object
- #writable? ⇒ Boolean
Constructor Details
#initialize(name) ⇒ Proxy
Returns a new instance of Proxy.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/switch_point/proxy.rb', line 5 def initialize(name) @initial_name = name @current_name = name [:readonly, :writable].each do |mode| model = define_model(SwitchPoint.config.model_name(name, mode)) model.establish_connection(SwitchPoint.config.database_name(name, mode)) memorize_switch_point(name, mode, model.connection) end @mode = :readonly end |
Instance Attribute Details
#initial_name ⇒ Object (readonly)
Returns the value of attribute initial_name.
3 4 5 |
# File 'lib/switch_point/proxy.rb', line 3 def initial_name @initial_name end |
Instance Method Details
#connection ⇒ Object
77 78 79 80 |
# File 'lib/switch_point/proxy.rb', line 77 def connection ProxyRepository.checkout(@current_name) # Ensure the target proxy is created Proxy.const_get(SwitchPoint.config.model_name(@current_name, @mode)).connection end |
#define_model(model_name) ⇒ Object
16 17 18 19 20 |
# File 'lib/switch_point/proxy.rb', line 16 def define_model(model_name) model = Class.new(ActiveRecord::Base) Proxy.const_set(model_name, model) model end |
#memorize_switch_point(name, mode, connection) ⇒ Object
22 23 24 25 |
# File 'lib/switch_point/proxy.rb', line 22 def memorize_switch_point(name, mode, connection) switch_point = { name: name, mode: mode } connection.pool.instance_variable_set(:@switch_point, switch_point) end |
#readonly! ⇒ Object
27 28 29 |
# File 'lib/switch_point/proxy.rb', line 27 def readonly! @mode = :readonly end |
#readonly? ⇒ Boolean
31 32 33 |
# File 'lib/switch_point/proxy.rb', line 31 def readonly? @mode == :readonly end |
#reset_name! ⇒ Object
73 74 75 |
# File 'lib/switch_point/proxy.rb', line 73 def reset_name! @current_name = @initial_name end |
#switch_name(new_name, &block) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/switch_point/proxy.rb', line 59 def switch_name(new_name, &block) if block begin old_name = @current_name @current_name = new_name block.call ensure @current_name = old_name end else @current_name = new_name end end |
#with_connection(mode, &block) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/switch_point/proxy.rb', line 51 def with_connection(mode, &block) saved_mode = @mode @mode = mode block.call ensure @mode = saved_mode end |
#with_readonly(&block) ⇒ Object
43 44 45 |
# File 'lib/switch_point/proxy.rb', line 43 def with_readonly(&block) with_connection(:readonly, &block) end |
#with_writable(&block) ⇒ Object
47 48 49 |
# File 'lib/switch_point/proxy.rb', line 47 def with_writable(&block) with_connection(:writable, &block) end |
#writable! ⇒ Object
35 36 37 |
# File 'lib/switch_point/proxy.rb', line 35 def writable! @mode = :writable end |
#writable? ⇒ Boolean
39 40 41 |
# File 'lib/switch_point/proxy.rb', line 39 def writable? @mode == :writable end |