Sha256: 8bc4377c4d419c95422d50ded3ad96c41630b60ce50e3b4e39e0c5e9974130bb

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

##
# @example Usual class.
#   ConvenientService::Utils::Class::DisplayName.call(String)
#   # => "String"
#
# @example Anonymous class.
#   ConvenientService::Utils::Class::DisplayName.call(Сlass.new)
#   # => "AnonymousClass(#76940)"
#
module ConvenientService
  module Utils
    module Class
      class DisplayName < Support::Command
        ##
        # @api private
        #
        # @!attribute [r] klass
        #   @return [Class]
        #
        attr_reader :klass

        ##
        # @api private
        #
        # @param klass [Class]
        # @return [void]
        #
        def initialize(klass)
          @klass = klass
        end

        ##
        # @api private
        #
        # @return [String]
        #
        # @internal
        #   TODO: `Module#name` returns `nil` for anonymous classes.
        #   - https://ruby-doc.org/core-2.7.0/Module.html#method-i-name
        #
        def call
          klass.name || "AnonymousClass(##{klass.object_id})"
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
convenient_service-0.19.1 lib/convenient_service/utils/class/display_name.rb
convenient_service-0.19.0 lib/convenient_service/utils/class/display_name.rb
convenient_service-0.18.0 lib/convenient_service/utils/class/display_name.rb
convenient_service-0.17.0 lib/convenient_service/utils/class/display_name.rb
convenient_service-0.16.0 lib/convenient_service/utils/class/display_name.rb
convenient_service-0.15.0 lib/convenient_service/utils/class/display_name.rb
convenient_service-0.14.0 lib/convenient_service/utils/class/display_name.rb
convenient_service-0.13.0 lib/convenient_service/utils/class/display_name.rb