Module: Musa::Extension::DeepCopy::DeepCopy

Extended by:
DeepCopy
Included in:
DeepCopy
Defined in:
lib/musa-dsl/core-ext/deep-copy.rb

Overview

Main deep copy module providing class methods.

Class Method Summary collapse

Instance Method Summary collapse

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.

Examples:

source = [60, 100].extend(Musa::Datasets::V)
target = [60, 100]
DeepCopy.copy_singleton_class_modules(source, target)
target.is_a?(Musa::Datasets::V)  # => true

Parameters:

  • source (Object)

    object whose singleton modules to copy.

  • target (Object)

    object to receive the modules.

Returns:

  • (Object)

    target with modules applied.



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.

Parameters:

  • object (Object)

    object to copy.

  • method (Symbol) (defaults to: :dup)

    :dup or :clone.

  • freeze (Boolean, nil) (defaults to: nil)

    for :clone, whether to freeze the copy.

Returns:

  • (Object)

    deep copy of the object.

Raises:

  • (ArgumentError)

    if method is not :dup or :clone.



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.

Examples:

source = [60, 100].extend(Musa::Datasets::V)
target = [60, 100]
DeepCopy.copy_singleton_class_modules(source, target)
target.is_a?(Musa::Datasets::V)  # => true

Parameters:

  • source (Object)

    object whose singleton modules to copy.

  • target (Object)

    object to receive the modules.

Returns:

  • (Object)

    target with modules applied.



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.

Parameters:

  • object (Object)

    object to copy.

  • method (Symbol) (defaults to: :dup)

    :dup or :clone.

  • freeze (Boolean, nil) (defaults to: nil)

    for :clone, whether to freeze the copy.

Returns:

  • (Object)

    deep copy of the object.

Raises:

  • (ArgumentError)

    if method is not :dup or :clone.



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