lib/slavery.rb in slavery-1.3.0 vs lib/slavery.rb in slavery-1.4.0
- old
+ new
@@ -14,13 +14,21 @@
end
end
class Error < StandardError; end
- mattr_accessor :disabled
+ mattr_accessor :disabled, :env, :spec_key
class << self
+ def spec_key
+ case @@spec_key
+ when String then @@spec_key
+ when Proc then @@spec_key = @@spec_key.call
+ when NilClass then @@spec_key = "#{Slavery.env}_slave"
+ end
+ end
+
def on_slave(&block)
run true, &block
end
def on_master(&block)
@@ -34,17 +42,12 @@
ensure
Thread.current[:on_slave] = old_value
end
def env
- self.env = defined?(Rails) ? Rails.env.to_s : 'development' unless @env
- @env
+ @@env ||= defined?(Rails) ? Rails.env.to_s : 'development'
end
-
- def env=(string)
- @env = ActiveSupport::StringInquirer.new(string)
- end
end
module ClassMethods
def on_slave
# Why where(nil)?
@@ -87,12 +90,12 @@
def self.name
"SlaveConnectionHolder"
end
- spec = ["#{Slavery.env}_slave", Slavery.env].find do |spec|
- ActiveRecord::Base.configurations[spec]
- end or raise Error.new("#{Slavery.env}_slave or #{Slavery.env} must exist!")
+ spec = [Slavery.spec_key, Slavery.env].find do |spec_key|
+ ActiveRecord::Base.configurations[spec_key]
+ end or raise Error.new("#{Slavery.spec_key} or #{Slavery.env} must exist!")
establish_connection spec
}
end
end