use 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +2 -0
- data/MANIFEST +9 -0
- data/README +54 -0
- data/lib/use.rb +18 -0
- data/test/tc_use.rb +49 -0
- metadata +46 -0
data/CHANGES
ADDED
data/MANIFEST
ADDED
data/README
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
== Description
|
2
|
+
The 'use' package allows you to selectively mixin methods from a given
|
3
|
+
module.
|
4
|
+
|
5
|
+
== Prerequisites
|
6
|
+
Ruby 1.8.0 or later
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
=== Standard
|
10
|
+
ruby test/tc_use.rb (optional)
|
11
|
+
ruby install.rb
|
12
|
+
|
13
|
+
=== Gem install
|
14
|
+
ruby test/tc_use.rb (optional)
|
15
|
+
ruby use.gemspec
|
16
|
+
gem install use.gem
|
17
|
+
|
18
|
+
== Synopsis
|
19
|
+
module Foo
|
20
|
+
def bar
|
21
|
+
"hello"
|
22
|
+
end
|
23
|
+
def baz
|
24
|
+
"world"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Zap
|
29
|
+
use Foo, :bar
|
30
|
+
end
|
31
|
+
|
32
|
+
z = Zap.new
|
33
|
+
|
34
|
+
z.bar # "hello"
|
35
|
+
z.baz # NoMethodError
|
36
|
+
|
37
|
+
== Acknowledgements
|
38
|
+
Thanks go to Ara Howard for providing the solution.
|
39
|
+
|
40
|
+
== Known Bugs
|
41
|
+
None that I'm aware of. If you find any, please log them on the project
|
42
|
+
page at http://www.rubyforge.org/projects/shards.
|
43
|
+
|
44
|
+
== License
|
45
|
+
Ruby's
|
46
|
+
|
47
|
+
== Copyright
|
48
|
+
(C) 2005, Daniel J. Berger
|
49
|
+
All Rights Reserved
|
50
|
+
|
51
|
+
== Author
|
52
|
+
Daniel J. Berger
|
53
|
+
djberg96@yahoo.com
|
54
|
+
IRC nickname: imperator/rubyhacker1
|
data/lib/use.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
class Module
|
2
|
+
# VERSION 1.0.0
|
3
|
+
|
4
|
+
# use(module, methods)
|
5
|
+
#
|
6
|
+
# The 'use' directive allows you to selectively mixin +methods+ from the
|
7
|
+
# given +module+. The +methods+ may be either strings or symbols.
|
8
|
+
def use(mod, *methods)
|
9
|
+
m = Module.new
|
10
|
+
m.module_eval do
|
11
|
+
include mod
|
12
|
+
all_methods = mod.instance_methods
|
13
|
+
unn_methods = all_methods - methods.map{ |method| method.to_s }
|
14
|
+
unn_methods.each{ |method| undef_method(method) }
|
15
|
+
end
|
16
|
+
module_eval{ include m }
|
17
|
+
end
|
18
|
+
end
|
data/test/tc_use.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
###############################################
|
2
|
+
# tc_use.rb
|
3
|
+
#
|
4
|
+
# Test suite for the "use" package.
|
5
|
+
###############################################
|
6
|
+
base = File.basename(Dir.pwd)
|
7
|
+
|
8
|
+
if base == "test" || base =~ /use/
|
9
|
+
Dir.chdir("..") if base == "test"
|
10
|
+
$LOAD_PATH.unshift(Dir.pwd)
|
11
|
+
$LOAD_PATH.unshift(Dir.pwd + "/lib")
|
12
|
+
Dir.chdir("test") rescue nil
|
13
|
+
end
|
14
|
+
|
15
|
+
require "test/unit"
|
16
|
+
require "use"
|
17
|
+
|
18
|
+
module Foo
|
19
|
+
def bar; self; end
|
20
|
+
def baz; self; end
|
21
|
+
end
|
22
|
+
|
23
|
+
class User
|
24
|
+
use Foo, :bar
|
25
|
+
end
|
26
|
+
|
27
|
+
class TC_Use < Test::Unit::TestCase
|
28
|
+
def setup
|
29
|
+
@user = User.new
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_mod_methods
|
33
|
+
assert_equal(["baz","bar"], Foo.instance_methods)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_bar_legal
|
37
|
+
assert_respond_to(@user, :bar)
|
38
|
+
assert_nothing_raised{ @user.bar }
|
39
|
+
assert_kind_of(User, @user.bar)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_baz_not_legal
|
43
|
+
assert_raises(NoMethodError){ @user.baz }
|
44
|
+
end
|
45
|
+
|
46
|
+
def teardown
|
47
|
+
@user = nil
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.6
|
3
|
+
specification_version: 1
|
4
|
+
name: use
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2005-08-01
|
8
|
+
summary: Selectively mixin methods from a given module
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: djberg96@gmail.com
|
12
|
+
homepage: http://www.rubyforge.org/projects/shards
|
13
|
+
rubyforge_project:
|
14
|
+
description: Selectively mixin methods from a given module
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
authors:
|
28
|
+
- Daniel J. Berger
|
29
|
+
files:
|
30
|
+
- lib/use.rb
|
31
|
+
- CHANGES
|
32
|
+
- CVS
|
33
|
+
- MANIFEST
|
34
|
+
- README
|
35
|
+
- test/CVS
|
36
|
+
- test/tc_use.rb
|
37
|
+
test_files:
|
38
|
+
- test/tc_use.rb
|
39
|
+
rdoc_options: []
|
40
|
+
extra_rdoc_files:
|
41
|
+
- README
|
42
|
+
- CHANGES
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
requirements: []
|
46
|
+
dependencies: []
|