module_creation_helper 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/MIT-LICENSE +2 -2
- data/README +7 -11
- data/Rakefile +5 -5
- data/lib/module_creation_helper.rb +53 -57
- data/test/module_creation_helper_test.rb +139 -75
- metadata +42 -35
data/CHANGELOG
CHANGED
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2006-
|
1
|
+
Copyright (c) 2006-2008 Aaron Pfeifer
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
17
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
18
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
19
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
CHANGED
@@ -5,25 +5,21 @@ at runtime.
|
|
5
5
|
|
6
6
|
== Resources
|
7
7
|
|
8
|
-
API
|
9
|
-
|
10
|
-
* http://api.pluginaweek.org/module_creation_helper
|
11
|
-
|
12
8
|
Wiki
|
13
9
|
|
14
10
|
* http://wiki.pluginaweek.org/Module_creation_helper
|
15
11
|
|
16
|
-
|
12
|
+
API
|
17
13
|
|
18
|
-
* http://
|
14
|
+
* http://api.pluginaweek.org/module_creation_helper
|
19
15
|
|
20
|
-
|
16
|
+
Development
|
21
17
|
|
22
|
-
* http://
|
18
|
+
* http://dev.pluginaweek.org/browser/trunk/module_creation_helper
|
23
19
|
|
24
|
-
|
20
|
+
Source
|
25
21
|
|
26
|
-
* http://
|
22
|
+
* http://svn.pluginaweek.org/trunk/module_creation_helper
|
27
23
|
|
28
24
|
== Description
|
29
25
|
|
@@ -104,4 +100,4 @@ block that defines the body of the class. For example,
|
|
104
100
|
|
105
101
|
== Dependencies
|
106
102
|
|
107
|
-
|
103
|
+
None.
|
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@ require 'rake/gempackagetask'
|
|
4
4
|
require 'rake/contrib/sshpublisher'
|
5
5
|
|
6
6
|
PKG_NAME = 'module_creation_helper'
|
7
|
-
PKG_VERSION = '0.0.
|
7
|
+
PKG_VERSION = '0.0.4'
|
8
8
|
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
9
9
|
RUBY_FORGE_PROJECT = 'pluginaweek'
|
10
10
|
|
@@ -39,8 +39,8 @@ spec = Gem::Specification.new do |s|
|
|
39
39
|
s.has_rdoc = true
|
40
40
|
s.test_files = Dir['test/**/*_test.rb']
|
41
41
|
|
42
|
-
s.author = 'Aaron Pfeifer
|
43
|
-
s.email = '
|
42
|
+
s.author = 'Aaron Pfeifer'
|
43
|
+
s.email = 'aaron@pluginaweek.org'
|
44
44
|
s.homepage = 'http://www.pluginaweek.org'
|
45
45
|
end
|
46
46
|
|
@@ -52,12 +52,12 @@ end
|
|
52
52
|
|
53
53
|
desc 'Publish the beta gem'
|
54
54
|
task :pgem => [:package] do
|
55
|
-
Rake::SshFilePublisher.new('
|
55
|
+
Rake::SshFilePublisher.new('aaron@pluginaweek.org', '/home/aaron/gems.pluginaweek.org/public/gems', 'pkg', "#{PKG_FILE_NAME}.gem").upload
|
56
56
|
end
|
57
57
|
|
58
58
|
desc 'Publish the API documentation'
|
59
59
|
task :pdoc => [:rdoc] do
|
60
|
-
Rake::SshDirPublisher.new('
|
60
|
+
Rake::SshDirPublisher.new('aaron@pluginaweek.org', "/home/aaron/api.pluginaweek.org/public/#{PKG_NAME}", 'rdoc').upload
|
61
61
|
end
|
62
62
|
|
63
63
|
desc 'Publish the API docs and gem'
|
@@ -1,64 +1,60 @@
|
|
1
1
|
module PluginAWeek #:nodoc:
|
2
|
-
module
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
else
|
45
|
-
superclass = ''
|
46
|
-
end
|
47
|
-
|
48
|
-
parent.class_eval <<-end_eval
|
49
|
-
#{self.to_s.downcase} #{name}#{superclass}
|
50
|
-
end
|
51
|
-
end_eval
|
52
|
-
|
53
|
-
mod = parent.const_get(name)
|
54
|
-
mod.class_eval(&block) if block_given?
|
55
|
-
mod
|
56
|
-
end
|
2
|
+
module ModuleCreationHelper
|
3
|
+
# Creates a new module with the specified name. This is essentially the
|
4
|
+
# same as actually defining the module like so:
|
5
|
+
#
|
6
|
+
# module NewModule
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
# or as a class:
|
10
|
+
#
|
11
|
+
# class NewClass < SuperKlass
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# Configuration options:
|
15
|
+
# <tt>superclass</tt> - The class to inherit from. This only applies when using Class#create. Default is Object.
|
16
|
+
# <tt>parent</tt> - The class/module that contains this module. Default is Object.
|
17
|
+
#
|
18
|
+
# Examples:
|
19
|
+
#
|
20
|
+
# Module.create('Foo') # => Foo
|
21
|
+
# Module.create('Bar', :parent => Foo) # => Foo::Bar
|
22
|
+
# Class.create('Waddle') # => Waddle
|
23
|
+
# Class.create('Widdle', :parent => Waddle) # => Waddle::Widdle
|
24
|
+
# Class.create('Woddle', :superclass => Waddle::Widdle, :parent => Waddle) # => Waddle::Woddle
|
25
|
+
# Waddle::Woddle.superclass # => Waddle::Widdle
|
26
|
+
def create(name, options = {}, &block)
|
27
|
+
options.assert_valid_keys(
|
28
|
+
:superclass,
|
29
|
+
:parent
|
30
|
+
)
|
31
|
+
raise ArgumentError, 'Modules cannot have superclasses' if options[:superclass] && self.to_s == 'Module'
|
32
|
+
|
33
|
+
options.reverse_merge!(
|
34
|
+
:superclass => Object,
|
35
|
+
:parent => Object
|
36
|
+
)
|
37
|
+
parent = options[:parent]
|
38
|
+
superclass = options[:superclass]
|
39
|
+
|
40
|
+
if superclass != Object
|
41
|
+
superclass = " < ::#{superclass}"
|
42
|
+
else
|
43
|
+
superclass = ''
|
57
44
|
end
|
45
|
+
|
46
|
+
parent.class_eval <<-end_eval
|
47
|
+
#{self.to_s.downcase} #{name}#{superclass}
|
48
|
+
end
|
49
|
+
end_eval
|
50
|
+
|
51
|
+
mod = parent.const_get(name)
|
52
|
+
mod.class_eval(&block) if block_given?
|
53
|
+
mod
|
58
54
|
end
|
59
55
|
end
|
60
56
|
end
|
61
57
|
|
62
|
-
|
63
|
-
extend PluginAWeek::
|
58
|
+
Module.class_eval do
|
59
|
+
extend PluginAWeek::ModuleCreationHelper
|
64
60
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper'
|
2
2
|
|
3
|
-
module
|
3
|
+
module Ford
|
4
4
|
end
|
5
5
|
|
6
|
-
class
|
6
|
+
class Vehicle
|
7
7
|
cattr_accessor :test_name
|
8
8
|
cattr_accessor :test_inspect
|
9
9
|
cattr_accessor :test_to_s
|
@@ -16,117 +16,181 @@ class TestSuperclass
|
|
16
16
|
end
|
17
17
|
|
18
18
|
class ModuleCreationHelperTest < Test::Unit::TestCase
|
19
|
+
def test_should_raise_exception_if_invalid_option_specified
|
20
|
+
assert_raise(ArgumentError) {Class.create(nil, :invalid => true)}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class ModuleCreationHelperForClassTest < Test::Unit::TestCase
|
19
25
|
def setup
|
20
|
-
|
21
|
-
TestSuperclass.test_inspect = nil
|
22
|
-
TestSuperclass.test_to_s = nil
|
26
|
+
@car = Class.create('Car')
|
23
27
|
end
|
24
28
|
|
25
|
-
def
|
26
|
-
|
27
|
-
assert_equal Object, klass.superclass
|
28
|
-
assert_equal Object, klass.parent
|
29
|
-
assert Object.const_defined?('Foo')
|
29
|
+
def test_should_have_object_as_superclass
|
30
|
+
assert_equal Object, @car.superclass
|
30
31
|
end
|
31
32
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
35
|
-
assert Object.const_defined?('FooMod')
|
33
|
+
def test_should_have_object_as_parent
|
34
|
+
assert_equal Object, @car.parent
|
35
|
+
assert Object.const_defined?('Car')
|
36
36
|
end
|
37
37
|
|
38
|
-
def
|
39
|
-
|
38
|
+
def teardown
|
39
|
+
Object.send(:remove_const, 'Car')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class ModuleCreationHelperForClassWithSuperclassTest < Test::Unit::TestCase
|
44
|
+
def setup
|
45
|
+
@car = Class.create('Car', :superclass => Vehicle)
|
40
46
|
end
|
41
47
|
|
42
|
-
def
|
43
|
-
|
48
|
+
def test_should_inherit_from_superclass
|
49
|
+
assert_equal Vehicle, @car.superclass
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_should_have_object_as_parent
|
53
|
+
assert_equal Object, @car.parent
|
54
|
+
assert Object.const_defined?('Car')
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_should_evaluate_name_as_name
|
58
|
+
assert_equal 'Car', Vehicle.test_name
|
44
59
|
end
|
45
60
|
|
46
|
-
def
|
47
|
-
|
48
|
-
assert_equal TestSuperclass, klass.superclass
|
49
|
-
assert_equal Object, klass.parent
|
50
|
-
assert Object.const_defined?('Bar')
|
61
|
+
def test_should_evaluate_inspect_as_name
|
62
|
+
assert_equal 'Car', Vehicle.test_inspect
|
51
63
|
end
|
52
64
|
|
53
|
-
def
|
54
|
-
|
55
|
-
assert_equal Object, klass.superclass
|
56
|
-
assert_equal TestParent, klass.parent
|
57
|
-
assert TestParent.const_defined?('Baz')
|
65
|
+
def test_should_evaluate_to_s_as_name
|
66
|
+
assert_equal 'Car', Vehicle.test_to_s
|
58
67
|
end
|
59
68
|
|
60
|
-
def
|
61
|
-
|
62
|
-
|
63
|
-
|
69
|
+
def teardown
|
70
|
+
Object.send(:remove_const, 'Car')
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class ModuleCreationHelperForClassWithParentTest < Test::Unit::TestCase
|
75
|
+
def setup
|
76
|
+
@car = Class.create('Car', :parent => Ford)
|
64
77
|
end
|
65
78
|
|
66
|
-
def
|
67
|
-
|
68
|
-
assert_equal TestSuperclass, klass.superclass
|
69
|
-
assert_equal TestParent, klass.parent
|
70
|
-
assert TestParent.const_defined?('Biz')
|
79
|
+
def test_should_have_object_as_superclass
|
80
|
+
assert_equal Object, @car.superclass
|
71
81
|
end
|
72
82
|
|
73
|
-
def
|
74
|
-
|
75
|
-
|
83
|
+
def test_should_be_nested_within_parent
|
84
|
+
assert_equal Ford, @car.parent
|
85
|
+
assert Ford.const_defined?('Car')
|
86
|
+
end
|
87
|
+
|
88
|
+
def teardown
|
89
|
+
Ford.send(:remove_const, 'Car')
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class ModuleCreationHelperForClassWithSuperclassAndParentTest < Test::Unit::TestCase
|
94
|
+
def setup
|
95
|
+
Vehicle.test_name = nil
|
96
|
+
Vehicle.test_inspect = nil
|
97
|
+
Vehicle.test_to_s = nil
|
98
|
+
|
99
|
+
@car = Class.create('Car', :superclass => Vehicle, :parent => Ford)
|
76
100
|
end
|
77
101
|
|
78
|
-
def
|
79
|
-
|
80
|
-
assert_equal 'Widdle', TestSuperclass.test_inspect
|
102
|
+
def test_should_inherit_from_superclass
|
103
|
+
assert_equal Vehicle, @car.superclass
|
81
104
|
end
|
82
105
|
|
83
|
-
def
|
84
|
-
|
85
|
-
|
106
|
+
def test_should_be_nested_within_parent
|
107
|
+
assert_equal Ford, @car.parent
|
108
|
+
assert Ford.const_defined?('Car')
|
86
109
|
end
|
87
110
|
|
88
|
-
def
|
89
|
-
|
90
|
-
assert_equal 'TestParent::Waddle', TestSuperclass.test_name
|
111
|
+
def test_should_evaluate_name_as_name
|
112
|
+
assert_equal 'Ford::Car', Vehicle.test_name
|
91
113
|
end
|
92
114
|
|
93
|
-
def
|
94
|
-
|
95
|
-
assert_equal 'TestParent::Widdle', TestSuperclass.test_inspect
|
115
|
+
def test_should_evaluate_inspect_as_name
|
116
|
+
assert_equal 'Ford::Car', Vehicle.test_inspect
|
96
117
|
end
|
97
118
|
|
98
|
-
def
|
99
|
-
|
100
|
-
assert_equal 'TestParent::Wuddle', TestSuperclass.test_to_s
|
119
|
+
def test_should_evaluate_to_s_as_name
|
120
|
+
assert_equal 'Ford::Car', Vehicle.test_to_s
|
101
121
|
end
|
102
122
|
|
103
|
-
def
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
123
|
+
def teardown
|
124
|
+
Ford.send(:remove_const, 'Car')
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
class ModuleCreationHelperForClassWithDynamicSuperclassTest < Test::Unit::TestCase
|
129
|
+
def setup
|
130
|
+
@car = Class.create('Car')
|
131
|
+
@convertible = Class.create('Convertible', :superclass => @car)
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_should_inherit_from_superclass
|
135
|
+
assert_equal @car, @convertible.superclass
|
111
136
|
end
|
112
137
|
|
113
|
-
def
|
114
|
-
|
115
|
-
|
116
|
-
|
138
|
+
def teardown
|
139
|
+
Object.send(:remove_const, 'Convertible')
|
140
|
+
Object.send(:remove_const, 'Car')
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
class ModuleCreationHelperForClassWithCustomMethods < Test::Unit::TestCase
|
145
|
+
def setup
|
146
|
+
@car = Class.create('Car', :superclass => Vehicle) do
|
147
|
+
def self.color
|
148
|
+
'red'
|
117
149
|
end
|
118
150
|
end
|
119
|
-
assert_equal 'hello', ClassWithBlock.say_hello
|
120
151
|
end
|
121
152
|
|
122
|
-
def
|
123
|
-
|
124
|
-
|
125
|
-
|
153
|
+
def test_should_evaluate_methods
|
154
|
+
assert_equal 'red', Car.color
|
155
|
+
end
|
156
|
+
|
157
|
+
def teardown
|
158
|
+
Object.send(:remove_const, 'Car')
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
class ModuleCreationHelperForModuleTest < Test::Unit::TestCase
|
163
|
+
def setup
|
164
|
+
@autopilot = Module.create('Autopilot')
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_should_have_object_as_parent
|
168
|
+
assert_equal Object, @autopilot.parent
|
169
|
+
assert Object.const_defined?('Autopilot')
|
170
|
+
end
|
171
|
+
|
172
|
+
def teardown
|
173
|
+
Object.send(:remove_const, 'Autopilot')
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
class ModuleCreationHelperForModuleWithSuperclassTest < Test::Unit::TestCase
|
178
|
+
def test_should_raise_an_exception
|
179
|
+
assert_raise(ArgumentError) {Module.create(nil, :superclass => Object)}
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
class ModuleCreationHelperForModuleWithParentTest < Test::Unit::TestCase
|
184
|
+
def setup
|
185
|
+
@autopilot = Module.create('Autopilot', :parent => Ford)
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_should_be_nested_within_parent
|
189
|
+
assert_equal Ford, @autopilot.parent
|
190
|
+
assert Ford.const_defined?('Autopilot')
|
126
191
|
end
|
127
192
|
|
128
|
-
|
129
|
-
|
130
|
-
"#<Class:0x#{(klass.object_id * 2).to_s(16)}>"
|
193
|
+
def teardown
|
194
|
+
Ford.send(:remove_const, 'Autopilot')
|
131
195
|
end
|
132
196
|
end
|
metadata
CHANGED
@@ -1,33 +1,26 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.0
|
3
|
-
specification_version: 1
|
4
2
|
name: module_creation_helper
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2007-09-18 00:00:00 -04:00
|
8
|
-
summary: Adds a helper method for creating new modules and classes at runtime.
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: info@pluginaweek.org
|
12
|
-
homepage: http://www.pluginaweek.org
|
13
|
-
rubyforge_project:
|
14
|
-
description:
|
15
|
-
autorequire: module_creation_helper
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.0.0
|
24
|
-
version:
|
4
|
+
version: 0.0.4
|
25
5
|
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
29
6
|
authors:
|
30
|
-
- Aaron Pfeifer
|
7
|
+
- Aaron Pfeifer
|
8
|
+
autorequire: module_creation_helper
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-05-05 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: aaron@pluginaweek.org
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
31
24
|
files:
|
32
25
|
- lib/module_creation_helper.rb
|
33
26
|
- test/test_helper.rb
|
@@ -37,17 +30,31 @@ files:
|
|
37
30
|
- MIT-LICENSE
|
38
31
|
- Rakefile
|
39
32
|
- README
|
40
|
-
|
41
|
-
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://www.pluginaweek.org
|
35
|
+
post_install_message:
|
42
36
|
rdoc_options: []
|
43
37
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
50
52
|
requirements: []
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.1.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: Adds a helper method for creating new modules and classes at runtime.
|
59
|
+
test_files:
|
60
|
+
- test/module_creation_helper_test.rb
|