Sha256: b9fbf2a5fe29cec4106dd689755f1fed50a2189174227c2fd0d3aed544503bde

Contents?: true

Size: 1.35 KB

Versions: 16

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

##
#
#
module ConvenientService
  module Utils
    module Object
      ##
      # Can be used instead of `return @ivar if defined? @ivar`.
      # @see https://www.justinweiss.com/articles/4-simple-memoization-patterns-in-ruby-and-one-gem/
      #
      class InstanceVariableFetch < Support::Command
        ##
        # @!attribute [r] object
        #   @return [Object]
        #
        attr_reader :object

        ##
        # @!attribute [r] ivar_name
        #   @return [Symbol, String]
        #
        attr_reader :ivar_name

        ##
        # @!attribute [r] fallback_block
        #   @return [Proc, nil]
        #
        attr_reader :fallback_block

        ##
        # @param object [Object]
        # @param ivar_name [Symbol]
        # @param fallback_block [Proc]
        # @return [void]
        #
        def initialize(object, ivar_name, &fallback_block)
          @object = object
          @ivar_name = ivar_name
          @fallback_block = fallback_block
        end

        ##
        # @return [Object] Value of ivar. Can be any type.
        #
        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
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
convenient_service-0.19.1 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.19.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.18.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.17.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.16.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.15.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.14.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.13.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.12.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.11.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.10.1 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.10.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.9.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.8.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.7.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.6.0 lib/convenient_service/utils/object/instance_variable_fetch.rb