Sha256: dbe90636c0ef77fd7383eba5e96396b45ce6b0b6af86ceccfe7d0f40c520ef2c

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 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/
      #
      # @note: `false` and `nil` are the only values that are considered `falsy` in Ruby.
      # @see https://riptutorial.com/ruby/example/2092/truthy-and-falsy-values
      # @see https://www.ruby-lang.org/en/documentation/faq/6/
      #
      # @internal
      #   NOTE: `falsy` is probably more common than `falsey`.
      #   - https://developer.mozilla.org/en-US/docs/Glossary/Falsy
      #
      class MemoizeIncludingFalsyValues < Support::Command
        ##
        # @!attribute [r] object
        #   @return [Object]
        #
        attr_reader :object

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

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

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

        ##
        # @return [Object] Value of ivar. Can be any type.
        #
        def call
          Utils::Object.instance_variable_fetch(object, ivar_name, &value_block)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
convenient_service-0.12.0 lib/convenient_service/utils/object/memoize_including_falsy_values.rb
convenient_service-0.11.0 lib/convenient_service/utils/object/memoize_including_falsy_values.rb
convenient_service-0.10.1 lib/convenient_service/utils/object/memoize_including_falsy_values.rb
convenient_service-0.10.0 lib/convenient_service/utils/object/memoize_including_falsy_values.rb
convenient_service-0.9.0 lib/convenient_service/utils/object/memoize_including_falsy_values.rb