recursive-open-struct icon indicating copy to clipboard operation
recursive-open-struct copied to clipboard

Freeze is not working + add deep freeze / immutable

Open kke opened this issue 5 years ago • 2 comments

Freeze does not work:

> r = RecursiveOpenStruct.new( { hello: 'world', foo: [ {bar: :baz} ] }, recurse_over_arrays: true)
> r.freeze
> r.delete_field('foo')
> r
=> #<RecursiveOpenStruct hello="world">
> r.bar = 'baz'
> r
=> #<RecursiveOpenStruct hello="world", bar="baz">

Deep freeze would be nice:

> r = RecursiveOpenStruct.new( { hello: 'world', foo: [ {bar: :baz} ] }, recurse_over_arrays: true)
> r.deep_freeze
> r.hello.reverse!
FrozenError (can't modify frozen String)
> r.foo << { dog: :cat }
FrozenError (can't modify frozen Array)
> r.foo.first.bar = :closed
FrozenError (can't modify frozen RecursiveOpenStruct)

This could be done after initialization if immutable: true has been given:

> r = RecursiveOpenStruct.new( { hello: 'world', foo: [ {bar: :baz} ] }, recurse_over_arrays: true, immutable: true)
> r.hello = 'universe'
FrozenError (can't modify frozen RecursiveOpenStruct)
> r.foo << { dog: :cat }
FrozenError (can't modify frozen Array)
> r.foo.first.bar = :closed
FrozenError (can't modify frozen RecursiveOpenStruct)

kke avatar Aug 09 '19 08:08 kke

Release 1.2.2, which contains https://github.com/aetherknight/recursive-open-struct/pull/75 gets #freeze working, however #deep_freeze still does not work as expected, and is still an open problem.

aetherknight avatar Jun 19 '24 03:06 aetherknight

Maybe I'll submit another PR for #deep_freeze if I can find the time. :)

Richard-Degenne avatar Jun 26 '24 09:06 Richard-Degenne