Skip to content

Commit

Permalink
Allowing an array to be modified.
Browse files Browse the repository at this point in the history
If a hash is added to an existing array in the ROS tree while recursing
over arrays, that hash will become part of the ROS tree and can have its
keys be called as methods.

Fixes #29
  • Loading branch information
aetherknight committed May 20, 2015
1 parent 54cbd86 commit 26b8c4f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/recursive_open_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +92,4 @@ def _get_key_from_table_(name)
return name.to_sym if @table.has_key?(name.to_sym)
name
end

end

43 changes: 43 additions & 0 deletions spec/recursive_open_struct/recursion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 26b8c4f

Please sign in to comment.