Class: Datadog::Core::Pin
- Inherits:
-
Object
- Object
- Datadog::Core::Pin
- 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
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
-
#initialize(**options) ⇒ Pin
constructor
A new instance of Pin.
- #key?(name) ⇒ Boolean
-
#onto(obj) ⇒ Object
rubocop:disable Style/TrivialAccessors.
-
#to_s ⇒ Object
rubocop:enable Style/TrivialAccessors.
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 = 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, **) if (pin = get_from(obj)) .each { |k, v| pin[k] = v } else pin = new(**) 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
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_s ⇒ Object
rubocop:enable Style/TrivialAccessors
63 64 65 66 |
# File 'lib/datadog/core/pin.rb', line 63 def to_s = .to_a.map { |k, v| "#{k}:#{v}" }.join(', ') "Pin(#{})" end |