diff --git a/lib/e7o.rb b/lib/e7o.rb index d7f7a1d..10ea4e4 100644 --- a/lib/e7o.rb +++ b/lib/e7o.rb @@ -1,5 +1,24 @@ -require 'e7o/middleware' -require 'e7o/railtie' if defined?(Rails) +module E7o + class Configuration < OpenStruct + def setup_defaults + self.redis_host = 'redis://localhost:6379' + self.e7o_request_store_key = :e7o_counter + self.enabled = false + end + end + + def self.configure + @config = Configuration.new + @config.setup_defaults + yield(@config) if block_given? + @config + end + + def self.config + @config || configure + end +end + module I18n module E7oKeyRegistry @@ -7,9 +26,9 @@ def lookup(locale, key, scope = [], options = {}) separator = options[:separator] || I18n.default_separator flat_key = I18n.normalize_keys(locale, key, scope, separator).join(separator) - RequestStore.store[:e7o_counter] ||= {} - RequestStore.store[:e7o_counter][flat_key] ||= 0 - RequestStore.store[:e7o_counter][flat_key] += 1 + RequestStore.store[E7o.config.e7o_request_store_key] ||= {} + RequestStore.store[E7o.config.e7o_request_store_key][flat_key] ||= 0 + RequestStore.store[E7o.config.e7o_request_store_key][flat_key] += 1 super end @@ -17,3 +36,6 @@ def lookup(locale, key, scope = [], options = {}) end I18n::Backend::Simple.send :include, I18n::E7oKeyRegistry + +require 'e7o/middleware' +require 'e7o/railtie' if defined?(Rails) \ No newline at end of file diff --git a/lib/e7o/middleware.rb b/lib/e7o/middleware.rb index ae71074..735ac8f 100644 --- a/lib/e7o/middleware.rb +++ b/lib/e7o/middleware.rb @@ -3,7 +3,7 @@ module E7o class Middleware def initialize(app) - @redis_connection ||= Redis.new url: determine_redis_provider + @redis_connection ||= Redis.new url: E7o.config.redis_host @app = app end @@ -27,11 +27,8 @@ def call(env) private def e7o_enabled - ENV['E7O_ENABLED'] == 'true' + E7o.config.enabled end - def determine_redis_provider - ENV['E7O_REDIS_URL'] || ENV[ENV['REDIS_PROVIDER'] || 'REDIS_URL'] - end end end