mynyml-override 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +19 -0
- data/Rakefile +48 -0
- data/lib/override.rb +11 -0
- data/test/test_helper.rb +13 -0
- data/test/test_override.rb +44 -0
- metadata +59 -0
data/README
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= Summary
|
2
|
+
|
3
|
+
Allows using the 'super' keyword in methods of reopened classes.
|
4
|
+
Simple and elegant alternative to alias_method_chain for redifining methods while keeping access to the previous definition.
|
5
|
+
|
6
|
+
= Examples
|
7
|
+
|
8
|
+
class Post
|
9
|
+
def text
|
10
|
+
@text.strip
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Post
|
15
|
+
override :text
|
16
|
+
def text
|
17
|
+
super.reverse
|
18
|
+
end
|
19
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rake/gempackagetask'
|
2
|
+
require 'pathname'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
def gem
|
6
|
+
RUBY_1_9 ? 'gem19' : 'gem'
|
7
|
+
end
|
8
|
+
|
9
|
+
def all_except(paths)
|
10
|
+
Dir['**/*'] - paths.map {|path| path.strip.gsub(/^\//,'').gsub(/\/$/,'') }
|
11
|
+
end
|
12
|
+
|
13
|
+
spec = Gem::Specification.new do |s|
|
14
|
+
s.name = 'override'
|
15
|
+
s.version = '0.5.0'
|
16
|
+
s.summary = "Ruby lib that allows using 'super' in methods of reopened classes."
|
17
|
+
s.description = "Provides a simple and elegant alternative to alias_method_chain for redifining methods, while keeping access to the previous defenition."
|
18
|
+
s.author = "Martin Aumont"
|
19
|
+
s.email = 'mynyml@gmail.com'
|
20
|
+
s.homepage = ''
|
21
|
+
s.has_rdoc = true
|
22
|
+
s.require_path = "lib"
|
23
|
+
s.files = Dir['**/*']
|
24
|
+
end
|
25
|
+
|
26
|
+
Rake::GemPackageTask.new(spec) do |p|
|
27
|
+
p.gem_spec = spec
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
desc "Remove package products"
|
32
|
+
task :clean => :clobber_package
|
33
|
+
|
34
|
+
desc "Update the gemspec for GitHub's gem server"
|
35
|
+
task :gemspec do
|
36
|
+
Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) }
|
37
|
+
end
|
38
|
+
|
39
|
+
desc "Install gem"
|
40
|
+
task :install => [:clobber, :package] do
|
41
|
+
sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem"
|
42
|
+
end
|
43
|
+
|
44
|
+
desc "Uninstall gem"
|
45
|
+
task :uninstall => :clean do
|
46
|
+
sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}"
|
47
|
+
end
|
48
|
+
|
data/lib/override.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#original idea: http://github.com/wycats/merb/blob/677d08b9dd972367369a509987264ded25e70f9a/merb-core/lib/merb-core/core_ext/class.rb
|
2
|
+
class Class
|
3
|
+
def override(*names)
|
4
|
+
mod = Module.new
|
5
|
+
names.each do |name|
|
6
|
+
method = self.instance_method(name.to_sym)
|
7
|
+
mod.module_eval { send(:define_method, name.to_sym, method) }
|
8
|
+
include mod
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
root = Pathname(__FILE__).dirname.parent
|
3
|
+
require root.join('test/test_helper')
|
4
|
+
require root.join('lib/override')
|
5
|
+
|
6
|
+
class OverrideTest < Test::Unit::TestCase
|
7
|
+
context "Override" do
|
8
|
+
context "in reopened class" do
|
9
|
+
before do
|
10
|
+
class ::A
|
11
|
+
def a() 'canada' end
|
12
|
+
define_method(:b) { 'brazil' }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
after do
|
16
|
+
Object.send(:remove_const, :A)
|
17
|
+
lambda { ::A }.should raise_error(NameError) #make sure ::A is unset
|
18
|
+
end
|
19
|
+
test "allows using 'super' keyword" do
|
20
|
+
class ::A
|
21
|
+
override(:a)
|
22
|
+
def a() super + '&montreal' end
|
23
|
+
end
|
24
|
+
A.new.a.should be('canada&montreal')
|
25
|
+
end
|
26
|
+
test "works with dynamically defined methods" do
|
27
|
+
class ::A
|
28
|
+
override(:b)
|
29
|
+
def b() super + '&saopaulo' end
|
30
|
+
end
|
31
|
+
A.new.b.should be('brazil&saopaulo')
|
32
|
+
end
|
33
|
+
test "accepts multiple method names" do
|
34
|
+
class ::A
|
35
|
+
override(:a,:b)
|
36
|
+
def a() super + '&montreal' end
|
37
|
+
def b() super + '&saopaulo' end
|
38
|
+
end
|
39
|
+
A.new.a.should be('canada&montreal')
|
40
|
+
A.new.b.should be('brazil&saopaulo')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mynyml-override
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Aumont
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-14 21:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Provides a simple and elegant alternative to alias_method_chain for redifining methods, while keeping access to the previous defenition.
|
17
|
+
email: mynyml@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib
|
26
|
+
- lib/override.rb
|
27
|
+
- README
|
28
|
+
- Rakefile
|
29
|
+
- test
|
30
|
+
- test/test_helper.rb
|
31
|
+
- test/test_override.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: ""
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.2.0
|
55
|
+
signing_key:
|
56
|
+
specification_version: 2
|
57
|
+
summary: Ruby lib that allows using 'super' in methods of reopened classes.
|
58
|
+
test_files: []
|
59
|
+
|