Sha256: d9c8b749746e6bc15468b3ce521f4f660e65e61912da7531df55c62de0a2e61b

Contents?: true

Size: 948 Bytes

Versions: 6

Compression:

Stored size: 948 Bytes

Contents

# frozen_string_literal: true

module Capistrano
  module DataPlaneApi
    # Include in a class to grant it the `#dig` method.
    # It's implemented so that it calls public methods.
    module Diggable
      # Extracts the nested value specified by the sequence of key objects by calling `dig` at each step,
      # returning `nil` if any intermediate step is `nil`.
      #
      # This implementation of `dig` uses `public_send` under the hood.
      #
      # @raise [TypeError] value has no #dig method
      # @return [Object]
      def dig(*args)
        return unless args.size.positive?

        return unless respond_to?(key = args.shift)

        value = public_send(key)
        return if value.nil?
        return value if args.size.zero?
        raise ::TypeError, "#{value.class} does not have #dig method" unless value.respond_to?(:dig)

        value.dig(*args)
      rescue ::ArgumentError
        nil
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
capistrano-data_plane_api-0.1.5 lib/capistrano/data_plane_api/diggable.rb
capistrano-data_plane_api-0.1.4 lib/capistrano/data_plane_api/diggable.rb
capistrano-data_plane_api-0.1.3 lib/capistrano/data_plane_api/diggable.rb
capistrano-data_plane_api-0.1.2 lib/capistrano/data_plane_api/diggable.rb
capistrano-data_plane_api-0.1.1 lib/capistrano/data_plane_api/diggable.rb
capistrano-data_plane_api-0.1.0 lib/capistrano/data_plane_api/diggable.rb