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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

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

Parameters:



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

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

Instance Attribute Details

#const_nameObject (readonly)

Returns the value of attribute const_name.



48
49
50
# File 'lib/convenient_service/utils/module/fetch_own_const.rb', line 48

def const_name
  @const_name
end

#fallback_blockObject (readonly)

Returns the value of attribute fallback_block.



54
55
56
# File 'lib/convenient_service/utils/module/fetch_own_const.rb', line 54

def fallback_block
  @fallback_block
end

#modObject (readonly)

Returns the value of attribute mod.



42
43
44
# File 'lib/convenient_service/utils/module/fetch_own_const.rb', line 42

def mod
  @mod
end

Instance Method Details

#callObject

Returns Value of own const. Can be any type.

Returns:

  • (Object)

    Value of own const. Can be any type.



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/convenient_service/utils/module/fetch_own_const.rb', line 74

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