Sha256: c4713d7ae684beaf04f0208bb9c2942f2cf89afa403190987c99140b01ce9ffe

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

##
#
#
module ConvenientService
  module Utils
    module Object
      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

4 entries across 4 versions & 1 rubygems

Version Path
convenient_service-0.5.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.4.0 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.3.1 lib/convenient_service/utils/object/instance_variable_fetch.rb
convenient_service-0.3.0 lib/convenient_service/utils/object/instance_variable_fetch.rb