Class: Datadog::Core::Configuration::OptionDefinition::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/configuration/option_definition.rb,
sig/datadog/core/configuration/option_definition.rbs

Overview

Acts as DSL for building OptionDefinitions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) {|_self| ... } ⇒ Builder

Returns a new instance of Builder.

Parameters:

  • name (String, Symbol)
  • options (attributes) (defaults to: {})

Yields:

  • (_self)

Yield Parameters:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/datadog/core/configuration/option_definition.rb', line 59

def initialize(name, options = {})
  @is_settings = options.fetch(:is_settings, false)

  @env = nil
  @env_parser = nil
  @default = nil
  @default_proc = nil
  @helpers = {}
  @name = name.to_sym
  @after_set = nil
  @skip_telemetry = false
  @resetter = nil
  @setter = OptionDefinition::IDENTITY
  @type = nil
  @type_options = {}
  # If options were supplied, apply them.
  apply_options!(options)

  # Apply block if given.
  yield(self) if block_given?

  validate_options!
end

Instance Attribute Details

#helpersObject (readonly)

Returns the value of attribute helpers.



56
57
58
# File 'lib/datadog/core/configuration/option_definition.rb', line 56

def helpers
  @helpers
end

Instance Method Details

#after_set(&block) ⇒ void

This method returns an undefined value.



105
106
107
# File 'lib/datadog/core/configuration/option_definition.rb', line 105

def after_set(&block)
  @after_set = block
end

#apply_options!(options = {}) ⇒ Object

For applying options for OptionDefinition



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/datadog/core/configuration/option_definition.rb', line 135

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)
  env(options[:env]) if options.key?(:env)
  env_parser(&options[:env_parser]) if options.key?(:env_parser)
  after_set(&options[:after_set]) if options.key?(:after_set)
  # Steep: https://github.com/soutaro/steep/issues/1979
  skip_telemetry(options[:skip_telemetry]) if options.key?(:skip_telemetry) # steep:ignore ArgumentTypeMismatch
  resetter(&options[:resetter]) if options.key?(:resetter)
  # Steep: https://github.com/soutaro/steep/issues/1979
  setter(&options[:setter]) if options.key?(:setter) # steep:ignore BlockTypeMismatch
  type(options[:type], **(options[:type_options] || {})) if options.key?(:type)
end

#attributesattributes

Returns:



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/datadog/core/configuration/option_definition.rb', line 155

def attributes
  {
    is_settings: @is_settings,

    default: @default,
    default_proc: @default_proc,
    env: @env,
    env_parser: @env_parser,
    after_set: @after_set,
    skip_telemetry: @skip_telemetry,
    resetter: @resetter,
    setter: @setter,
    type: @type,
    type_options: @type_options
  }
end

#default(value = nil, &block) ⇒ Object

can also be a class that includes Configuration::Base for new settings



100
101
102
# File 'sig/datadog/core/configuration/option_definition.rbs', line 100

def default(value = nil, &block)
  @default = block || value
end

#default_proc(&block) ⇒ void

This method returns an undefined value.



97
98
99
# File 'lib/datadog/core/configuration/option_definition.rb', line 97

def default_proc(&block)
  @default_proc = block
end

#env(value) ⇒ Object

standard:disable Style/TrivialAccessors



83
84
85
# File 'lib/datadog/core/configuration/option_definition.rb', line 83

def env(value) # standard:disable Style/TrivialAccessors
  @env = value
end

#env_parser(&block) ⇒ Object

Invoked when the option is first read, and #env is defined. The block provided is only invoked if the environment variable is present (not-nil).



89
90
91
# File 'lib/datadog/core/configuration/option_definition.rb', line 89

def env_parser(&block)
  @env_parser = block
end

#helper(name, *_args) {|arg0| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • name (Symbol)
  • _args (Object)

Yields:

Yield Parameters:

  • arg0

Yield Returns:

  • (void)


101
102
103
# File 'lib/datadog/core/configuration/option_definition.rb', line 101

def helper(name, *_args, &block)
  @helpers[name] = block
end

#resetter(&block) ⇒ void

This method returns an undefined value.



119
120
121
# File 'lib/datadog/core/configuration/option_definition.rb', line 119

def resetter(&block)
  @resetter = block
end

#setter {|new_value, old_value| ... } ⇒ void

This method returns an undefined value.

Yields:

Yield Parameters:

  • new_value (option_type)
  • old_value (option_type)

Yield Returns:

  • (void)


123
124
125
# File 'lib/datadog/core/configuration/option_definition.rb', line 123

def setter(&block)
  @setter = block
end

#skip_telemetry(value) ⇒ Object

This should only be set to true for options that are manually modified before being added to the telemetry payload in app_started.rb. E.g. telemetry is skipped for tracing.writer_options, but in app_started.rb, we send two separate entries for the buffer_size and flush_interval values. Standard: We want to keep this method as skip_telemetry(value), not skip_telemetry = value, so attr_writer is not used.



115
116
117
# File 'lib/datadog/core/configuration/option_definition.rb', line 115

def skip_telemetry(value) # standard:disable Style/TrivialAccessors
  @skip_telemetry = value
end

#to_definitionOptionDefinition

Returns:



151
152
153
# File 'lib/datadog/core/configuration/option_definition.rb', line 151

def to_definition
  OptionDefinition.new(@name, attributes)
end

#type(value, nilable: false) ⇒ Symbol?

Parameters:

  • value (Symbol, nil)
  • nilable: (Boolean) (defaults to: false)

Returns:

  • (Symbol, nil)


127
128
129
130
131
132
# File 'lib/datadog/core/configuration/option_definition.rb', line 127

def type(value, nilable: false)
  @type = value
  @type_options = {nilable: nilable}

  value
end

#validate_options!void

This method returns an undefined value.



174
175
176
177
178
179
180
181
# File 'lib/datadog/core/configuration/option_definition.rb', line 174

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