Permalink
Browse files

Add guard clause to prevent refreezing an object

  • Loading branch information...
1 parent 46fba18 commit d6cb350e9aa4979896b39a4c3b6ad63e921b28dd @dkubb committed Apr 1, 2012
Showing with 29 additions and 13 deletions.
  1. +0 −5 TODO
  2. +1 −1 config/flay.yml
  3. +1 −0 lib/ice_nine.rb
  4. +27 −7 spec/unit/ice_nine/class_methods/deep_freeze_spec.rb
View
5 TODO
@@ -1,7 +1,2 @@
-* Add a guard clause to IceNine.deep_freeze to return the object immediately if
- it is already frozen.
- * If something is already frozen we want to trust that it's already been
- visited by IceNine, or some user provided code. We do not want to attempt to
- re-freeze the same objects over and over.
* Add a cache in IceNine::Freezer[] to avoid looking up the Freezer class more
than once per type.
View
2 config/flay.yml
@@ -1,3 +1,3 @@
---
threshold: 6
-total_score: 39.0
+total_score: 41.0
View
1 lib/ice_nine.rb
@@ -37,6 +37,7 @@ module IceNine
#
# @api public
def self.deep_freeze(object)
+ return object if object.frozen?
RecursionGuard.guard(object.object_id) do
Freezer[object.class].deep_freeze(object)
end
View
34 spec/unit/ice_nine/class_methods/deep_freeze_spec.rb
@@ -13,15 +13,35 @@
value.instance_eval { @a = '1' }
end
- it 'returns the object' do
- should be(value)
- end
+ context 'when the object is not frozen' do
+ it 'returns the object' do
+ should be(value)
+ end
+
+ it 'freezes the object' do
+ expect { subject }.should change(value, :frozen?).from(false).to(true)
+ end
- it 'freezes the object' do
- expect { subject }.should change(value, :frozen?).from(false).to(true)
+ it 'freezes the instance variables in the Object' do
+ subject.instance_variable_get(:@a).should be_frozen
+ end
end
- it 'freezes the instance variables in the Object' do
- subject.instance_variable_get(:@a).should be_frozen
+ context 'when the object is frozen' do
+ before do
+ value.freeze
+ end
+
+ it 'returns the object' do
+ should be(value)
+ end
+
+ it 'does not freeze the object' do
+ expect { subject }.should_not change(value, :frozen?).from(true)
+ end
+
+ it 'does not freeze the instance variables in the Object' do
+ subject.instance_variable_get(:@a).should_not be_frozen
+ end
end
end

0 comments on commit d6cb350

Please sign in to comment.