module Flows::Util::InheritableSingletonVars::IsolationStrategy

Strategy which uses procs to generate initial values in target class and children.

This strategy designed to make fully isolated singleton variables between classes.

Can be applied several times to the same class.

Can be applied in the middle of inheritance chain.

@see InheritableSingletonVars the parent module's documentation describes the problem this module solves.

@since 0.4.0

Constants

VAR_MAP_VAR_NAME

Public Class Methods

make_module(vars_with_default = {}) click to toggle source

Applies behaviour and defaults for singleton variables.

@example

class MyClass
  SingletonVarsSetup = Flows::Util::InheritableSingletonVars::IsolationStrategy.make_module(
    :@my_list => -> { [] }
  )

  include SingletonVarsSetup
end

@note Variable names should look like `:@var` or `'@var'`.

@param vars_with_default [Hash<Symbol, String => Proc>] keys are variable names,

values are procs or lambdas which return default values.
# File lib/flows/util/inheritable_singleton_vars/isolation_strategy.rb, line 72
def make_module(vars_with_default = {})
  Module.new.tap do |mod|
    mod.instance_variable_set(VAR_MAP_VAR_NAME, vars_with_default.dup)
    init_vars(mod, vars_with_default)
    mod.extend Injector
  end
end

Private Class Methods

init_vars(mod, vars_with_default) click to toggle source
# File lib/flows/util/inheritable_singleton_vars/isolation_strategy.rb, line 82
def init_vars(mod, vars_with_default)
  vars_with_default.each do |name, value_proc|
    mod.instance_variable_set(name, value_proc.call)
  end
end