Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mynyml committed Mar 15, 2009
0 parents commit 801bf5c
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README
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
48 changes: 48 additions & 0 deletions Rakefile
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

11 changes: 11 additions & 0 deletions lib/override.rb
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
13 changes: 13 additions & 0 deletions test/test_helper.rb
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
44 changes: 44 additions & 0 deletions test/test_override.rb
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

0 comments on commit 801bf5c

Please sign in to comment.