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.



25
26
27
# File 'lib/datadog/core/pin.rb', line 25

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

Class Method Details

.get_from(obj) ⇒ Object



8
9
10
11
12
# File 'lib/datadog/core/pin.rb', line 8

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

  obj.datadog_pin
end

.set_on(obj, **options) ⇒ Object



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

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



29
30
31
# File 'lib/datadog/core/pin.rb', line 29

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

#[]=(name, value) ⇒ Object



33
34
35
# File 'lib/datadog/core/pin.rb', line 33

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

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/datadog/core/pin.rb', line 37

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

#onto(obj) ⇒ Object

rubocop:disable Style/TrivialAccessors



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

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



63
64
65
66
# File 'lib/datadog/core/pin.rb', line 63

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