Module: Musa::Extension::DeepCopy::DeepCopy
Overview
Main deep copy module providing class methods.
Class Method Summary collapse
-
.copy_singleton_class_modules(source, target) ⇒ Object
Copies singleton class modules from source to target.
-
.deep_copy(object, method: :dup, freeze: nil) ⇒ Object
Creates a deep copy of an object, recursively copying nested structures.
Instance Method Summary collapse
-
#copy_singleton_class_modules(source, target) ⇒ Object
Copies singleton class modules from source to target.
-
#deep_copy(object, method: :dup, freeze: nil) ⇒ Object
Creates a deep copy of an object, recursively copying nested structures.
Class Method Details
.copy_singleton_class_modules(source, target) ⇒ Object
Copies singleton class modules from source to target.
This is essential for preserving dataset extensions (P, V, AbsI, etc.) when copying musical data structures. Without this, copied objects would lose their dataset behaviors.
85 86 87 88 89 90 91 |
# File 'lib/musa-dsl/core-ext/deep-copy.rb', line 85 def copy_singleton_class_modules(source, target) source.singleton_class.included_modules.each do |m| target.extend m unless target.is_a?(m) end target end |
.deep_copy(object, method: :dup, freeze: nil) ⇒ Object
Creates a deep copy of an object, recursively copying nested structures.
Uses an object registry to handle circular references, ensuring each object is copied only once and all references point to the same copy.
62 63 64 65 66 67 |
# File 'lib/musa-dsl/core-ext/deep-copy.rb', line 62 def deep_copy(object, method: :dup, freeze: nil) raise ArgumentError, "deep_copy method can only be :dup or :clone" unless method == :dup || method == :clone register = {} _deep_copy(register, object, method, freeze) end |
Instance Method Details
#copy_singleton_class_modules(source, target) ⇒ Object
Copies singleton class modules from source to target.
This is essential for preserving dataset extensions (P, V, AbsI, etc.) when copying musical data structures. Without this, copied objects would lose their dataset behaviors.
85 86 87 88 89 90 91 |
# File 'lib/musa-dsl/core-ext/deep-copy.rb', line 85 def copy_singleton_class_modules(source, target) source.singleton_class.included_modules.each do |m| target.extend m unless target.is_a?(m) end target end |
#deep_copy(object, method: :dup, freeze: nil) ⇒ Object
Creates a deep copy of an object, recursively copying nested structures.
Uses an object registry to handle circular references, ensuring each object is copied only once and all references point to the same copy.
62 63 64 65 66 67 |
# File 'lib/musa-dsl/core-ext/deep-copy.rb', line 62 def deep_copy(object, method: :dup, freeze: nil) raise ArgumentError, "deep_copy method can only be :dup or :clone" unless method == :dup || method == :clone register = {} _deep_copy(register, object, method, freeze) end |