From 3775ed5ab75f47fa8e74525638e3130e7c1f22d0 Mon Sep 17 00:00:00 2001 From: Adam Rice Date: Sun, 9 Oct 2022 15:28:06 -0400 Subject: [PATCH] Always cache config values --- lib/dry/view.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/dry/view.rb b/lib/dry/view.rb index b21791f..53bc9e9 100644 --- a/lib/dry/view.rb +++ b/lib/dry/view.rb @@ -34,6 +34,27 @@ module Dry # # @api public class View + class Config < Dry::Configurable::Config + # When the superclass returns a value, it only caches it to _values if it + # is considered "cloneable". The high frequency in which Dry::View calls + # this method, however, has considerable performance implications and, as + # such, as values are cached + # + # @see Dry::Configurable::Config#[] + # + # @param [String,Symbol] name + # + # @return Config value + def [](name) + name = name.to_sym + return _values[name] if _values.key?(name) + + super + _values[name] = _settings[name].to_value unless _values.key?(name) + _values[name] + end + end + # @api private DEFAULT_RENDERER_OPTIONS = {default_encoding: "utf-8"}.freeze @@ -41,7 +62,7 @@ class View extend Dry::Core::Cache - extend Dry::Configurable + extend Dry::Configurable(config_class: Dry::View::Config) # @!group Configuration