Module: Alap::DeepFreeze
- Defined in:
- lib/alap/deep_freeze.rb
Overview
Recursive immutability — Ruby port of src/core/deepFreeze.ts.
Walks obj recursively and calls .freeze on every Hash and Array it encounters. Strings are deliberately not frozen: JS strings are immutable primitives (there is nothing to freeze), so matching TS semantics means locking containers only. Freezing strings here would also propagate-freeze the caller’s input strings, since Alap::DeepClone shares string references (Ruby strings are reference types; copying every string on clone would be wasteful and pointless). Hash / Array assignment is what we need to prevent — result = “javascript:…” — not leaf-string edits.
Freezes in place, matching TypeScript Object.freeze semantics. Pair with Alap::DeepClone.call on intake — clone detaches from the caller, freeze locks the container shape on return.
Class Method Summary collapse
-
.call(obj) ⇒ Object
Recursively freeze
objand return it.
Class Method Details
.call(obj) ⇒ Object
Recursively freeze obj and return it. Already-frozen sub-objects are left alone.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/alap/deep_freeze.rb', line 26 def self.call(obj) case obj when Hash unless obj.frozen? obj.each_value { |v| call(v) } obj.freeze end when Array unless obj.frozen? obj.each { |v| call(v) } obj.freeze end end obj end |