Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Being able to tackle 'BlankObject instances' when extending:

A BlankObject class is considered not to have meta_class and unextend defined, but does respond to extend (e.g. Liquid::Strainer)
See also: http://rubydoc.info/gems/liquid/2.3.0/Liquid/Strainer
  • Loading branch information...
commit 748410009bff54cab765a7410131b21e458cb97a 1 parent a0706ca
Paul Engel authored
Showing with 65 additions and 9 deletions.
  1. +13 −9 lib/unextendable/object.rb
  2. +52 −0 test/blank_object_test.rb
View
22 lib/unextendable/object.rb
@@ -12,20 +12,24 @@ def method_procs
def extend(*modules)
modules.each do |mod|
- wrap_unextendable_module mod if mod.unextendable?
- add_extended_module mod
+ if respond_to?(:meta_class)
+ wrap_unextendable_module mod if mod.unextendable?
+ add_extended_module mod
+ end
super(mod)
end
end
def unextend(*modules, &block)
- if modules.empty?
- meta_class.extended_modules.delete_if do |mod|
- unextend? mod, &block
- end
- else
- modules.each do |mod|
- meta_class.extended_modules.delete mod if unextend? mod, &block
+ if respond_to?(:meta_class)
+ if modules.empty?
+ meta_class.extended_modules.delete_if do |mod|
+ unextend? mod, &block
+ end
+ else
+ modules.each do |mod|
+ meta_class.extended_modules.delete mod if unextend? mod, &block
+ end
end
end
end
View
52 test/blank_object_test.rb
@@ -0,0 +1,52 @@
+require File.expand_path("../test_helper", __FILE__)
+
+class BlankObjectTest < Test::Unit::TestCase
+
+ # ==================================================================================================================================
+ # A BlankObject class is considered not to have meta_class and unextend defined, but does respond to extend (e.g. Liquid::Strainer)
+ # See also: http://rubydoc.info/gems/liquid/2.3.0/Liquid/Strainer
+ # ==================================================================================================================================
+
+ context "A blank object instance" do
+ setup do
+ module Hi
+ unextendable
+ def say_hi
+ "Hi!"
+ end
+ end
+ class BlankObject
+ def meta_class
+ raise NoMethodError
+ end
+ def unextend
+ raise NoMethodError
+ end
+ def respond_to?(symbol, include_private = false)
+ return false if %w(meta_class unextend).include? symbol.to_s
+ super
+ end
+ end
+ end
+
+ should "have extend defined" do
+ assert BlankObject.new.extend(Hi)
+ end
+
+ should "not have meta_class and unextend defined" do
+ assert_raises NoMethodError do
+ BlankObject.new.meta_class
+ end
+ assert_raises NoMethodError do
+ BlankObject.new.unextend
+ end
+ end
+
+ should "be extendable" do
+ b = BlankObject.new
+ b.extend Hi
+ assert_equal "Hi!", b.say_hi
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.