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

Inherits:
Object
  • Object
show all
Defined in:
lib/ddtrace/configuration/option_definition.rb

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.

Yields:

  • (_self)

Yield Parameters:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ddtrace/configuration/option_definition.rb', line 44

def initialize(name, options = {})
  @default = nil
  @delegate_to = nil
  @depends_on = []
  @helpers = {}
  @lazy = false
  @name = name.to_sym
  @on_set = nil
  @resetter = nil
  @setter = OptionDefinition::IDENTITY

  # If options were supplied, apply them.
  apply_options!(options)

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

Instance Attribute Details

#helpersObject (readonly)

Returns the value of attribute helpers.



41
42
43
# File 'lib/ddtrace/configuration/option_definition.rb', line 41

def helpers
  @helpers
end

Instance Method Details

#apply_options!(options = {}) ⇒ Object

For applying options for OptionDefinition



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ddtrace/configuration/option_definition.rb', line 97

def apply_options!(options = {})
  return if options.nil? || options.empty?

  default(options[:default]) if options.key?(:default)
  delegate_to(&options[:delegate_to]) if options.key?(:delegate_to)
  depends_on(*options[:depends_on]) if options.key?(:depends_on)
  lazy(options[:lazy]) if options.key?(:lazy)
  on_set(&options[:on_set]) if options.key?(:on_set)
  resetter(&options[:resetter]) if options.key?(:resetter)
  setter(&options[:setter]) if options.key?(:setter)
end

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



66
67
68
# File 'lib/ddtrace/configuration/option_definition.rb', line 66

def default(value = nil, &block)
  @default = block_given? ? block : value
end

#delegate_to(&block) ⇒ Object



70
71
72
# File 'lib/ddtrace/configuration/option_definition.rb', line 70

def delegate_to(&block)
  @delegate_to = block
end

#depends_on(*values) ⇒ Object



62
63
64
# File 'lib/ddtrace/configuration/option_definition.rb', line 62

def depends_on(*values)
  @depends_on = values.flatten
end

#helper(name, *_args, &block) ⇒ Object



74
75
76
# File 'lib/ddtrace/configuration/option_definition.rb', line 74

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

#lazy(value = true) ⇒ Object

rubocop:disable Style/TrivialAccessors (Rubocop erroneously thinks #lazy == #lazy= )



80
81
82
# File 'lib/ddtrace/configuration/option_definition.rb', line 80

def lazy(value = true)
  @lazy = value
end

#metaObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ddtrace/configuration/option_definition.rb', line 113

def meta
  {
    default: @default,
    delegate_to: @delegate_to,
    depends_on: @depends_on,
    lazy: @lazy,
    on_set: @on_set,
    resetter: @resetter,
    setter: @setter
  }
end

#on_set(&block) ⇒ Object



84
85
86
# File 'lib/ddtrace/configuration/option_definition.rb', line 84

def on_set(&block)
  @on_set = block
end

#resetter(&block) ⇒ Object



88
89
90
# File 'lib/ddtrace/configuration/option_definition.rb', line 88

def resetter(&block)
  @resetter = block
end

#setter(&block) ⇒ Object



92
93
94
# File 'lib/ddtrace/configuration/option_definition.rb', line 92

def setter(&block)
  @setter = block
end

#to_definitionObject



109
110
111
# File 'lib/ddtrace/configuration/option_definition.rb', line 109

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