Class: Magick::Config
- Inherits:
-
Object
- Object
- Magick::Config
- Defined in:
- lib/magick/config.rb
Instance Attribute Summary collapse
-
#active_record_model_class ⇒ Object
Returns the value of attribute active_record_model_class.
-
#adapter_registry ⇒ Object
Returns the value of attribute adapter_registry.
-
#async_updates(enabled: true) ⇒ Object
Returns the value of attribute async_updates.
-
#audit_log(enabled: true, adapter: nil) ⇒ Object
Returns the value of attribute audit_log.
-
#circuit_breaker_threshold ⇒ Object
Returns the value of attribute circuit_breaker_threshold.
-
#circuit_breaker_timeout ⇒ Object
Returns the value of attribute circuit_breaker_timeout.
-
#environment(name) ⇒ Object
Returns the value of attribute environment.
-
#memory_ttl(seconds) ⇒ Object
Returns the value of attribute memory_ttl.
-
#performance_metrics(enabled: true, redis_tracking: nil, batch_size: 100, flush_interval: 60, **_options) ⇒ Object
Returns the value of attribute performance_metrics.
-
#redis_db ⇒ Object
Returns the value of attribute redis_db.
-
#redis_namespace ⇒ Object
Returns the value of attribute redis_namespace.
-
#redis_url ⇒ Object
Returns the value of attribute redis_url.
-
#versioning(enabled: true) ⇒ Object
Returns the value of attribute versioning.
-
#warn_on_deprecated(enabled: true) ⇒ Object
Returns the value of attribute warn_on_deprecated.
Instance Method Summary collapse
- #active_record(model_class: nil, primary: false, **options) ⇒ Object
-
#adapter(type, **options, &block) ⇒ Object
DSL methods for configuration.
- #apply! ⇒ Object
- #circuit_breaker(threshold: nil, timeout: nil) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #memory(**options) ⇒ Object
- #redis(url: nil, namespace: nil, db: nil, **options) ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/magick/config.rb', line 8 def initialize @warn_on_deprecated = false @async_updates = false @memory_ttl = 3600 # 1 hour @circuit_breaker_threshold = 5 @circuit_breaker_timeout = 60 @redis_namespace = 'magick:features' @redis_db = nil # Use default database (0) unless specified @environment = defined?(Rails) ? Rails.env.to_s : 'development' end |
Instance Attribute Details
#active_record_model_class ⇒ Object
Returns the value of attribute active_record_model_class.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def active_record_model_class @active_record_model_class end |
#adapter_registry ⇒ Object
Returns the value of attribute adapter_registry.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def adapter_registry @adapter_registry end |
#async_updates(enabled: true) ⇒ Object
Returns the value of attribute async_updates.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def async_updates @async_updates end |
#audit_log(enabled: true, adapter: nil) ⇒ Object
Returns the value of attribute audit_log.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def audit_log @audit_log end |
#circuit_breaker_threshold ⇒ Object
Returns the value of attribute circuit_breaker_threshold.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def circuit_breaker_threshold @circuit_breaker_threshold end |
#circuit_breaker_timeout ⇒ Object
Returns the value of attribute circuit_breaker_timeout.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def circuit_breaker_timeout @circuit_breaker_timeout end |
#environment(name) ⇒ Object
Returns the value of attribute environment.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def environment @environment end |
#memory_ttl(seconds) ⇒ Object
Returns the value of attribute memory_ttl.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def memory_ttl @memory_ttl end |
#performance_metrics(enabled: true, redis_tracking: nil, batch_size: 100, flush_interval: 60, **_options) ⇒ Object
Returns the value of attribute performance_metrics.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def performance_metrics @performance_metrics end |
#redis_db ⇒ Object
Returns the value of attribute redis_db.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def redis_db @redis_db end |
#redis_namespace ⇒ Object
Returns the value of attribute redis_namespace.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def redis_namespace @redis_namespace end |
#redis_url ⇒ Object
Returns the value of attribute redis_url.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def redis_url @redis_url end |
#versioning(enabled: true) ⇒ Object
Returns the value of attribute versioning.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def versioning @versioning end |
#warn_on_deprecated(enabled: true) ⇒ Object
Returns the value of attribute warn_on_deprecated.
5 6 7 |
# File 'lib/magick/config.rb', line 5 def warn_on_deprecated @warn_on_deprecated end |
Instance Method Details
#active_record(model_class: nil, primary: false, **options) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/magick/config.rb', line 80 def active_record(model_class: nil, primary: false, **) @active_record_model_class = model_class if model_class @active_record_primary = primary active_record_adapter = configure_active_record_adapter(model_class: model_class, **) # Automatically create Registry adapter if it doesn't exist if @adapter_registry # If registry already exists, update it with the new Active Record adapter if active_record_adapter && @adapter_registry.is_a?(Adapters::Registry) @adapter_registry.instance_variable_set(:@active_record_adapter, active_record_adapter) # Update primary if specified @adapter_registry.instance_variable_set(:@primary, :active_record) if primary end else memory_adapter = configure_memory_adapter redis_adapter = configure_redis_adapter cb = Magick::CircuitBreaker.new( failure_threshold: @circuit_breaker_threshold, timeout: @circuit_breaker_timeout ) primary_adapter = primary ? :active_record : :memory @adapter_registry = Adapters::Registry.new( memory_adapter, redis_adapter, active_record_adapter: active_record_adapter, circuit_breaker: cb, async: @async_updates, primary: primary_adapter ) end active_record_adapter end |
#adapter(type, **options, &block) ⇒ Object
DSL methods for configuration
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/magick/config.rb', line 20 def adapter(type, **, &block) case type.to_sym when :memory configure_memory_adapter(**) when :redis configure_redis_adapter(**) when :active_record configure_active_record_adapter(**) when :registry if block_given? instance_eval(&block) configure_registry_adapter else configure_registry_adapter(**) end else raise ArgumentError, "Unknown adapter type: #{type}" end end |
#apply! ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/magick/config.rb', line 163 def apply! # Apply configuration to Magick module Magick.adapter_registry = adapter_registry if adapter_registry # Apply performance metrics (preserve redis_tracking setting) if performance_metrics Magick.performance_metrics = performance_metrics # Re-apply redis_tracking setting after assignment (in case object was replaced) if defined?(@performance_metrics_redis_tracking) && !@performance_metrics_redis_tracking.nil? # Explicitly set value takes precedence Magick.performance_metrics.enable_redis_tracking(enable: @performance_metrics_redis_tracking) # Otherwise, auto-enable if Redis adapter is configured # Check Magick.adapter_registry (after it's been set) instead of local instance variable elsif Magick.adapter_registry.is_a?(Adapters::Registry) && Magick.adapter_registry.redis_available? # Always enable if Redis adapter is available (unless explicitly disabled above) Magick.performance_metrics.enable_redis_tracking(enable: true) end elsif Magick.performance_metrics # If no new performance_metrics was configured, but one exists, still try to enable Redis tracking # if Redis adapter is available and redis_tracking wasn't explicitly disabled # Only auto-enable if not explicitly disabled if Magick.adapter_registry.is_a?(Adapters::Registry) && Magick.adapter_registry.redis_available? && !(defined?(@performance_metrics_redis_tracking) && @performance_metrics_redis_tracking == false) Magick.performance_metrics.enable_redis_tracking(enable: true) end end Magick.audit_log = audit_log if audit_log Magick.versioning = versioning if versioning Magick.warn_on_deprecated = warn_on_deprecated end |
#circuit_breaker(threshold: nil, timeout: nil) ⇒ Object
142 143 144 145 |
# File 'lib/magick/config.rb', line 142 def circuit_breaker(threshold: nil, timeout: nil) @circuit_breaker_threshold = threshold if threshold @circuit_breaker_timeout = timeout if timeout end |
#memory(**options) ⇒ Object
40 41 42 |
# File 'lib/magick/config.rb', line 40 def memory(**) configure_memory_adapter(**) end |
#redis(url: nil, namespace: nil, db: nil, **options) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/magick/config.rb', line 44 def redis(url: nil, namespace: nil, db: nil, **) @redis_url = url if url @redis_namespace = namespace if namespace @redis_db = db if db redis_adapter = configure_redis_adapter(url: url, namespace: namespace, db: db, **) # Automatically create Registry adapter if it doesn't exist # This allows users to just call `redis url: ...` without needing to call `adapter :registry` if @adapter_registry # If registry already exists, update it with the new Redis adapter # This allows reconfiguring Redis without recreating the registry if redis_adapter && @adapter_registry.is_a?(Adapters::Registry) # Update the Redis adapter in the existing registry @adapter_registry.instance_variable_set(:@redis_adapter, redis_adapter) # Restart cache invalidation subscriber with new Redis adapter @adapter_registry.send(:start_cache_invalidation_subscriber) if redis_adapter end else memory_adapter = configure_memory_adapter active_record_adapter = configure_active_record_adapter if defined?(::ActiveRecord::Base) cb = Magick::CircuitBreaker.new( failure_threshold: @circuit_breaker_threshold, timeout: @circuit_breaker_timeout ) @adapter_registry = Adapters::Registry.new( memory_adapter, redis_adapter, active_record_adapter: active_record_adapter, circuit_breaker: cb, async: @async_updates ) end redis_adapter end |