Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Maintain contextual methods after a method redefinition

  • Loading branch information...
commit 2f6f20d7d721f72ee8a2f0def43b9cdd414d6e7e 1 parent 46d290f
@patmaddox authored
Showing with 22 additions and 4 deletions.
  1. +15 −4 lib/context.rb
  2. +7 −0 spec/context_spec.rb
View
19 lib/context.rb
@@ -21,19 +21,26 @@ def in_context(name, &block)
m = module_for_context(name) || Module.new
m.module_eval &block
m.instance_methods.each do |meth|
- if instance_methods.include?(meth)
- default_context_methods << instance_method(meth)
- remove_method meth
- end
+ add_to_default_context meth if instance_methods.include?(meth)
+ contextual_methods << meth
end
context_modules[name] = m
name
end
+ def method_added(meth)
+ add_to_default_context(meth) if contextual_methods.include?(meth.to_s)
+ end
+
def module_for_context(context)
context && context_modules[context]
end
+ def add_to_default_context(meth)
+ default_context_methods << instance_method(meth.to_s)
+ remove_method meth
+ end
+
def context_modules
@context_modules ||= { }
end
@@ -41,6 +48,10 @@ def context_modules
def default_context_methods
@default_context_methods ||= []
end
+
+ def contextual_methods
+ @contextual_methods ||= []
+ end
end
def self.included(klass)
View
7 spec/context_spec.rb
@@ -35,6 +35,13 @@ def called?; @called; end
lambda { @object.call }.should raise_error(NoMethodError)
end
+ it "should call the method in context after the instance method is redefined" do
+ @klass.send(:define_method, :call) { :noop }
+ with_context(:callable) { @object.call }
+ @object.should be_called
+ lambda { @object.call.should == :noop }
+ end
+
describe "when the context is opened again" do
it "should redefine the method" do
@klass.in_context(:callable) { def call; :redefined; end }
Please sign in to comment.
Something went wrong with that request. Please try again.