Class: Mayu::Utils::DeepDuper

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/mayu/utils.rb

Constant Summary collapse

Elem =
type_member { { upper: Object } }

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ DeepDuper

Returns a new instance of DeepDuper.



75
# File 'lib/mayu/utils.rb', line 75

def initialize(obj) = @obj = obj

Instance Method Details

#deep_dupObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mayu/utils.rb', line 78

def deep_dup
  case @obj
  when Hash
    @obj.dup
    T.cast(
      @obj
        .transform_keys { Utils.deep_dup(_1) }
        .transform_values { Utils.deep_dup(_1) },
      Elem
    )
  when Array
    T.cast(@obj.map(&:dup), Elem)
  else
    @obj.dup
  end
end