Permalink
Browse files

Being able to pass a block to 'unextend' and only unextending a modul…

…e when the block passes
  • Loading branch information...
1 parent a0802ee commit 0dfd36306dd7575e0cbdfa414915ea51dcf26c0e Paul Engel committed May 1, 2011
Showing with 33 additions and 3 deletions.
  1. +5 −3 lib/unextendable/object.rb
  2. +28 −0 test/object_instance_test.rb
@@ -26,12 +26,14 @@ def extend(*modules)
end
end
- def unextend(*modules)
+ def unextend(*modules, &block)
if modules.empty?
- meta_class.extended_modules.delete_if{|mod| mod.unextendable?}
+ meta_class.extended_modules.delete_if do |mod|
+ mod.unextendable? if !block_given? || block.call(mod)
+ end
else
modules.each do |mod|
- meta_class.extended_modules.delete mod
+ meta_class.extended_modules.delete mod if !block_given? || block.call(mod)
end
end
end
@@ -143,6 +143,23 @@ def name
assert !@c.meta_class.extended_modules.include?(U)
end
+ should "remove the module but when passed a block only when it passes" do
+ assert_equal "Mr. C", @c.salutation
+
+ @c.extend U
+ assert_equal "Mr. U", @c.salutation
+
+ @c.unextend do |mod|
+ mod != U
+ end
+ assert_equal "Mr. U", @c.salutation
+
+ @c.unextend do |mod|
+ mod == U
+ end
+ assert_equal "Mr. C", @c.salutation
+ end
+
context "when calling an unextendable method" do
should "match the expected method" do
assert_equal @c.method(:name), @c.send(:method_for, :name)
@@ -207,6 +224,17 @@ def name
@c.unextend
assert_equal "Ms. A", @c.salutation
+
+ @c.extend U
+ @c.unextend do |mod|
+ mod != U
+ end
+ assert_equal "Ms. U", @c.salutation
+
+ @c.unextend do |mod|
+ mod == U
+ end
+ assert_equal "Ms. A", @c.salutation
end
end
end

0 comments on commit 0dfd363

Please sign in to comment.