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:



67
68
69
70
71
# File 'lib/convenient_service/utils/module/fetch_own_const.rb', line 67

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.



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

def const_name
  @const_name
end

#fallback_blockObject (readonly)

Returns the value of attribute fallback_block.



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

def fallback_block
  @fallback_block
end

#modObject (readonly)

Returns the value of attribute mod.



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

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.



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/convenient_service/utils/module/fetch_own_const.rb', line 79

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