diff --git a/lib/recursive_open_struct.rb b/lib/recursive_open_struct.rb index 18cc9a4..a3ec80e 100644 --- a/lib/recursive_open_struct.rb +++ b/lib/recursive_open_struct.rb @@ -50,6 +50,7 @@ class << self; self; end.class_eval do :mutate_input_hash => true) elsif v.is_a?(Array) and @recurse_over_arrays @sub_elements[key_name] ||= recurse_over_array(v) + @sub_elements[key_name] = recurse_over_array(@sub_elements[key_name]) else v end @@ -91,6 +92,4 @@ def _get_key_from_table_(name) return name.to_sym if @table.has_key?(name.to_sym) name end - end - diff --git a/spec/recursive_open_struct/recursion_spec.rb b/spec/recursive_open_struct/recursion_spec.rb index 0c5e952..c37046f 100644 --- a/spec/recursive_open_struct/recursion_spec.rb +++ b/spec/recursive_open_struct/recursion_spec.rb @@ -236,6 +236,49 @@ it { expect(subject.blah[0][:foo]).to eq '1' } end # when recursing over arrays is disabled + describe 'modifying an array and recursing over it' do + let(:h) { {} } + subject { RecursiveOpenStruct.new(h, recurse_over_arrays: true) } + + context 'when adding an array with hashes into the tree' do + before(:each) do + subject.mystery = {} + subject.mystery.science = [{ theatre: 9000 }] + end + + it "ROS's it" do + expect(subject.mystery.science[0].theatre).to eq 9000 + end + end + + context 'when appending a hash to an array' do + before(:each) do + subject.mystery = {} + subject.mystery.science = [] + subject.mystery.science << { theatre: 9000 } + end + + it "ROS's it" do + expect(subject.mystery.science[0].theatre).to eq 9000 + end + end + + context 'after appending a hash to an array' do + before(:each) do + subject.mystery = {} + subject.mystery.science = [] + subject.mystery.science[0] = {} + end + + it "can have new values be set" do + expect do + subject.mystery.science[0].theatre = 9000 + end.to_not raise_error + + expect(subject.mystery.science[0].theatre).to eq 9000 + end + end + end # modifying an array and then recursing end # recursing over arrays end # recursive behavior end