Sha256: e8b5fc38485199872e8f1b4e88a059809ca546db61327b10142847b0829aeb98

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

module Datadog
  module Core
    module Configuration
      # Represents an instance of an integration configuration option
      # @public_api
      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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ddtrace-1.10.1 lib/datadog/core/configuration/option.rb
ddtrace-1.10.0 lib/datadog/core/configuration/option.rb