-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 801bf5c
Showing
5 changed files
with
135 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
= Summary | ||
|
||
Allows using the 'super' keyword in methods of reopened classes. | ||
Simple and elegant alternative to alias_method_chain for redifining methods while keeping access to the previous definition. | ||
|
||
= Examples | ||
|
||
class Post | ||
def text | ||
@text.strip | ||
end | ||
end | ||
|
||
class Post | ||
override :text | ||
def text | ||
super.reverse | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'rake/gempackagetask' | ||
require 'pathname' | ||
require 'yaml' | ||
|
||
def gem | ||
RUBY_1_9 ? 'gem19' : 'gem' | ||
end | ||
|
||
def all_except(paths) | ||
Dir['**/*'] - paths.map {|path| path.strip.gsub(/^\//,'').gsub(/\/$/,'') } | ||
end | ||
|
||
spec = Gem::Specification.new do |s| | ||
s.name = 'override' | ||
s.version = '0.5.0' | ||
s.summary = "Allows using 'super' in methods of reopened classes." | ||
s.description = "Provides a simple and elegant alternative to alias_method_chain for redifining methods, while keeping access to the previous defenition." | ||
s.author = "Martin Aumont" | ||
s.email = 'mynyml@gmail.com' | ||
s.homepage = '' | ||
s.has_rdoc = true | ||
s.require_path = "lib" | ||
s.files = Dir['**/*'] | ||
end | ||
|
||
Rake::GemPackageTask.new(spec) do |p| | ||
p.gem_spec = spec | ||
end | ||
|
||
|
||
desc "Remove package products" | ||
task :clean => :clobber_package | ||
|
||
desc "Update the gemspec for GitHub's gem server" | ||
task :gemspec do | ||
Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) } | ||
end | ||
|
||
desc "Install gem" | ||
task :install => [:clobber, :package] do | ||
sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem" | ||
end | ||
|
||
desc "Uninstall gem" | ||
task :uninstall => :clean do | ||
sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#original idea: http://github.com/wycats/merb/blob/677d08b9dd972367369a509987264ded25e70f9a/merb-core/lib/merb-core/core_ext/class.rb | ||
class Class | ||
def override(*names) | ||
mod = Module.new | ||
names.each do |name| | ||
method = self.instance_method(name.to_sym) | ||
mod.module_eval { send(:define_method, name.to_sym, method) } | ||
include mod | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#require 'pathname' | ||
require 'test/unit' | ||
require 'rubygems' | ||
require 'context' | ||
require 'matchy' | ||
#require 'zebra' | ||
#require 'mocha' | ||
begin | ||
require 'ruby-debug' | ||
require 'quietbacktrace' | ||
rescue LoadError, RuntimeError | ||
# pass | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'pathname' | ||
root = Pathname(__FILE__).dirname.parent | ||
require root.join('test/test_helper') | ||
require root.join('lib/override') | ||
|
||
class OverrideTest < Test::Unit::TestCase | ||
context "Override" do | ||
context "in reopened class" do | ||
before do | ||
class ::A | ||
def a() 'canada' end | ||
define_method(:b) { 'brazil' } | ||
end | ||
end | ||
after do | ||
Object.send(:remove_const, :A) | ||
lambda { ::A }.should raise_error(NameError) #make sure ::A is unset | ||
end | ||
test "allows using 'super' keyword" do | ||
class ::A | ||
override(:a) | ||
def a() super + '&montreal' end | ||
end | ||
A.new.a.should be('canada&montreal') | ||
end | ||
test "works with dynamically defined methods" do | ||
class ::A | ||
override(:b) | ||
def b() super + '&saopaulo' end | ||
end | ||
A.new.b.should be('brazil&saopaulo') | ||
end | ||
test "accepts multiple method names" do | ||
class ::A | ||
override(:a,:b) | ||
def a() super + '&montreal' end | ||
def b() super + '&saopaulo' end | ||
end | ||
A.new.a.should be('canada&montreal') | ||
A.new.b.should be('brazil&saopaulo') | ||
end | ||
end | ||
end | ||
end |