micon 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +5 -5
- data/lib/micon.rb +5 -1
- data/lib/micon/class.rb +2 -2
- data/lib/micon/core.rb +408 -0
- data/lib/micon/helper.rb +26 -0
- data/lib/micon/metadata.rb +120 -124
- data/lib/micon/module.rb +39 -8
- data/lib/micon/rad.rb +8 -0
- data/lib/micon/spec.rb +20 -0
- data/lib/micon/support.rb +23 -9
- data/readme.md +18 -4
- data/spec/callbacks_spec.rb +65 -22
- data/spec/constants_spec.rb +75 -0
- data/spec/constants_spec/get_constant_component/lib/components/TheController.rb +3 -0
- data/spec/custom_scope_spec.rb +33 -34
- data/spec/initialization_spec.rb +71 -0
- data/spec/managed_spec.rb +14 -14
- data/spec/micelaneous_spec.rb +33 -29
- data/spec/micelaneous_spec/autoload/lib/components/TheRad/TheView.rb +3 -0
- data/spec/micelaneous_spec/autoload/lib/components/TheRouter.rb +3 -0
- data/spec/micelaneous_spec/autoload/lib/components/some_value.rb +3 -0
- data/spec/nested_custom_scope_spec.rb +13 -14
- data/spec/overview_spec.rb +7 -3
- data/spec/spec_helper.rb +10 -3
- data/spec/static_scope_spec.rb +47 -22
- metadata +30 -41
- data/lib/micon/micon.rb +0 -250
@@ -0,0 +1,75 @@
|
|
1
|
+
# require 'spec_helper'
|
2
|
+
#
|
3
|
+
# describe "Autoloading" do
|
4
|
+
# with_load_path "#{spec_dir}/get_constant_component/lib"
|
5
|
+
#
|
6
|
+
# before do
|
7
|
+
# self.micon = Micon::Core.new
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# after do
|
11
|
+
# remove_constants :TheRouter, :TheRad, :TheController, :SomeModule
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# describe "get_constant_component" do
|
15
|
+
# it "should autoload components" do
|
16
|
+
# micon.get_constant_component(:TheController).should == "TheController"
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# it "should not raise error if component not defined" do
|
20
|
+
# micon.get_constant_component(:TheValue).should == nil
|
21
|
+
# micon.register(:TheValue, constant: true){"TheValue"}
|
22
|
+
# micon.get_constant_component(:TheValue).should == "TheValue"
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# it "should get constants only" do
|
26
|
+
# micon.register(:TheController){"TheController"}
|
27
|
+
# micon.get_constant_component(:TheController).should == nil
|
28
|
+
#
|
29
|
+
# micon.register(:TheController, constant: true){"TheController"}
|
30
|
+
# micon.get_constant_component(:TheController).should == "TheController"
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# it 'validation' do
|
35
|
+
# -> {micon.register 'TheController', constant: true}.should raise_error(/symbol/)
|
36
|
+
# -> {micon.register 'TheController'.to_sym, constant: true, scope: :custom}.should raise_error(/scope/)
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# it "should use constants as components" do
|
40
|
+
# -> {::TheRouter}.should raise_error(/uninitialized constant/)
|
41
|
+
#
|
42
|
+
# micon.register :TheRouter, constant: true do
|
43
|
+
# "TheRouter"
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# module ::TheRad
|
47
|
+
# TheRouter.should == 'TheRouter'
|
48
|
+
# end
|
49
|
+
# ::TheRouter.should == 'TheRouter'
|
50
|
+
# micon[:TheRouter].should == 'TheRouter'
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# it "should support nested constants" do
|
54
|
+
# module ::TheRad; end
|
55
|
+
# -> {::TheRad::TheView}.should raise_error(/uninitialized constant/)
|
56
|
+
#
|
57
|
+
# micon.register 'TheRad::TheView'.to_sym, constant: true do
|
58
|
+
# 'TheRad::TheView'
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# module ::SomeModule
|
62
|
+
# ::TheRad::TheView.should == 'TheRad::TheView'
|
63
|
+
# end
|
64
|
+
# ::TheRad::TheView.should == 'TheRad::TheView'
|
65
|
+
# micon['TheRad::TheView'.to_sym].should == 'TheRad::TheView'
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# it "should check if constant is already defined" do
|
69
|
+
# micon.register :TheRouter, constant: true do
|
70
|
+
# class ::TheRouter; end
|
71
|
+
# 'TheRouter'
|
72
|
+
# end
|
73
|
+
# -> {::TheRouter}.should raise_error(/redefine/)
|
74
|
+
# end
|
75
|
+
# end
|
data/spec/custom_scope_spec.rb
CHANGED
@@ -1,49 +1,48 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "
|
4
|
-
before
|
5
|
-
Micon.
|
6
|
-
Micon.metadata.clear
|
3
|
+
describe "Custom Scope" do
|
4
|
+
before do
|
5
|
+
self.micon = Micon::Core.new
|
7
6
|
end
|
8
7
|
|
9
8
|
it "activate" do
|
10
9
|
container = {}
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
micon.should_not be_active(:custom)
|
11
|
+
micon.activate :custom, container
|
12
|
+
micon.should be_active(:custom)
|
14
13
|
|
15
|
-
|
14
|
+
-> {micon.activate :custom, container}.should raise_error(/active/)
|
16
15
|
|
17
|
-
|
18
|
-
|
16
|
+
micon.deactivate :custom
|
17
|
+
-> {micon.deactivate :custom}.should raise_error(/not active/)
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
micon.should_not be_active(:custom)
|
20
|
+
micon.activate :custom, container do
|
21
|
+
micon.should be_active(:custom)
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
26
25
|
it "check" do
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
micon.register(:value, scope: :custom){"The Object"}
|
27
|
+
-> {micon[:value]}.should raise_error(/not started/)
|
28
|
+
-> {micon[:value] = 'value'}.should raise_error(/not started/)
|
30
29
|
end
|
31
30
|
|
32
31
|
it "get" do
|
33
|
-
|
32
|
+
micon.register(:value, scope: :custom){"The Object"}
|
34
33
|
container, the_object = {}, nil
|
35
34
|
|
36
|
-
|
37
|
-
|
38
|
-
the_object =
|
35
|
+
micon.activate :custom, container do
|
36
|
+
micon[:value].should == "The Object"
|
37
|
+
the_object = micon[:value]
|
39
38
|
end
|
40
39
|
|
41
|
-
|
42
|
-
|
40
|
+
micon.activate :custom, {} do
|
41
|
+
micon[:value].object_id.should_not == the_object.object_id
|
43
42
|
end
|
44
43
|
|
45
|
-
|
46
|
-
|
44
|
+
micon.activate :custom, container do
|
45
|
+
micon[:value].object_id.should == the_object.object_id
|
47
46
|
end
|
48
47
|
|
49
48
|
container.size.should == 1
|
@@ -51,25 +50,25 @@ describe "Micon custom scope" do
|
|
51
50
|
end
|
52
51
|
|
53
52
|
it "set" do
|
54
|
-
|
53
|
+
micon.register(:value, scope: :custom){"The Object"}
|
55
54
|
container = {}
|
56
55
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
the_object =
|
56
|
+
micon.activate :custom, container do
|
57
|
+
micon[:value].should == "The Object"
|
58
|
+
micon[:value] = "Another Object"
|
59
|
+
the_object = micon[:value]
|
61
60
|
end
|
62
61
|
|
63
|
-
|
64
|
-
|
62
|
+
micon.activate :custom, {} do
|
63
|
+
micon[:value].should == "The Object"
|
65
64
|
end
|
66
65
|
|
67
|
-
|
68
|
-
|
66
|
+
micon.activate :custom, container do
|
67
|
+
micon[:value].should == "Another Object"
|
69
68
|
end
|
70
69
|
end
|
71
70
|
|
72
71
|
it "scope should return block value (from error)" do
|
73
|
-
|
72
|
+
micon.activate(:custom, {}){'value'}.should == 'value'
|
74
73
|
end
|
75
74
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Initialization" do
|
4
|
+
after do
|
5
|
+
remove_constants :TheRouter
|
6
|
+
end
|
7
|
+
|
8
|
+
it "clone" do
|
9
|
+
m = Micon::Core.new
|
10
|
+
m.initialize!
|
11
|
+
|
12
|
+
m.register(:the_value){'the_value'}
|
13
|
+
m[:the_value]
|
14
|
+
|
15
|
+
another = m.clone
|
16
|
+
another.metadata.should include(:the_value)
|
17
|
+
another.instance_variable_get('@registry').should include(:the_value)
|
18
|
+
another.instance_variable_get('@application').should include(:the_value)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "initialize! should set caller as global, deinitialize! should remove it" do
|
22
|
+
m = Micon::Core.new
|
23
|
+
m.initialize!
|
24
|
+
|
25
|
+
::MICON.object_id.should == m.object_id
|
26
|
+
m.deinitialize!
|
27
|
+
Object.const_defined?(:MICON).should be_false
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should support isolation" do
|
31
|
+
m1 = Micon::Core.new
|
32
|
+
m1.initialize!
|
33
|
+
|
34
|
+
m1.register(:first){'first'}
|
35
|
+
m1.first.should == 'first'
|
36
|
+
|
37
|
+
m1.deinitialize!
|
38
|
+
|
39
|
+
# m2
|
40
|
+
m2 = m1.clone
|
41
|
+
m2.initialize!
|
42
|
+
|
43
|
+
m2.first.should == 'first'
|
44
|
+
m2.register(:second){'second'}
|
45
|
+
m2.second.should == 'second'
|
46
|
+
m2.deinitialize!
|
47
|
+
|
48
|
+
# m1 shouldn't have any of m2 stuff
|
49
|
+
m1.initialize!
|
50
|
+
m1.first.should == 'first'
|
51
|
+
m1.metadata.should_not include(:second)
|
52
|
+
m1.include? :second
|
53
|
+
m1.should_not include(:second)
|
54
|
+
end
|
55
|
+
|
56
|
+
# describe "constants" do
|
57
|
+
# it "deinitialize! should delete all defined constants" do
|
58
|
+
# m = Micon::Core.new
|
59
|
+
# m.initialize!
|
60
|
+
#
|
61
|
+
# m.register(:TheRouter, constant: true){'TheRouter'}
|
62
|
+
# ::TheRouter.should == 'TheRouter'
|
63
|
+
#
|
64
|
+
# m.deinitialize!
|
65
|
+
# Object.const_defined?(:TheRouter).should be_false
|
66
|
+
#
|
67
|
+
# m.initialize!
|
68
|
+
# ::TheRouter.should == 'TheRouter'
|
69
|
+
# end
|
70
|
+
# end
|
71
|
+
end
|
data/spec/managed_spec.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "
|
3
|
+
describe "Managed" do
|
4
4
|
before :all do
|
5
|
-
Micon.
|
5
|
+
self.micon = Micon::Core.new
|
6
6
|
|
7
7
|
class ManagedObject
|
8
8
|
register_as :managed_object
|
9
|
-
inject :
|
9
|
+
inject object: :object_key
|
10
10
|
|
11
11
|
class << self
|
12
|
-
inject :
|
12
|
+
inject object: :object_key
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
before
|
18
|
-
|
17
|
+
before do
|
18
|
+
micon.clear
|
19
19
|
end
|
20
20
|
|
21
21
|
it "scope" do
|
22
|
-
scope =
|
23
|
-
initializer, dependencies =
|
22
|
+
scope = micon.metadata[:managed_object]
|
23
|
+
initializer, dependencies = micon.metadata.initializers[:managed_object]
|
24
24
|
scope.should == :application
|
25
25
|
initializer.call.should be_a(ManagedObject)
|
26
26
|
end
|
27
27
|
|
28
28
|
it "injection" do
|
29
29
|
the_object = "The Object"
|
30
|
-
|
30
|
+
micon.register(:object_key){the_object}
|
31
31
|
|
32
32
|
ManagedObject.object.should == the_object
|
33
33
|
o = ManagedObject.new
|
@@ -36,16 +36,16 @@ describe "Micon Managed" do
|
|
36
36
|
|
37
37
|
it "outjection" do
|
38
38
|
the_object = "The Object"
|
39
|
-
|
39
|
+
micon.register(:object_key)
|
40
40
|
|
41
|
-
ManagedObject.object.should
|
41
|
+
-> {ManagedObject.object}.should raise_error(/no initializer/)
|
42
42
|
ManagedObject.object = the_object
|
43
43
|
ManagedObject.object.should == the_object
|
44
44
|
end
|
45
45
|
|
46
46
|
it "empty?" do
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
micon.should be_empty
|
48
|
+
micon[:managed_object]
|
49
|
+
micon.should_not be_empty
|
50
50
|
end
|
51
51
|
end
|
data/spec/micelaneous_spec.rb
CHANGED
@@ -1,52 +1,56 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "Micelaneous" do
|
4
|
-
|
5
|
-
Micon.clear
|
6
|
-
Micon.metadata.clear
|
7
|
-
end
|
3
|
+
describe "Micelaneous" do
|
4
|
+
with_load_path "#{spec_dir}/autoload/lib"
|
8
5
|
|
9
|
-
|
10
|
-
Micon.
|
11
|
-
|
12
|
-
Micon.instance_variable_get("@_r").should include(:the_object)
|
13
|
-
|
14
|
-
old_metadat = Micon.swap_metadata
|
15
|
-
|
16
|
-
Micon.metadata[:the_object].should be_nil
|
17
|
-
Micon.instance_variable_get("@_r").should_not include(:the_object)
|
18
|
-
|
19
|
-
Micon.swap_metadata old_metadat
|
20
|
-
Micon.metadata[:the_object].should_not be_nil
|
21
|
-
Micon.instance_variable_get("@_r").should include(:the_object)
|
22
|
-
end
|
6
|
+
before do
|
7
|
+
self.micon = Micon::Core.new
|
8
|
+
end
|
23
9
|
|
24
|
-
|
25
|
-
|
26
|
-
lambda{Micon[:another_object]}.should raise_error(/the_object/)
|
27
|
-
Micon.register :the_object
|
28
|
-
Micon[:another_object]
|
10
|
+
after do
|
11
|
+
remove_constants :TheRouter, :TheRad
|
29
12
|
end
|
30
13
|
|
14
|
+
describe "autoloading" do
|
15
|
+
it "should autoload component definition" do
|
16
|
+
micon[:some_value].should == "some_value"
|
17
|
+
end
|
18
|
+
|
19
|
+
# it 'should autoload component - constants (and nested constants)' do
|
20
|
+
# TheRouter.should == "TheRouter"
|
21
|
+
# module ::TheRad; end
|
22
|
+
# TheRad::TheView.should == "TheView"
|
23
|
+
# end
|
24
|
+
end
|
25
|
+
|
31
26
|
it "should not initialize twice (from error)" do
|
32
27
|
check = mock
|
33
28
|
check.should_receive(:environment).once.ordered
|
34
29
|
check.should_receive(:router).once.ordered
|
35
30
|
|
36
|
-
|
31
|
+
micon.register :environment do
|
37
32
|
check.environment
|
38
33
|
'environment'
|
39
34
|
end
|
40
35
|
|
41
|
-
|
36
|
+
micon.register :router, depends_on: :environment do
|
42
37
|
check.router
|
43
38
|
'router'
|
44
39
|
end
|
45
|
-
|
40
|
+
micon.after :environment do
|
46
41
|
# some code that needs :router
|
47
|
-
|
42
|
+
micon[:router]
|
48
43
|
end
|
49
44
|
|
50
|
-
|
45
|
+
micon[:router]
|
51
46
|
end
|
47
|
+
|
48
|
+
it "helper method generation" do
|
49
|
+
micon.register :router
|
50
|
+
|
51
|
+
micon.router?.should be_false
|
52
|
+
micon.router = 'router'
|
53
|
+
micon.router?.should be_true
|
54
|
+
micon.router.should == 'router'
|
55
|
+
end
|
52
56
|
end
|
@@ -1,31 +1,30 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "
|
4
|
-
before
|
5
|
-
Micon.
|
6
|
-
Micon.metadata.clear
|
3
|
+
describe "Nested custom scope" do
|
4
|
+
before do
|
5
|
+
self.micon = Micon::Core.new
|
7
6
|
end
|
8
7
|
|
9
8
|
it "with block" do
|
10
|
-
|
9
|
+
micon.register :value, scope: :custom
|
11
10
|
|
12
11
|
custom_a = {}
|
13
|
-
|
14
|
-
|
12
|
+
micon.activate :custom, custom_a do
|
13
|
+
micon[:value] = 'value a'
|
15
14
|
|
16
15
|
custom_b = {}
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
micon.activate :custom, custom_b do
|
17
|
+
micon.should_not include(:value)
|
18
|
+
micon[:value] = 'value b'
|
19
|
+
micon[:value].should == 'value b'
|
21
20
|
end
|
22
21
|
|
23
|
-
|
22
|
+
micon[:value].should == 'value a'
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
27
26
|
it "should not support nested scopes without block" do
|
28
|
-
|
29
|
-
|
27
|
+
micon.activate :custom, {}
|
28
|
+
-> {micon.activate :custom, {}}.should raise_error(/active/)
|
30
29
|
end
|
31
30
|
end
|