Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced Class#class_method_proxy in order to make available annota…
…ted class methods as private methods
- Loading branch information
Luca Guidi
committed
May 22, 2008
1 parent
f850c1b
commit ae283df
Showing
6 changed files
with
77 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Dir[File.dirname(__FILE__) + '/core_ext/*.rb'].each{|f| require f} |
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,31 @@ | ||
class Class | ||
# Dinamically creates a proxy for given class methods. | ||
# | ||
# Example: | ||
# | ||
# class Repository | ||
# def self.path | ||
# @@path | ||
# end | ||
# | ||
# class_method_proxy :path | ||
# end | ||
# | ||
# It produces: | ||
# # Proxy method for <tt>Repository#path</tt> | ||
# def path | ||
# self.class.path | ||
# end | ||
# private :path | ||
def class_method_proxy(*method_names) | ||
method_names.each do |m| | ||
self.class_eval %{ | ||
# Proxy method for <tt>#{self.class.name}##{m}</tt> | ||
def #{m}(*args) | ||
self.class.#{m}(*args) | ||
end | ||
private :#{m} | ||
}, __FILE__, __LINE__ | ||
end | ||
end | ||
end |
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,22 @@ | ||
require 'test/unit' | ||
require 'test/test_helper' | ||
|
||
class Repository | ||
def self.path | ||
@@path ||= 'path' | ||
end | ||
|
||
def self.another_path(another_path) | ||
another_path | ||
end | ||
|
||
class_method_proxy :path, :another_path | ||
end | ||
|
||
class ClassTest < Test::Unit::TestCase | ||
def test_should_respond_to_proxied_methods | ||
repository = Repository.new | ||
assert repository.send(:path) | ||
assert repository.send(:another_path, 'another_path') | ||
end | ||
end |