module_functions 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +5 -0
- data/Gemfile.lock +13 -0
- data/README +27 -0
- data/lib/module_functions.rb +3 -0
- data/lib/module_functions/module_functions.rb +64 -0
- data/module_functions.gemspec +27 -0
- data/rakefile +2 -0
- data/test/lib/empty_module.rb +37 -0
- data/test/lib/my_class.rb +55 -0
- data/test/lib/my_module.rb +68 -0
- data/test/module_functions_test.rb +92 -0
- data/test/require.rb +9 -0
- metadata +97 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
== Welcome to ModuleFunctions
|
2
|
+
|
3
|
+
ModuleFunctions contains useful module functions.
|
4
|
+
One example is the function that adds
|
5
|
+
all public module functions to another module.
|
6
|
+
|
7
|
+
== Getting Started
|
8
|
+
|
9
|
+
1. Install ModuleFunctions at the command prompt if you haven't yet:
|
10
|
+
|
11
|
+
gem install module_functions
|
12
|
+
|
13
|
+
2. Require the gem in your Gemfile:
|
14
|
+
|
15
|
+
gem 'module_functions', '~> 0.0.1'
|
16
|
+
|
17
|
+
3. Require the gem wherever you need to use it:
|
18
|
+
|
19
|
+
require 'module_functions'
|
20
|
+
|
21
|
+
== Additional Documentation
|
22
|
+
|
23
|
+
rake rdoc:app
|
24
|
+
|
25
|
+
== License
|
26
|
+
|
27
|
+
ModuleFunctions is released under the GPLv3 license.
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# This file contains useful module functions.
|
2
|
+
|
3
|
+
#--
|
4
|
+
################################################################################
|
5
|
+
# Copyright (C) 2011 Travis Herrick #
|
6
|
+
################################################################################
|
7
|
+
# #
|
8
|
+
# \v^V,^!v\^/ #
|
9
|
+
# ~% %~ #
|
10
|
+
# { _ _ } #
|
11
|
+
# ( * - ) #
|
12
|
+
# | / | #
|
13
|
+
# \ _, / #
|
14
|
+
# \__.__/ #
|
15
|
+
# #
|
16
|
+
################################################################################
|
17
|
+
# This program is free software: you can redistribute it #
|
18
|
+
# and/or modify it under the terms of the GNU General Public License #
|
19
|
+
# as published by the Free Software Foundation, #
|
20
|
+
# either version 3 of the License, or (at your option) any later version. #
|
21
|
+
# #
|
22
|
+
# Commercial licensing may be available for a fee under a different license. #
|
23
|
+
################################################################################
|
24
|
+
# This program is distributed in the hope that it will be useful, #
|
25
|
+
# but WITHOUT ANY WARRANTY; #
|
26
|
+
# without even the implied warranty of MERCHANTABILITY #
|
27
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. #
|
28
|
+
# See the GNU General Public License for more details. #
|
29
|
+
# #
|
30
|
+
# You should have received a copy of the GNU General Public License #
|
31
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
32
|
+
################################################################################
|
33
|
+
#++
|
34
|
+
|
35
|
+
# Useful functions for modules.
|
36
|
+
module ModuleFunctions
|
37
|
+
class << self
|
38
|
+
# Imports public methods from one module into another.
|
39
|
+
# ==== Input
|
40
|
+
# [receiver : Module] This module will have new methods added to it.
|
41
|
+
# [sender : Module] This module will have it's methods
|
42
|
+
# called from <tt>receiver</tt>.
|
43
|
+
def import_public_methods(receiver, sender)
|
44
|
+
unless receiver.class == Module && sender.class == Module
|
45
|
+
raise ArgumentError, "Invalid parameter for #{__method__}. " +
|
46
|
+
"A #{Module} was expected, but was a #{receiver.class}."
|
47
|
+
end
|
48
|
+
|
49
|
+
# Create a public methods of the same name in the receiving module
|
50
|
+
# that will call the original module's methods.
|
51
|
+
(sender.public_methods - sender.class.public_methods).each do |method|
|
52
|
+
receiver.module_eval(<<-EOT, __FILE__, __LINE__)
|
53
|
+
def self.#{method}(*args)
|
54
|
+
if args.length == 0
|
55
|
+
#{sender.name}.#{method}
|
56
|
+
else
|
57
|
+
#{sender.name}.#{method}(*args)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
EOT
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'module_functions'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
|
5
|
+
s.summary = 'This gem contains useful module functions.'
|
6
|
+
s.description = %Q{
|
7
|
+
ModuleFunctions contains useful module functions.
|
8
|
+
One example is the function that adds
|
9
|
+
all public module functions to another module.
|
10
|
+
}.strip
|
11
|
+
|
12
|
+
s.author = 'Travis Herrick'
|
13
|
+
s.email = 'tthetoad@gmail.com'
|
14
|
+
s.homepage = 'http://www.bitbucket.org/ToadJamb/gems_module_functions'
|
15
|
+
|
16
|
+
s.license = 'GPLv3'
|
17
|
+
|
18
|
+
s.extra_rdoc_files << 'README'
|
19
|
+
|
20
|
+
s.require_paths = ['lib']
|
21
|
+
s.files = Dir['lib/**/*.rb', '*']
|
22
|
+
s.test_files = Dir['test/**/*.rb']
|
23
|
+
|
24
|
+
s.add_development_dependency 'rake_tasks', '~> 0.0.1'
|
25
|
+
|
26
|
+
s.has_rdoc = true
|
27
|
+
end
|
data/rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# This file contains a module that is used to assist in testing this gem.
|
2
|
+
|
3
|
+
#--
|
4
|
+
################################################################################
|
5
|
+
# Copyright (C) 2011 Travis Herrick #
|
6
|
+
################################################################################
|
7
|
+
# #
|
8
|
+
# \v^V,^!v\^/ #
|
9
|
+
# ~% %~ #
|
10
|
+
# { _ _ } #
|
11
|
+
# ( * - ) #
|
12
|
+
# | / | #
|
13
|
+
# \ _, / #
|
14
|
+
# \__.__/ #
|
15
|
+
# #
|
16
|
+
################################################################################
|
17
|
+
# This program is free software: you can redistribute it #
|
18
|
+
# and/or modify it under the terms of the GNU General Public License #
|
19
|
+
# as published by the Free Software Foundation, #
|
20
|
+
# either version 3 of the License, or (at your option) any later version. #
|
21
|
+
# #
|
22
|
+
# Commercial licensing may be available for a fee under a different license. #
|
23
|
+
################################################################################
|
24
|
+
# This program is distributed in the hope that it will be useful, #
|
25
|
+
# but WITHOUT ANY WARRANTY; #
|
26
|
+
# without even the implied warranty of MERCHANTABILITY #
|
27
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. #
|
28
|
+
# See the GNU General Public License for more details. #
|
29
|
+
# #
|
30
|
+
# You should have received a copy of the GNU General Public License #
|
31
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
32
|
+
################################################################################
|
33
|
+
#++
|
34
|
+
|
35
|
+
# This module is used to assist in testing this gem.
|
36
|
+
module EmptyModule
|
37
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# This file contains a class that is used to assist in testing this gem.
|
2
|
+
|
3
|
+
#--
|
4
|
+
################################################################################
|
5
|
+
# Copyright (C) 2011 Travis Herrick #
|
6
|
+
################################################################################
|
7
|
+
# #
|
8
|
+
# \v^V,^!v\^/ #
|
9
|
+
# ~% %~ #
|
10
|
+
# { _ _ } #
|
11
|
+
# ( * - ) #
|
12
|
+
# | / | #
|
13
|
+
# \ _, / #
|
14
|
+
# \__.__/ #
|
15
|
+
# #
|
16
|
+
################################################################################
|
17
|
+
# This program is free software: you can redistribute it #
|
18
|
+
# and/or modify it under the terms of the GNU General Public License #
|
19
|
+
# as published by the Free Software Foundation, #
|
20
|
+
# either version 3 of the License, or (at your option) any later version. #
|
21
|
+
# #
|
22
|
+
# Commercial licensing may be available for a fee under a different license. #
|
23
|
+
################################################################################
|
24
|
+
# This program is distributed in the hope that it will be useful, #
|
25
|
+
# but WITHOUT ANY WARRANTY; #
|
26
|
+
# without even the implied warranty of MERCHANTABILITY #
|
27
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. #
|
28
|
+
# See the GNU General Public License for more details. #
|
29
|
+
# #
|
30
|
+
# You should have received a copy of the GNU General Public License #
|
31
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
32
|
+
################################################################################
|
33
|
+
#++
|
34
|
+
|
35
|
+
# This class is used to assist in testing this gem.
|
36
|
+
class MyClass
|
37
|
+
class << self
|
38
|
+
def my_public_class_method; end
|
39
|
+
|
40
|
+
protected
|
41
|
+
def my_protected_class_method; end
|
42
|
+
|
43
|
+
private
|
44
|
+
def my_private_class_method; end
|
45
|
+
end
|
46
|
+
|
47
|
+
public
|
48
|
+
def my_public_instance_method; end
|
49
|
+
|
50
|
+
protected
|
51
|
+
def my_protected_instance_method; end
|
52
|
+
|
53
|
+
private
|
54
|
+
def my_private_instance_method; end
|
55
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# This file contains a module that is used to assist in testing this gem.
|
2
|
+
|
3
|
+
#--
|
4
|
+
################################################################################
|
5
|
+
# Copyright (C) 2011 Travis Herrick #
|
6
|
+
################################################################################
|
7
|
+
# #
|
8
|
+
# \v^V,^!v\^/ #
|
9
|
+
# ~% %~ #
|
10
|
+
# { _ _ } #
|
11
|
+
# ( * - ) #
|
12
|
+
# | / | #
|
13
|
+
# \ _, / #
|
14
|
+
# \__.__/ #
|
15
|
+
# #
|
16
|
+
################################################################################
|
17
|
+
# This program is free software: you can redistribute it #
|
18
|
+
# and/or modify it under the terms of the GNU General Public License #
|
19
|
+
# as published by the Free Software Foundation, #
|
20
|
+
# either version 3 of the License, or (at your option) any later version. #
|
21
|
+
# #
|
22
|
+
# Commercial licensing may be available for a fee under a different license. #
|
23
|
+
################################################################################
|
24
|
+
# This program is distributed in the hope that it will be useful, #
|
25
|
+
# but WITHOUT ANY WARRANTY; #
|
26
|
+
# without even the implied warranty of MERCHANTABILITY #
|
27
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. #
|
28
|
+
# See the GNU General Public License for more details. #
|
29
|
+
# #
|
30
|
+
# You should have received a copy of the GNU General Public License #
|
31
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
32
|
+
################################################################################
|
33
|
+
#++
|
34
|
+
|
35
|
+
# This module is used to assist in testing this gem.
|
36
|
+
module MyModule
|
37
|
+
class << self
|
38
|
+
def my_public_module_method; end
|
39
|
+
|
40
|
+
protected
|
41
|
+
def my_protected_module_method; end
|
42
|
+
|
43
|
+
private
|
44
|
+
def my_private_module_method; end
|
45
|
+
end
|
46
|
+
|
47
|
+
public
|
48
|
+
def my_public_instance_method; end
|
49
|
+
|
50
|
+
protected
|
51
|
+
def my_protected_instance_method; end
|
52
|
+
|
53
|
+
private
|
54
|
+
def my_private_instance_method; end
|
55
|
+
|
56
|
+
module_function
|
57
|
+
|
58
|
+
def my_default_module_function; end
|
59
|
+
|
60
|
+
def my_public_module_function; end
|
61
|
+
public :my_public_module_function
|
62
|
+
|
63
|
+
def my_protected_module_function; end
|
64
|
+
protected :my_protected_module_function
|
65
|
+
|
66
|
+
def my_private_module_function; end
|
67
|
+
private :my_private_module_function
|
68
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
#--
|
2
|
+
################################################################################
|
3
|
+
# Copyright (C) 2011 Travis Herrick #
|
4
|
+
################################################################################
|
5
|
+
# #
|
6
|
+
# \v^V,^!v\^/ #
|
7
|
+
# ~% %~ #
|
8
|
+
# { _ _ } #
|
9
|
+
# ( * - ) #
|
10
|
+
# | / | #
|
11
|
+
# \ _, / #
|
12
|
+
# \__.__/ #
|
13
|
+
# #
|
14
|
+
################################################################################
|
15
|
+
# This program is free software: you can redistribute it #
|
16
|
+
# and/or modify it under the terms of the GNU General Public License #
|
17
|
+
# as published by the Free Software Foundation, #
|
18
|
+
# either version 3 of the License, or (at your option) any later version. #
|
19
|
+
# #
|
20
|
+
# Commercial licensing may be available for a fee under a different license. #
|
21
|
+
################################################################################
|
22
|
+
# This program is distributed in the hope that it will be useful, #
|
23
|
+
# but WITHOUT ANY WARRANTY; #
|
24
|
+
# without even the implied warranty of MERCHANTABILITY #
|
25
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. #
|
26
|
+
# See the GNU General Public License for more details. #
|
27
|
+
# #
|
28
|
+
# You should have received a copy of the GNU General Public License #
|
29
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
30
|
+
################################################################################
|
31
|
+
#++
|
32
|
+
|
33
|
+
require_relative 'require'
|
34
|
+
|
35
|
+
class ModuleFunctionsTest < Test::Unit::TestCase
|
36
|
+
ITEMS = {
|
37
|
+
:public_methods => [
|
38
|
+
:my_public_module_method,
|
39
|
+
:my_default_module_function,
|
40
|
+
:my_public_module_function,
|
41
|
+
:my_protected_module_function,
|
42
|
+
:my_private_module_function,
|
43
|
+
],
|
44
|
+
:non_public_methods => [
|
45
|
+
:my_protected_module_method,
|
46
|
+
:my_private_module_method,
|
47
|
+
],
|
48
|
+
}
|
49
|
+
|
50
|
+
def setup
|
51
|
+
super
|
52
|
+
@class = MyClass
|
53
|
+
@module = ModuleFunctions
|
54
|
+
@my_module = MyModule
|
55
|
+
@empty = EmptyModule
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_import_public_methods
|
59
|
+
assert_nothing_raised {
|
60
|
+
@module.import_public_methods(@empty, @my_module) }
|
61
|
+
assert_responses @empty, ITEMS[:public_methods]
|
62
|
+
assert_no_responses @empty, ITEMS[:non_public_methods]
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_import_into_class_should_fail
|
66
|
+
assert_raise(ArgumentError) {
|
67
|
+
@module.import_public_methods(@class, @my_module) }
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_import_from_class_should_fail
|
71
|
+
assert_raise(ArgumentError) {
|
72
|
+
@module.import_public_methods(@my_module, @class) }
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
############################################################################
|
77
|
+
private
|
78
|
+
############################################################################
|
79
|
+
|
80
|
+
def assert_responses(object, methods)
|
81
|
+
methods.each do |method|
|
82
|
+
assert_respond_to object, method
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def assert_no_responses(object, methods)
|
87
|
+
methods.each do |method|
|
88
|
+
assert !object.respond_to?(method),
|
89
|
+
"#{object.class} was not expected to respond to #{method}."
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/test/require.rb
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: module_functions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Travis Herrick
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-08-15 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rake_tasks
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 0
|
30
|
+
- 1
|
31
|
+
version: 0.0.1
|
32
|
+
type: :development
|
33
|
+
prerelease: false
|
34
|
+
version_requirements: *id001
|
35
|
+
description: |-
|
36
|
+
ModuleFunctions contains useful module functions.
|
37
|
+
One example is the function that adds
|
38
|
+
all public module functions to another module.
|
39
|
+
email: tthetoad@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files:
|
45
|
+
- README
|
46
|
+
files:
|
47
|
+
- lib/module_functions/module_functions.rb
|
48
|
+
- lib/module_functions.rb
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- module_functions.gemspec
|
52
|
+
- rakefile
|
53
|
+
- README
|
54
|
+
- test/require.rb
|
55
|
+
- test/module_functions_test.rb
|
56
|
+
- test/lib/my_module.rb
|
57
|
+
- test/lib/empty_module.rb
|
58
|
+
- test/lib/my_class.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://www.bitbucket.org/ToadJamb/gems_module_functions
|
61
|
+
licenses:
|
62
|
+
- GPLv3
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: -3178384406756779379
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.3.7
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: This gem contains useful module functions.
|
92
|
+
test_files:
|
93
|
+
- test/require.rb
|
94
|
+
- test/module_functions_test.rb
|
95
|
+
- test/lib/my_module.rb
|
96
|
+
- test/lib/empty_module.rb
|
97
|
+
- test/lib/my_class.rb
|