Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

define imported methods on singleton classes

  • Loading branch information...
commit 5ec2ceb19029147ab5d7f81f99ca519957177fa1 1 parent c0d6a9b
@ronhopper authored
View
3  README.md
@@ -225,6 +225,9 @@ If you're feeling reckless, you can monkey-patch Jeeves into your project with
History
-------
+### Version 0.2.3
+* Bug fix -- was importing methods into Class, now using singleton classes
+
### Version 0.2.2
* Default to smart dependency resolution - resolve once on first call
* Mock undefined dependency scope within RSpec or Test::Unit
View
8 lib/jeeves/define_imported_method.rb
@@ -20,7 +20,7 @@ def call(target, internal_name, options)
def define_lazy
scope = @scope
external_name = @external_name
- @target.class.send(:define_method, @internal_name) do |*args, &block|
+ @target.singleton_class.send(:define_method, @internal_name) do |*args, &block|
delegator = ResolveDependency.call(scope, external_name)
delegator.call(*args, &block)
end
@@ -28,7 +28,7 @@ def define_lazy
def define_non_lazy
delegator = ResolveDependency.call(@scope, @external_name)
- @target.class.send(:define_method, @internal_name) do |*args, &block|
+ @target.singleton_class.send(:define_method, @internal_name) do |*args, &block|
delegator.call(*args, &block)
end
end
@@ -37,9 +37,9 @@ def define_smart
scope = @scope
internal_name = @internal_name
external_name = @external_name
- @target.class.send(:define_method, @internal_name) do |*args, &block|
+ @target.singleton_class.send(:define_method, @internal_name) do |*args, &block|
delegator = ResolveDependency.call(scope, external_name)
- self.class.send(:define_method, internal_name) do |*args, &block|
+ self.singleton_class.send(:define_method, internal_name) do |*args, &block|
delegator.call(*args, &block)
end
delegator.call(*args, &block)
View
10 spec/integration/import_spec.rb
@@ -26,6 +26,10 @@ def self.call(*args, &block)
end
end
+ class AClass
+ A_CONSTANT = 4326
+ end
+
class TestSubject
extend Jeeves
import :my_method, :my_callable, :my_constant, from: OtherScope
@@ -33,6 +37,7 @@ class TestSubject
import :lazy_method, lazy: true
import :smart_resolver
import [:marco, :say_polo]
+ import :a_constant, from: AClass, lazy: true
end
def self.smart_resolver
@@ -94,6 +99,11 @@ def self.smart_resolver
result.should == "FOO-BAR-BAZ"
end
+ it "defines imported methods in the singleton class" do
+ subject.a_constant.should == 4326
+ # this results in a stack overflow if the methods are defined on Class
+ end
+
it "raises an error if no importers can find the dependency" do
Jeeves.stub(:in_test_framework?) { false } # to avoid RSpec integration
expect do
Please sign in to comment.
Something went wrong with that request. Please try again.