Class: ConvenientService::Utils::Module::FetchOwnConst Private

Inherits:
Support::Command show all
Defined in:
lib/convenient_service/utils/module/fetch_own_const.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Returns a constant that is defined directly in module or defines it there.

Examples:

Two args form (works as GetOwnConst).

module Test
end

ConvenientService::Utils::Module::FetchOwnConst.call(Test, :File)
# => nil, not File from Ruby Core.

module Test
  class File
  end
end

ConvenientService::Utils::Module::FetchOwnConst.call(Test, :File)
# => Test::File

Two args + block form.

module Test
end

ConvenientService::Utils::Module::FetchOwnConst.call(Test, :File) { Class.new }
# => Test::File, just created.

module Test
  class File
  end
end

ConvenientService::Utils::Module::FetchOwnConst.call(Test, :File)
# => Test::File, already existing.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(mod, const_name, &fallback_block) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Since:

  • 1.0.0



72
73
74
75
76
# File 'lib/convenient_service/utils/module/fetch_own_const.rb', line 72

def initialize(mod, const_name, &fallback_block)
  @mod = mod
  @const_name = const_name
  @fallback_block = fallback_block
end

Instance Attribute Details

#const_nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



58
59
60
# File 'lib/convenient_service/utils/module/fetch_own_const.rb', line 58

def const_name
  @const_name
end

#fallback_blockObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



64
65
66
# File 'lib/convenient_service/utils/module/fetch_own_const.rb', line 64

def fallback_block
  @fallback_block
end

#modObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



52
53
54
# File 'lib/convenient_service/utils/module/fetch_own_const.rb', line 52

def mod
  @mod
end

Instance Method Details

#callObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Value of own const. Can be any type.

Returns:

  • (Object)

    Value of own const. Can be any type.

Since:

  • 1.0.0



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/convenient_service/utils/module/fetch_own_const.rb', line 84

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 mod.const_get(const_name, false) if mod.const_defined?(const_name, false)

  return mod.const_set(const_name, fallback_block.call) if fallback_block

  nil
end