Class: Datadog::Core::Pin

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/pin.rb

Overview

A Pin sets metadata on a particular object.

This is useful if you want the object to reflect customized behavior or attributes, like an eigenclass.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Pin

Returns a new instance of Pin.



27
28
29
# File 'lib/datadog/core/pin.rb', line 27

def initialize(**options)
  @options = options
end

Class Method Details

.get_from(obj) ⇒ Object



10
11
12
13
14
# File 'lib/datadog/core/pin.rb', line 10

def self.get_from(obj)
  return nil unless obj.respond_to? :datadog_pin

  obj.datadog_pin
end

.set_on(obj, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/datadog/core/pin.rb', line 16

def self.set_on(obj, **options)
  if (pin = get_from(obj))
    options.each { |k, v| pin[k] = v }
  else
    pin = new(**options)
    pin.onto(obj)
  end

  pin
end

Instance Method Details

#[](name) ⇒ Object



31
32
33
# File 'lib/datadog/core/pin.rb', line 31

def [](name)
  @options[name]
end

#[]=(name, value) ⇒ Object



35
36
37
# File 'lib/datadog/core/pin.rb', line 35

def []=(name, value)
  @options[name] = value
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/datadog/core/pin.rb', line 39

def key?(name)
  @options.key?(name)
end

#onto(obj) ⇒ Object

rubocop:disable Style/TrivialAccessors



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/datadog/core/pin.rb', line 44

def onto(obj)
  unless obj.respond_to? :datadog_pin=
    obj.instance_exec do
      def datadog_pin=(pin)
        @datadog_pin = pin
      end
    end
  end

  unless obj.respond_to? :datadog_pin
    obj.instance_exec do
      def datadog_pin
        @datadog_pin
      end
    end
  end

  obj.datadog_pin = self
end

#to_sObject

rubocop:enable Style/TrivialAccessors



65
66
67
68
# File 'lib/datadog/core/pin.rb', line 65

def to_s
  pretty_options = options.to_a.map { |k, v| "#{k}:#{v}" }.join(', ')
  "Pin(#{pretty_options})"
end