aetherknight / recursive-open-struct

OpenStruct subclass that returns nested hash attributes as RecursiveOpenStructs
Other
276 stars 54 forks source link

Freeze is not working + add deep freeze / immutable #61

Open kke opened 5 years ago

kke commented 5 years ago

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)
aetherknight commented 2 months ago

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.

Richard-Degenne commented 2 months ago

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