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: InheritanceCallback

Constant Summary collapse

VAR_MAP_VAR_NAME =

Since:

  • 0.4.0

:@inheritable_vars_with_isolation

Class Method Summary collapse

Class Method Details

.call(klass, attrs_with_default = {}) ⇒ Object

Note:

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

Applies behaviour and defaults for singleton variables.

Examples:

class MyClass
  Flows::Util::InheritableSingletonVars::IsolationStrategy.call(
    self,
    :@my_list => -> { [] }
  )
end

Parameters:

  • klass (Class)

    target class.

  • attrs_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



59
60
61
62
63
64
65
66
# File 'lib/flows/util/inheritable_singleton_vars/isolation_strategy.rb', line 59

def call(klass, attrs_with_default = {})
  init_variables_with_default_values(klass, attrs_with_default)

  var_defaults = attrs_with_default
  add_variables_to_store(klass, var_defaults)

  inject_inheritance_hook(klass)
end

.migrate(from_mod, to_mod) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Moves variables between modules

Since:

  • 0.4.0



71
72
73
74
75
76
77
78
79
# File 'lib/flows/util/inheritable_singleton_vars/isolation_strategy.rb', line 71

def migrate(from_mod, to_mod)
  new_var_map = from_mod.instance_variable_get(VAR_MAP_VAR_NAME).dup

  to_mod.instance_variable_set(VAR_MAP_VAR_NAME, new_var_map)

  new_var_map.each do |name, default_value_proc|
    to_mod.instance_variable_set(name, default_value_proc.call)
  end
end