module_creation_helper 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +4 -0
- data/Rakefile +2 -2
- data/lib/module_creation_helper/extensions/module.rb +64 -55
- metadata +3 -3
data/CHANGELOG.rdoc
CHANGED
data/Rakefile
CHANGED
@@ -5,11 +5,11 @@ require 'rake/contrib/sshpublisher'
|
|
5
5
|
|
6
6
|
spec = Gem::Specification.new do |s|
|
7
7
|
s.name = 'module_creation_helper'
|
8
|
-
s.version = '0.
|
8
|
+
s.version = '0.2.0'
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
10
|
s.summary = 'Adds a helper method for creating new modules and classes at runtime.'
|
11
11
|
|
12
|
-
s.files = FileList['{lib,test}/**/*']
|
12
|
+
s.files = FileList['{lib,test}/**/*'] + %w(CHANGELOG.rdoc init.rb LICENSE Rakefile README.rdoc)
|
13
13
|
s.require_path = 'lib'
|
14
14
|
s.has_rdoc = true
|
15
15
|
s.test_files = Dir['test/**/*_test.rb']
|
@@ -1,63 +1,72 @@
|
|
1
|
-
module
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
module
|
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
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
1
|
+
module ModuleCreationHelper
|
2
|
+
module Extensions #:nodoc:
|
3
|
+
# Adds helper methods for easily generating new modules/classes
|
4
|
+
module Module
|
5
|
+
# Creates a new module with the specified name. This is essentially the
|
6
|
+
# same as actually defining the module like so:
|
7
|
+
#
|
8
|
+
# module NewModule
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# or as a class:
|
12
|
+
#
|
13
|
+
# class NewClass < SuperKlass
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# Configuration options:
|
17
|
+
# * +superclass+ - The class to inherit from. This only applies when using Class#create. Default is Object.
|
18
|
+
# * +parent+ - The class/module that contains this module. Default is Object.
|
19
|
+
#
|
20
|
+
# == Examples
|
21
|
+
#
|
22
|
+
# Module.create('Foo') # => Foo
|
23
|
+
# Module.create('Bar', :parent => Foo) # => Foo::Bar
|
24
|
+
# Class.create('Waddle') # => Waddle
|
25
|
+
# Class.create('Widdle', :parent => Waddle) # => Waddle::Widdle
|
26
|
+
# Class.create('Woddle', :superclass => Waddle::Widdle, :parent => Waddle) # => Waddle::Woddle
|
27
|
+
# Waddle::Woddle.superclass # => Waddle::Widdle
|
28
|
+
#
|
29
|
+
# == Setting the parent
|
30
|
+
#
|
31
|
+
# Rather than setting the parent directly using the +parent+ configuration
|
32
|
+
# option, you can specify it in the actual name of the class like so:
|
33
|
+
#
|
34
|
+
# Module.create('Foo::Bar') # => Foo::Bar
|
35
|
+
#
|
36
|
+
# This has the same effect as the following:
|
37
|
+
#
|
38
|
+
# Module.create('Bar', :parent => Foo)
|
39
|
+
def create(name, options = {}, &block)
|
40
|
+
# Validate the provided options
|
41
|
+
invalid_options = options.keys - [:superclass, :parent]
|
42
|
+
raise ArgumentError, "Unknown key(s): #{invalid_options.join(", ")}" unless invalid_options.empty?
|
43
|
+
|
44
|
+
# Validate usage of :superclass option
|
45
|
+
raise ArgumentError, 'Modules cannot have superclasses' if options[:superclass] && self.to_s == 'Module'
|
46
|
+
|
47
|
+
options = {:superclass => Object, :parent => Object}.merge(options)
|
48
|
+
parent = options[:parent]
|
49
|
+
superclass = options[:superclass]
|
50
|
+
|
51
|
+
if superclass != Object
|
52
|
+
superclass = " < ::#{superclass}"
|
53
|
+
else
|
54
|
+
superclass = ''
|
55
55
|
end
|
56
|
+
|
57
|
+
mod = parent.class_eval <<-end_eval
|
58
|
+
#{self.to_s.downcase} #{name}#{superclass}
|
59
|
+
self
|
60
|
+
end
|
61
|
+
end_eval
|
62
|
+
|
63
|
+
mod.class_eval(&block) if block_given?
|
64
|
+
mod
|
56
65
|
end
|
57
66
|
end
|
58
67
|
end
|
59
68
|
end
|
60
69
|
|
61
70
|
Module.class_eval do
|
62
|
-
extend
|
71
|
+
extend ModuleCreationHelper::Extensions::Module
|
63
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: module_creation_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Pfeifer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-12-14 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -55,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
55
|
requirements: []
|
56
56
|
|
57
57
|
rubyforge_project: pluginaweek
|
58
|
-
rubygems_version: 1.
|
58
|
+
rubygems_version: 1.2.0
|
59
59
|
signing_key:
|
60
60
|
specification_version: 2
|
61
61
|
summary: Adds a helper method for creating new modules and classes at runtime.
|