Class: ConvenientService::Utils::Object::MemoizeIncludingFalsyValues

Inherits:
Support::Command
  • Object
show all
Defined in:
lib/convenient_service/utils/object/memoize_including_falsy_values.rb

Overview

Note:

‘false` and `nil` are the only values that are considered `falsy` in Ruby.

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, &value_block) ⇒ void

Parameters:

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


46
47
48
49
50
# File 'lib/convenient_service/utils/object/memoize_including_falsy_values.rb', line 46

def initialize(object, ivar_name, &value_block)
  @object = object
  @ivar_name = ivar_name
  @value_block = value_block
end

Instance Attribute Details

#ivar_nameObject (readonly)

Returns the value of attribute ivar_name.



32
33
34
# File 'lib/convenient_service/utils/object/memoize_including_falsy_values.rb', line 32

def ivar_name
  @ivar_name
end

#objectObject (readonly)

Returns the value of attribute object.



26
27
28
# File 'lib/convenient_service/utils/object/memoize_including_falsy_values.rb', line 26

def object
  @object
end

#value_blockObject (readonly)

Returns the value of attribute value_block.



38
39
40
# File 'lib/convenient_service/utils/object/memoize_including_falsy_values.rb', line 38

def value_block
  @value_block
end

Instance Method Details

#callObject

Returns Value of ivar. Can be any type.

Returns:

  • (Object)

    Value of ivar. Can be any type.



55
56
57
# File 'lib/convenient_service/utils/object/memoize_including_falsy_values.rb', line 55

def call
  Utils::Object.instance_variable_fetch(object, ivar_name, &value_block)
end