Sha256: 127175470e826d32843afa2c7a034dea1f2ff90ed750e20e9a92627283fc2e4a

Contents?: true

Size: 1.51 KB

Versions: 16

Compression:

Stored size: 1.51 KB

Contents

# typed: true

module Datadog
  module Core
    # A {Datadog::Core::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 Pin
      def self.get_from(obj)
        return nil unless obj.respond_to? :datadog_pin

        obj.datadog_pin
      end

      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

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

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

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

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

      # rubocop:disable Style/TrivialAccessors
      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
      # rubocop:enable Style/TrivialAccessors

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

      private

      attr_accessor :options
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ddtrace-1.9.0 lib/datadog/core/pin.rb
ddtrace-1.8.0 lib/datadog/core/pin.rb
ddtrace-1.7.0 lib/datadog/core/pin.rb
ddtrace-1.6.1 lib/datadog/core/pin.rb
ddtrace-1.6.0 lib/datadog/core/pin.rb
ddtrace-1.5.2 lib/datadog/core/pin.rb
ddtrace-1.5.1 lib/datadog/core/pin.rb
ddtrace-1.5.0 lib/datadog/core/pin.rb
ddtrace-1.4.2 lib/datadog/core/pin.rb
ddtrace-1.4.1 lib/datadog/core/pin.rb
ddtrace-1.4.0 lib/datadog/core/pin.rb
ddtrace-1.3.0 lib/datadog/core/pin.rb
ddtrace-1.2.0 lib/datadog/core/pin.rb
ddtrace-1.1.0 lib/datadog/core/pin.rb
ddtrace-1.0.0 lib/datadog/core/pin.rb
ddtrace-1.0.0.beta2 lib/datadog/core/pin.rb