Module: Flows::Util::InheritableSingletonVars::DupStrategy

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

Overview

Note:

If you change variables in a parent class after a child being defined it will have no effect on a child. Remember this when working in environments with tricky or experimental autoload mechanism.

Strategy which uses #dup to copy variables to a child class.

Can be applied several times to the same class.

Can be applied in the middle of inheritance chain.

When your value is a custom class you may need to adjust #dup behaviour. It can be done using initialize_dup method. Unfortunately it's not documented well in the standard library. So, this will help you.

Defined Under Namespace

Modules: Injector, Migrator

Constant Summary collapse

VAR_LIST_VAR_NAME =

Since:

  • 0.4.0

:@inheritable_vars_with_dup

Class Method Summary collapse

Class Method Details

.make_module(vars_with_default = {}) ⇒ Object

Note:

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

Generates a module which applies behaviour and defaults for singleton variables.

Examples:

class MyClass
  SingletonVarsSetup = Flows::Util::InheritableSingletonVars::DupStrategy.make_module(
    :@my_list => []
  )

  include SingletonVarsSetup
end

Parameters:

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

    keys are variable names, values are default values.

Since:

  • 0.4.0



81
82
83
84
85
86
87
# File 'lib/flows/util/inheritable_singleton_vars/dup_strategy.rb', line 81

def make_module(vars_with_default = {})
  Module.new.tap do |mod|
    mod.instance_variable_set(VAR_LIST_VAR_NAME, vars_with_default.keys.map(&:to_sym))
    init_vars(mod, vars_with_default)
    mod.extend Injector
  end
end