Module: Flows::Util::InheritableSingletonVars::DupStrategy
- Defined in:
- lib/flows/util/inheritable_singleton_vars/dup_strategy.rb
Overview
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: InheritanceCallback
Constant Summary collapse
- VAR_LIST_VAR_NAME =
:@inheritable_vars_with_dup
Class Method Summary collapse
-
.call(klass, attrs_with_default = {}) ⇒ Object
Applies behaviour and defaults for singleton variables.
-
.migrate(from_mod, to_mod) ⇒ Object
private
Moves variables between modules.
Class Method Details
.call(klass, attrs_with_default = {}) ⇒ Object
Variable names should look like :@var or '@var'.
Applies behaviour and defaults for singleton variables.
66 67 68 69 70 71 72 73 |
# File 'lib/flows/util/inheritable_singleton_vars/dup_strategy.rb', line 66 def call(klass, attrs_with_default = {}) init_variables_with_default_values(klass, attrs_with_default) var_names = attrs_with_default.keys.map(&:to_sym) add_var_list(klass, var_names) 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
78 79 80 81 82 83 84 85 |
# File 'lib/flows/util/inheritable_singleton_vars/dup_strategy.rb', line 78 def migrate(from_mod, to_mod) var_list = from_mod.instance_variable_get(VAR_LIST_VAR_NAME) to_mod.instance_variable_set(VAR_LIST_VAR_NAME, var_list.dup) var_list.each do |name| to_mod.instance_variable_set(name, from_mod.instance_variable_get(name).dup) end end |