From f54d36fe86e01b1aa86e20fe00dcb16c53c5869d Mon Sep 17 00:00:00 2001 From: Gustavo Caso Date: Tue, 27 Jun 2023 09:56:27 +0100 Subject: [PATCH] add default_proc option to store block value as default value --- lib/datadog/core/configuration/option.rb | 2 +- .../core/configuration/option_definition.rb | 24 +++++++++++++++ lib/datadog/core/configuration/settings.rb | 5 ++-- .../active_job/configuration/settings.rb | 2 +- .../delayed_job/configuration/settings.rb | 2 +- .../contrib/faraday/configuration/settings.rb | 2 +- .../contrib/grpc/configuration/settings.rb | 2 +- .../contrib/que/configuration/settings.rb | 2 +- .../contrib/resque/configuration/settings.rb | 2 +- .../shoryuken/configuration/settings.rb | 2 +- .../contrib/sidekiq/configuration/settings.rb | 2 +- .../sneakers/configuration/settings.rb | 2 +- .../configuration/option_definition_spec.rb | 29 +++++++++++++++++++ .../datadog/core/configuration/option_spec.rb | 8 +++++ 14 files changed, 74 insertions(+), 12 deletions(-) diff --git a/lib/datadog/core/configuration/option.rb b/lib/datadog/core/configuration/option.rb index 1b27c06fd93..8c186aede9f 100644 --- a/lib/datadog/core/configuration/option.rb +++ b/lib/datadog/core/configuration/option.rb @@ -50,7 +50,7 @@ def default_value if definition.default.instance_of?(Proc) context_eval(&definition.default) else - definition.default + definition.default_proc || definition.default end end diff --git a/lib/datadog/core/configuration/option_definition.rb b/lib/datadog/core/configuration/option_definition.rb index a9c82ed86b5..ca24d725111 100644 --- a/lib/datadog/core/configuration/option_definition.rb +++ b/lib/datadog/core/configuration/option_definition.rb @@ -11,6 +11,7 @@ class OptionDefinition attr_reader \ :default, + :default_proc, :delegate_to, :depends_on, :name, @@ -21,6 +22,7 @@ class OptionDefinition def initialize(name, meta = {}, &block) @default = meta[:default] + @default_proc = meta[:default_proc] @delegate_to = meta[:delegate_to] @depends_on = meta[:depends_on] || [] @name = name.to_sym @@ -38,11 +40,14 @@ def build(context) # Acts as DSL for building OptionDefinitions # @public_api class Builder + class InvalidOptionError < StandardError; end + attr_reader \ :helpers def initialize(name, options = {}) @default = nil + @default_proc = nil @delegate_to = nil @depends_on = [] @helpers = {} @@ -57,6 +62,8 @@ def initialize(name, options = {}) # Apply block if given. yield(self) if block_given? + + validate_options! end def depends_on(*values) @@ -67,6 +74,10 @@ def default(value = nil, &block) @default = block || value end + def default_proc(&block) + @default_proc = block + end + def delegate_to(&block) @delegate_to = block end @@ -96,6 +107,7 @@ def apply_options!(options = {}) return if options.nil? || options.empty? default(options[:default]) if options.key?(:default) + default_proc(&options[:default_proc]) if options.key?(:default_proc) delegate_to(&options[:delegate_to]) if options.key?(:delegate_to) depends_on(*options[:depends_on]) if options.key?(:depends_on) on_set(&options[:on_set]) if options.key?(:on_set) @@ -111,6 +123,7 @@ def to_definition def meta { default: @default, + default_proc: @default_proc, delegate_to: @delegate_to, depends_on: @depends_on, on_set: @on_set, @@ -119,6 +132,17 @@ def meta type: @type } end + + private + + def validate_options! + if !@default.nil? && @default_proc + raise InvalidOptionError, + 'Using `default` and `default_proc` is not allowed. Please use one or the other.' \ + 'If you want to store a block as the default value use `default_proc`'\ + ' otherwise use `default`' + end + end end end end diff --git a/lib/datadog/core/configuration/settings.rb b/lib/datadog/core/configuration/settings.rb index 2c05310ab41..276ac5f5fd9 100644 --- a/lib/datadog/core/configuration/settings.rb +++ b/lib/datadog/core/configuration/settings.rb @@ -472,8 +472,9 @@ def initialize(*_) # @default `->{ Time.now }` # @return [Proc