This repository has been archived by the owner on Aug 15, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added shoulda tests, basic test for comments, etc.
- Loading branch information
Josh Adams
committed
Apr 20, 2009
1 parent
c415593
commit 5bd3c74
Showing
22 changed files
with
627 additions
and
1,230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Hijacker class | ||
# | ||
# This class is used by RoleRequirementTestHelper to temporarily hijack a controller action for testing | ||
# | ||
# Example usage: | ||
# hijacker = Hijacker.new(ListingsController) | ||
# hijacker.hijack_instance_method("index", "render :text => 'hello world!'" ) | ||
# get :index # will return "hello world" | ||
# hijacker.restore # put things back the way you found it | ||
|
||
class Hijacker | ||
def initialize(klass) | ||
@target_klass = klass | ||
@method_stores = {} | ||
end | ||
|
||
def hijack_class_method(method_name, eval_string = nil, arg_names = [], &block) | ||
hijack_method(class_self_instance, method_name, eval_string, arg_names, &block ) | ||
end | ||
|
||
def hijack_instance_method(method_name, eval_string = nil, arg_names = [], &block) | ||
hijack_method(@target_klass, method_name, eval_string, arg_names, &block ) | ||
end | ||
|
||
# restore all | ||
def restore | ||
@method_stores.each_pair{|klass, method_stores| | ||
method_stores.reverse_each{ |method_name, method| | ||
klass.send :undef_method, method_name | ||
klass.send :define_method, method_name, method if method | ||
} | ||
} | ||
@method_stores.clear | ||
true | ||
rescue | ||
false | ||
end | ||
|
||
protected | ||
|
||
def class_self_instance | ||
@target_klass.send :eval, "class << self; self; end;" | ||
end | ||
|
||
def hijack_method(klass, method_name, eval_string = nil, arg_names = [], &block) | ||
method_name = method_name.to_s | ||
# You have got love ruby! What other language allows you to pillage and plunder a class like this? | ||
|
||
(@method_stores[klass]||=[]) << [ | ||
method_name, | ||
klass.instance_methods.include?(method_name) && klass.instance_method(method_name) | ||
] | ||
|
||
klass.send :undef_method, method_name | ||
if Symbol === eval_string | ||
klass.send :define_method, method_name, klass.instance_methods(eval_string) | ||
elsif String === eval_string | ||
klass.class_eval <<-EOF | ||
def #{method_name}(#{arg_names * ','}) | ||
#{eval_string} | ||
end | ||
EOF | ||
elsif block_given? | ||
klass.send :define_method, method_name, block | ||
end | ||
|
||
true | ||
rescue | ||
false | ||
end | ||
|
||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.