Class: ConvenientService::Utils::Object::InstanceVariableFetch
- Inherits:
-
Support::Command
- Object
- Support::Command
- ConvenientService::Utils::Object::InstanceVariableFetch
- Defined in:
- lib/convenient_service/utils/object/instance_variable_fetch.rb
Overview
Can be used instead of return @ivar if defined? @ivar
.
Instance Attribute Summary collapse
-
#fallback_block ⇒ Object
readonly
Returns the value of attribute fallback_block.
-
#ivar_name ⇒ Object
readonly
Returns the value of attribute ivar_name.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
-
#call ⇒ Object
Value of ivar.
- #initialize(object, ivar_name, &fallback_block) ⇒ void constructor
Methods inherited from Support::Command
Constructor Details
#initialize(object, ivar_name, &fallback_block) ⇒ void
38 39 40 41 42 |
# File 'lib/convenient_service/utils/object/instance_variable_fetch.rb', line 38 def initialize(object, ivar_name, &fallback_block) @object = object @ivar_name = ivar_name @fallback_block = fallback_block end |
Instance Attribute Details
#fallback_block ⇒ Object (readonly)
Returns the value of attribute fallback_block.
30 31 32 |
# File 'lib/convenient_service/utils/object/instance_variable_fetch.rb', line 30 def fallback_block @fallback_block end |
#ivar_name ⇒ Object (readonly)
Returns the value of attribute ivar_name.
24 25 26 |
# File 'lib/convenient_service/utils/object/instance_variable_fetch.rb', line 24 def ivar_name @ivar_name end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
18 19 20 |
# File 'lib/convenient_service/utils/object/instance_variable_fetch.rb', line 18 def object @object end |
Instance Method Details
#call ⇒ Object
Returns Value of ivar. Can be any type.
47 48 49 50 51 52 53 |
# File 'lib/convenient_service/utils/object/instance_variable_fetch.rb', line 47 def call return object.instance_variable_get(ivar_name) if object.instance_variable_defined?(ivar_name) return object.instance_variable_set(ivar_name, fallback_block.call) if fallback_block nil end |