Sha256: 2a38563eb8eaea38e8acc2803fbda362c8bcdf257d909671c1caf16432060837

Contents?: true

Size: 1.31 KB

Versions: 23

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

##
# @example
#   module Test
#   end
#
#   ConvenientService::Utils::Module::GetOwnConst.call(Test, :File)
#   # => nil, not File from Ruby Core.
#
#   module Test
#     class File
#     end
#   end
#
#   ConvenientService::Utils::Module::GetOwnConst.call(Test, :File)
#   # => Test::File
#
module ConvenientService
  module Utils
    module Module
      class GetOwnConst < Support::Command
        ##
        # @!attribute [r] mod
        #   @return [Class, Module]
        #
        attr_reader :mod

        ##
        # @!attribute [r] const_name
        #   @return [Symbol]
        #
        attr_reader :const_name

        ##
        # @param mod [Class, Module]
        # @param const_name [Symbol]
        # @return [void]
        #
        def initialize(mod, const_name)
          @mod = mod
          @const_name = const_name
        end

        ##
        # @return [Object] Value of own const. Can be any type.
        #
        def call
          ##
          # NOTE: > If `inherit` is `false`, the lookup only checks the constants in the receiver:
          # https://ruby-doc.org/core-3.0.0/Module.html#method-i-const_defined-3F
          #
          return unless mod.const_defined?(const_name, false)

          mod.const_get(const_name, false)
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
convenient_service-0.19.1 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.19.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.18.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.17.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.16.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.15.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.14.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.13.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.12.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.11.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.10.1 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.10.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.9.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.8.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.7.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.6.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.5.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.4.0 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.3.1 lib/convenient_service/utils/module/get_own_const.rb
convenient_service-0.3.0 lib/convenient_service/utils/module/get_own_const.rb