ghaki-registry 2011.11.30.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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Gerald Kalafut
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,23 @@
1
+ = Ghaki Registry = Register and autoload packages.
2
+
3
+ Ghaki Registry is a helper library for registering and loading features.
4
+
5
+ == Download
6
+
7
+ The latest version of Ghaki Registry can be found at
8
+
9
+ * git@github.com:ghaki/ghaki-registry.git
10
+
11
+ == Installation
12
+
13
+ The preferred method of installing Ghaki Registry is through its GEM file.
14
+
15
+ % [sudo] gem install ghaki-registry-1.0.0.gem
16
+
17
+ == License
18
+
19
+ Ghaki Registry is released under the MIT license.
20
+
21
+ == Support
22
+
23
+ Contact mailto:gerald@kalafut.org
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2011.11.30.1
@@ -0,0 +1,8 @@
1
+ require 'ghaki/app/engine'
2
+ require 'ghaki/registry/engine'
3
+
4
+ Ghaki::App::Engine.class_eval do
5
+ def registry
6
+ Ghaki::Registry::Engine.instance
7
+ end
8
+ end
@@ -0,0 +1,99 @@
1
+ ############################################################################
2
+ require 'singleton'
3
+ require 'ghaki/registry/errors'
4
+ require 'ghaki/registry/feature'
5
+
6
+ ############################################################################
7
+ module Ghaki module Registry
8
+ class Engine
9
+ include Singleton
10
+
11
+ ########################################################################
12
+ attr_reader :features
13
+
14
+ ########################################################################
15
+ def initialize
16
+ clear_features
17
+ end
18
+ def clear_features
19
+ @features = {}
20
+ end
21
+
22
+ ########################################################################
23
+ def reserve_feature name
24
+ @features[name] ||= Ghaki::Registry::Feature.new( name )
25
+ end
26
+ def remove_feature name
27
+ @features.delete(name)
28
+ end
29
+ def has_feature? name
30
+ @features.has_key? name
31
+ end
32
+ def assert_feature! name
33
+ raise FeatureNotFoundError.new(name) unless has_feature? name
34
+ end
35
+ def get_feature name
36
+ assert_feature! name
37
+ @features[name]
38
+ end
39
+
40
+ ########################################################################
41
+ def enable_feature name
42
+ get_feature(name).enable
43
+ end
44
+ def disable_feature name
45
+ get_feature(name).disable
46
+ end
47
+ def enabled_feature? name
48
+ get_feature(name).enabled?
49
+ end
50
+
51
+ ########################################################################
52
+ def plugins feat
53
+ get_feature(feat).plugins
54
+ end
55
+ def add_plugin feat, plug, klass
56
+ reserve_feature(feat).add_plugin( plug, klass )
57
+ end
58
+ def has_plugin? feat, plug
59
+ get_feature(feat).has_plugin? plug
60
+ end
61
+ def assert_plugin! feat, plug
62
+ get_feature(feat).assert_plugin!(plug)
63
+ end
64
+ def remove_plugin feat, plug
65
+ get_feature(feat).remove_plugin(plug) if has_feature?(feat)
66
+ end
67
+ def clear_plugins feat
68
+ get_feature(feat).clear_plugins if has_feature?(feat)
69
+ end
70
+ def load_plugin feat, plug, lib_path
71
+ get_feature(feat).load_plugin(plug,lib_path)
72
+ end
73
+ def get_plugin feat, plug
74
+ get_feature(feat).get_plugin(plug)
75
+ end
76
+
77
+ ########################################################################
78
+ def enable_plugin feat, plug
79
+ get_plugin(feat,plug).enable
80
+ end
81
+ def disable_plugin feat, plug
82
+ get_plugin(feat,plug).disable
83
+ end
84
+ def enabled_plugin? feat, plug
85
+ get_plugin(feat,plug).enabled?
86
+ end
87
+ def factory_create feat, plug, *args
88
+ get_plugin(feat,plug).create(*args)
89
+ end
90
+ def plugin_failure feat, plug
91
+ get_plugin(feat,plug).failure
92
+ end
93
+ def failed_plugin? feat, plug
94
+ get_plugin(feat,plug).failed?
95
+ end
96
+
97
+ end
98
+ end end
99
+ ############################################################################
@@ -0,0 +1,60 @@
1
+ ############################################################################
2
+ module Ghaki
3
+
4
+ ##########################################################################
5
+ class FeatureDisabledError < RuntimeError
6
+ attr_accessor :feature
7
+ def initialize _feature, msg=nil
8
+ @feature = _feature
9
+ msg ||= 'Feature Disabled: ' + @feature.to_s
10
+ super( msg )
11
+ end
12
+ end
13
+
14
+ #-------------------------------------------------------------------------
15
+ class FeatureNotFoundError < RuntimeError
16
+ attr_accessor :feature
17
+ def initialize _feature, msg=nil
18
+ @feature = _feature
19
+ msg ||= 'Feature Not Registered: ' + @feature.to_s
20
+ super(msg)
21
+ end
22
+ end
23
+
24
+ ##########################################################################
25
+ class PluginDisabledError < RuntimeError
26
+ attr_accessor :feature, :plugin
27
+ def initialize _feature, _plugin, msg=nil
28
+ @feature = _feature
29
+ @plugin = _plugin
30
+ msg ||= 'Plugin Disabled: ' + @plugin.to_s
31
+ super(msg)
32
+ end
33
+ end
34
+
35
+ #-------------------------------------------------------------------------
36
+ class PluginNotFoundError < RuntimeError
37
+ attr_accessor :feature, :plugin
38
+ def initialize _feature, _plugin, msg=nil
39
+ @feature = _feature
40
+ @plugin = _plugin
41
+ msg ||= 'Plugin Not Registered: ' + @plugin.to_s
42
+ super(msg)
43
+ end
44
+ end
45
+
46
+ #-------------------------------------------------------------------------
47
+ class PluginLoadingError < RuntimeError
48
+ attr_accessor :feature, :plugin, :lib_path, :reason
49
+ def initialize _feature, _plugin, _lib_path, _reason
50
+ @feature = _feature
51
+ @plugin = _plugin
52
+ @lib_path = _lib_path
53
+ @reason = _reason
54
+ msg ||= 'Plugin Loading Error: ' + @reason.to_s
55
+ super(msg)
56
+ end
57
+ end
58
+
59
+ end
60
+ ############################################################################
@@ -0,0 +1,65 @@
1
+ ############################################################################
2
+ require 'ghaki/registry/engine'
3
+
4
+ ############################################################################
5
+ module Ghaki module Registry
6
+ module Factory
7
+
8
+ ########################################################################
9
+ def register_feature feat_name, opts={}
10
+ use_simple = if opts.has_key?(:simple_naming) then opts[:simple_naming] else true end
11
+ feat_nick = if opts.has_key?(:nick_name) then opts[:nick_name] else feat_name end
12
+
13
+ feat_and_plug_map = {
14
+ :has_plugin? => :"has_#{feat_nick}_plugin?",
15
+ :enable_plugin => :"enable_#{feat_nick}_plugin",
16
+ :enabled_plugin? => :"enabled_#{feat_nick}_plugin?",
17
+ :disable_plugin => :"disable_#{feat_nick}_plugin",
18
+ :remove_plugin => :"remove_#{feat_nick}_plugin",
19
+ }
20
+
21
+ just_feat_map = {
22
+ :clear_plugins => :"clear_#{feat_nick}_plugins",
23
+ :plugins => :"#{feat_nick}_plugins",
24
+ }
25
+
26
+ Ghaki::Registry::Engine.instance.reserve_feature(feat_name)
27
+
28
+ (class << self; self; end).instance_eval do
29
+
30
+ feat_and_plug_map.each_pair do |simple,complex|
31
+ fac_meth = if use_simple then simple else complex end
32
+ define_method fac_meth do |plug_name|
33
+ Ghaki::Registry::Engine.instance.send( simple, feat_name, plug_name )
34
+ end
35
+ end
36
+
37
+ just_feat_map.each_pair do |simple,complex|
38
+ fac_meth = if use_simple then simple else complex end
39
+ define_method fac_meth do
40
+ Ghaki::Registry::Engine.instance.send( simple, feat_name )
41
+ end
42
+ end
43
+
44
+ fac_meth = if use_simple then :feature else :"#{feat_nick}_feature" end
45
+ define_method fac_meth do
46
+ Ghaki::Registry::Engine.instance.get_feature( feat_name )
47
+ end
48
+
49
+ fac_meth = if use_simple then :create else :"create_#{feat_nick}_plugin" end
50
+ define_method fac_meth do |plug_name,*args|
51
+ Ghaki::Registry::Engine.instance.factory_create( feat_name, plug_name, *args )
52
+ end
53
+
54
+ fac_meth = if use_simple then :add_plugin else :"add_#{feat_nick}_plugin" end
55
+ define_method fac_meth do |plug_name,klass|
56
+ Ghaki::Registry::Engine.instance.add_plugin( feat_name, plug_name, klass )
57
+ end
58
+
59
+ end # inst eval
60
+
61
+ end # def reg
62
+
63
+ end # class
64
+ end end # namespace
65
+ ############################################################################
@@ -0,0 +1,95 @@
1
+ ############################################################################
2
+ require 'ghaki/registry/service'
3
+
4
+ ############################################################################
5
+ module Ghaki module Registry
6
+ class Feature
7
+
8
+ ########################################################################
9
+ attr_accessor :name, :plugins
10
+
11
+ ########################################################################
12
+ def initialize _name
13
+ @name = _name
14
+ @enabled = true
15
+ @plugins = {}
16
+ end
17
+
18
+ ########################################################################
19
+ def to_s; self.name.to_s end
20
+
21
+ ########################################################################
22
+ def clear_plugins
23
+ @plugins = {}
24
+ end
25
+
26
+ ########################################################################
27
+ def enabled?
28
+ @enabled
29
+ end
30
+ def enable val=true
31
+ @enabled = val
32
+ end
33
+ def disable val=true
34
+ self.enable !val
35
+ end
36
+ def assert_enabled!
37
+ raise FeatureDisabledError.new( self.name ) unless self.enabled?
38
+ end
39
+
40
+ ########################################################################
41
+ def add_plugin plugin_name, plugin_class
42
+ @plugins[plugin_name] = Ghaki::Registry::Service.new( self, plugin_name, plugin_class )
43
+ end
44
+
45
+ ########################################################################
46
+ def load_plugin plugin_name, lib_path
47
+ begin
48
+ require lib_path
49
+ rescue Exception => e
50
+ @plugins[plugin_name] = Ghaki::Registry::Service.new( self, plugin_name, nil )
51
+ @plugins[plugin_name].failure = Ghaki::PluginLoadingError.new( self.name, plugin_name, lib_path, e)
52
+ nil
53
+ end
54
+ end
55
+
56
+ ########################################################################
57
+ def assert_plugin! plugin_name
58
+ raise PluginNotFoundError.new( self.name, plugin_name ) unless has_plugin? plugin_name
59
+ end
60
+ def get_plugin plugin_name
61
+ assert_plugin! plugin_name
62
+ @plugins[plugin_name]
63
+ end
64
+ def remove_plugin plugin_name
65
+ @plugins.delete plugin_name
66
+ end
67
+ def has_plugin? plugin_name
68
+ @plugins.has_key? plugin_name
69
+ end
70
+
71
+ ########################################################################
72
+ def factory_create plugin_name, *args
73
+ get_plugin(plugin_name).create(*args)
74
+ end
75
+ def enabled_plugin? plugin_name
76
+ get_plugin(plugin_name).enabled?
77
+ end
78
+ def disable_plugin plugin_name
79
+ get_plugin(plugin_name).disable
80
+ end
81
+ def enable_plugin plugin_name
82
+ get_plugin(plugin_name).enable
83
+ end
84
+
85
+ ########################################################################
86
+ def failed_plugin? plugin_name
87
+ get_plugin(plugin_name).failed?
88
+ end
89
+ def plugin_failure plugin_name
90
+ get_plugin(plugin_name).failure
91
+ end
92
+
93
+ end
94
+ end end
95
+ ############################################################################
@@ -0,0 +1,23 @@
1
+ ############################################################################
2
+ require 'ghaki/registry/engine'
3
+
4
+ ############################################################################
5
+ module Ghaki module Registry
6
+ module Plugin
7
+
8
+ ########################################################################
9
+ def set_plugin_registry feat_name, opts={}
10
+ use_simple = if opts.has_key?(:simple_naming) then opts[:simple_naming] else true end
11
+ feat_nick = if opts.has_key?(:nick_name) then opts[:nick_name] else feat_name end
12
+ reg_meth = if use_simple then :register_plugin else :"register_#{feat_nick}_plugin" end
13
+ Ghaki::Registry::Engine.instance.reserve_feature( feat_name )
14
+ (class << self; self; end).instance_eval do
15
+ define_method reg_meth do |reg_item|
16
+ Ghaki::Registry::Engine.instance.add_plugin( feat_name, reg_item, self )
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+ end end
23
+ ############################################################################
@@ -0,0 +1,57 @@
1
+ ############################################################################
2
+ require 'ghaki/registry/errors'
3
+
4
+ ############################################################################
5
+ module Ghaki module Registry
6
+ class Service
7
+
8
+ ########################################################################
9
+ attr_accessor :name, :feature, :klass, :failure
10
+
11
+ ########################################################################
12
+ def initialize _feature, _name, _klass
13
+ @feature = _feature
14
+ @name = _name
15
+ @klass = _klass
16
+ @enabled = true
17
+ @failure = nil
18
+ end
19
+
20
+ ########################################################################
21
+ def to_s; @name.to_s end
22
+
23
+ ########################################################################
24
+ def enabled?
25
+ @enabled
26
+ end
27
+ def enable val=true
28
+ @enabled = val
29
+ end
30
+ def disable val=true
31
+ self.enable !val
32
+ end
33
+ def assert_enabled!
34
+ raise PluginDisabledError.new( @feature, @name ) unless enabled?
35
+ end
36
+
37
+ ########################################################################
38
+ def failed?
39
+ !@failure.nil?
40
+ end
41
+ def assert_not_failed!
42
+ raise @failure if failed?
43
+ end
44
+
45
+ ########################################################################
46
+ def assert_can_create!
47
+ assert_not_failed!
48
+ assert_enabled!
49
+ end
50
+ def create *args
51
+ assert_can_create!
52
+ return @klass.new( *args )
53
+ end
54
+
55
+ end
56
+ end end
57
+ ############################################################################