Class: ConvenientService::Utils::Object::InstanceVariableFetch

Inherits:
Support::Command
  • Object
show all
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

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(object, ivar_name, &fallback_block) ⇒ void

Parameters:

  • object (Object)
  • ivar_name (Symbol)
  • fallback_block (Proc)


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_blockObject (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_nameObject (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

#objectObject (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

#callObject

Returns Value of ivar. Can be any type.

Returns:

  • (Object)

    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