Module: Flows::Util::InheritableSingletonVars::IsolationStrategy

Defined in:
lib/flows/util/inheritable_singleton_vars/isolation_strategy.rb

Overview

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.

Defined Under Namespace

Modules: Injector, Migrator

Constant Summary collapse

VAR_MAP_VAR_NAME =

Since:

  • 0.4.0

:@inheritable_vars_with_isolation

Class Method Summary collapse

Class Method Details

.make_module(vars_with_default = {}) ⇒ Object

Note:

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

Applies behaviour and defaults for singleton variables.

Examples:

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

  include SingletonVarsSetup
end

Parameters:

  • vars_with_default (Hash<Symbol, String => Proc>) (defaults to: {})

    keys are variable names, values are procs or lambdas which return default values.

Since:

  • 0.4.0



72
73
74
75
76
77
78
# 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