Skip to content

Commit

Permalink
Add #freeze override
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard-Degenne committed Mar 2, 2023
1 parent 9f80c7f commit 8ad69f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/recursive_open_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ def method_missing(mid, *args)
end
end

def freeze
@table.each_key do |key|
new_ostruct_member!(key)
end

super
end

# TODO: Rename to new_ostruct_member! once we care less about Rubies before
# 2.4.0.
def new_ostruct_member(name)
Expand Down
18 changes: 17 additions & 1 deletion spec/recursive_open_struct/open_struct_behavior_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
ros.freeze
end

it "can read existing keys", pending: 'Awaiting fix' do
it "can read existing keys" do
expect(ros.asdf).to eq 'John Smith'
end

Expand All @@ -127,6 +127,22 @@
it "cannot write existing keys" do
expect { ros.asdf = 'new_value' }.to raise_error FrozenError
end

context "with recursive structure" do
let(:hash) { { :key => { :subkey => 42 } } }

it "can read existing sub-elements" do
expect(ros.key.subkey).to eq 42
end

it "can write new sub-elements" do
expect { ros.key.new_subkey = 43 }.not_to raise_error
end

it "can write existing sub-elements" do
expect { ros.key.subkey = 43 }.not_to raise_error
end
end
end
end # describe behavior it inherits from OpenStruct
end

0 comments on commit 8ad69f6

Please sign in to comment.