dyoder-autocode 1.0.0 → 1.0.1
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/lib/autocode.rb +37 -16
- data/test/auto_create.rb +6 -5
- data/test/auto_eval.rb +2 -1
- data/test/auto_load.rb +1 -1
- data/test/helpers.rb +3 -1
- data/test/least_surprise.rb +61 -0
- metadata +4 -2
data/lib/autocode.rb
CHANGED
@@ -30,12 +30,38 @@ module AutoCode
|
|
30
30
|
:loaded => []
|
31
31
|
}
|
32
32
|
|
33
|
-
|
33
|
+
def auto_constructor( key = true, &block)
|
34
|
+
@autocode[:constructors][ AutoCode.normalize( key ) ] << block
|
35
|
+
end
|
36
|
+
|
34
37
|
def auto_create( key = true, options = {}, &block )
|
35
|
-
|
36
|
-
exemplar =
|
37
|
-
exemplar.
|
38
|
-
|
38
|
+
auto_constructor( key ) do | cname |
|
39
|
+
exemplar = options[:exemplar] || Module.new
|
40
|
+
new_constant = exemplar.clone
|
41
|
+
new_constant.send(:include, AutoCode)
|
42
|
+
new_constant.module_eval( &block ) if block
|
43
|
+
const_set( cname, new_constant )
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Convenience method for auto_creating classes.
|
48
|
+
def auto_create_class( key = true, parent = Object, &block )
|
49
|
+
auto_constructor( key ) do | cname |
|
50
|
+
parent = const_get(parent) unless parent.is_a? Class
|
51
|
+
new_constant = Class.new( parent )
|
52
|
+
new_constant.send(:include, AutoCode)
|
53
|
+
new_constant.module_eval( &block ) if block
|
54
|
+
const_set( cname, new_constant )
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Convenience method for auto_creating modules.
|
59
|
+
def auto_create_module( key = true, &block )
|
60
|
+
auto_constructor( key ) do | cname |
|
61
|
+
new_constant = Module.new
|
62
|
+
new_constant.send(:include, AutoCode)
|
63
|
+
new_constant.module_eval( &block ) if block
|
64
|
+
const_set( cname, new_constant )
|
39
65
|
end
|
40
66
|
end
|
41
67
|
|
@@ -53,20 +79,15 @@ module AutoCode
|
|
53
79
|
|
54
80
|
# Adds an arbitrary initializer block for the given key
|
55
81
|
def auto_eval( key, &block )
|
56
|
-
|
57
|
-
|
82
|
+
if key.is_a?( Symbol) && const_defined?( AutoCode.normalize( key ) )
|
83
|
+
const_get( key ).module_eval &block
|
84
|
+
else
|
85
|
+
@autocode[:initializers][ AutoCode.normalize( key ) ] << lambda do | mod |
|
86
|
+
mod.module_eval( &block )
|
87
|
+
end
|
58
88
|
end
|
59
89
|
end
|
60
90
|
|
61
|
-
# Convenience method for auto_create.
|
62
|
-
def auto_create_class( key = true, superclass = Object, &block )
|
63
|
-
auto_create( key,{ :exemplar => Class.new( superclass ) }, &block )
|
64
|
-
end
|
65
|
-
|
66
|
-
# Convenience method for auto_create.
|
67
|
-
def auto_create_module( key = true, &block )
|
68
|
-
auto_create( key,{ :exemplar => Module.new }, &block )
|
69
|
-
end
|
70
91
|
|
71
92
|
# Returns the list of constants that would be reloaded upon a call to reload.
|
72
93
|
def reloadable ; @autocode[:loaded] ; end
|
data/test/auto_create.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require File.
|
1
|
+
require "#{File.dirname(__FILE__)}/helpers"
|
2
2
|
|
3
3
|
describe "auto_create" do
|
4
4
|
|
@@ -7,18 +7,19 @@ describe "auto_create" do
|
|
7
7
|
module A
|
8
8
|
include AutoCode
|
9
9
|
auto_create_module :B do
|
10
|
-
|
11
|
-
auto_create_class
|
10
|
+
auto_create_class :C
|
11
|
+
auto_create_class true, :C
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
specify "allows you
|
16
|
+
specify "allows you create and initialize a given const name" do
|
17
17
|
A::B.class.should == Module
|
18
18
|
end
|
19
19
|
|
20
|
-
specify "allows you
|
20
|
+
specify "allows you create and initialize const using a wildcard" do
|
21
21
|
A::B::C.class.should === Class
|
22
|
+
A::B::D.superclass.should == A::B::C
|
22
23
|
end
|
23
24
|
|
24
25
|
specify "raises a NameError if a const doesn't match" do
|
data/test/auto_eval.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require File.
|
1
|
+
require "#{File.dirname(__FILE__)}/helpers"
|
2
2
|
|
3
3
|
describe "auto_eval" do
|
4
4
|
|
@@ -27,5 +27,6 @@ describe "auto_eval" do
|
|
27
27
|
specify "allows you to define nested auto_eval declarations" do
|
28
28
|
A::B::C::D.should == true
|
29
29
|
end
|
30
|
+
|
30
31
|
|
31
32
|
end
|
data/test/auto_load.rb
CHANGED
data/test/helpers.rb
CHANGED
@@ -0,0 +1,61 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/helpers"
|
2
|
+
|
3
|
+
describe "An auto_created module" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
Object.instance_eval { remove_const(:A) if const_defined?(:A) }
|
7
|
+
module A
|
8
|
+
module Foo; end
|
9
|
+
include AutoCode
|
10
|
+
auto_create_class :B do
|
11
|
+
def kobold
|
12
|
+
"koboldy"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "automatically includes AutoCode" do
|
19
|
+
A::B.included_modules.should.include AutoCode
|
20
|
+
end
|
21
|
+
|
22
|
+
it "can be initialized by later auto_eval declarations" do
|
23
|
+
A.auto_eval(:B) { def smurf; "smurfy" ; end }
|
24
|
+
A::B.new.kobold.should == "koboldy"
|
25
|
+
A::B.new.smurf.should == "smurfy"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "can be referenced in auto_* declarations without getting born" do
|
29
|
+
A.auto_create_class :C, :B
|
30
|
+
A.const_defined?(:B).should == false
|
31
|
+
A::C.new.kobold.should == "koboldy"
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "auto_eval" do
|
37
|
+
|
38
|
+
it "does a module_eval for constants that are already defined" do
|
39
|
+
A.auto_eval :Foo do
|
40
|
+
def self.yell; "Brick!" ; end
|
41
|
+
end
|
42
|
+
A::Foo.yell.should == "Brick!"
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
from_waves = lambda do
|
49
|
+
app.auto_create_module( :Views ) do
|
50
|
+
include AutoCode
|
51
|
+
auto_create_class :Default, Waves::Views::Base
|
52
|
+
auto_load :Default, :directories => [ :views ]
|
53
|
+
auto_create_class true, :Default
|
54
|
+
auto_load true, :directories => [ :views ]
|
55
|
+
end
|
56
|
+
app.auto_eval :Views do
|
57
|
+
auto_eval :Default do
|
58
|
+
include ViewMethods
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dyoder-autocode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Yoder
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date:
|
14
|
+
date: 2009-01-08 00:00:00 -08:00
|
15
15
|
default_executable:
|
16
16
|
dependencies: []
|
17
17
|
|
@@ -28,7 +28,9 @@ files:
|
|
28
28
|
- test/auto_create.rb
|
29
29
|
- test/auto_eval.rb
|
30
30
|
- test/auto_load.rb
|
31
|
+
- test/auto_module.rb
|
31
32
|
- test/helpers.rb
|
33
|
+
- test/least_surprise.rb
|
32
34
|
- test/normalize.rb
|
33
35
|
has_rdoc: true
|
34
36
|
homepage: http://dev.zeraweb.com/
|