Sha256: 8197b98ee59efb314084b74fa58b2d0cc50bb9fac1ebe245b00c5cf438d2bbbe

Contents?: true

Size: 816 Bytes

Versions: 1

Compression:

Stored size: 816 Bytes

Contents

module AbstractFeatureBranch
  module Memoizable
    private
    
    # memoizes a variable thread-safe
    # expects a MUTEX constant on the class including this moddule, which pre-initializes
    # mutexes at class definition time
    # Example:
    # MUTEX = { '@varname' => Mutex.new }
    def memoize_thread_safe(variable, variable_build_method_name = nil, &variable_builder)
      variable_builder ||= method(variable_build_method_name)
      if instance_variable_get(variable).nil?
        mutex_hash = self.is_a?(Module) ? self::MUTEX : self.class::MUTEX
        mutex_hash[variable].synchronize do
          if instance_variable_get(variable).nil?
            instance_variable_set(variable, variable_builder.call)
          end
        end
      end
      instance_variable_get(variable)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
abstract_feature_branch-1.6.0 lib/abstract_feature_branch/memoizable.rb