Sha256: ab9093de1c48f60182ae789d789c873a6688b30278e6891e2df822bf3cf1aa71

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

# typed: true
module Datadog
  module Configuration
    # Represents an instance of an integration configuration option
    class Option
      attr_reader \
        :definition

      def initialize(definition, context)
        @definition = definition
        @context = context
        @value = nil
        @is_set = false
      end

      def set(value)
        old_value = @value
        (@value = context_exec(value, old_value, &definition.setter)).tap do |v|
          @is_set = true
          context_exec(v, old_value, &definition.on_set) if definition.on_set
        end
      end

      def get
        if @is_set
          @value
        elsif definition.delegate_to
          context_eval(&definition.delegate_to)
        else
          set(default_value)
        end
      end

      def reset
        @value = if definition.resetter
                   # Don't change @is_set to false; custom resetters are
                   # responsible for changing @value back to a good state.
                   # Setting @is_set = false would cause a default to be applied.
                   context_exec(@value, &definition.resetter)
                 else
                   @is_set = false
                   nil
                 end
      end

      def default_value
        if definition.lazy
          context_eval(&definition.default)
        else
          definition.default
        end
      end

      private

      def context_exec(*args, &block)
        @context.instance_exec(*args, &block)
      end

      def context_eval(&block)
        @context.instance_eval(&block)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ddtrace-0.54.2 lib/ddtrace/configuration/option.rb
ddtrace-0.54.1 lib/ddtrace/configuration/option.rb
ddtrace-0.54.0 lib/ddtrace/configuration/option.rb
ddtrace-0.53.0 lib/ddtrace/configuration/option.rb
ddtrace-0.52.0 lib/ddtrace/configuration/option.rb