Module: RTM::Extendable
- Defined in:
- lib/rtm/extensions.rb
Overview
Extend a module or class with this method to make it a receiver for RTM extensions. E.g. MyModule.extend(Extendable) or MyClass.extend(Extendable).
A extension can then be registered using MyModule.register_extension(MyExtension)
The module will then keep track of all modules or classes it’s included in. These modules and classes are called implementations here. If an extension is added later, the implementations will also be updated which would not be the case if normal includes were used.
Instance Method Summary collapse
-
#included(klass) ⇒ Object
A standard Ruby hook to be notified about inclusions of this module.
-
#register_extension(mod) ⇒ Object
Register a module as an extension to an Entity in RTM.
-
#register_implementation(klass) ⇒ Object
Register an implementation to this module.
Instance Method Details
#included(klass) ⇒ Object
A standard Ruby hook to be notified about inclusions of this module. This normally eliminates the need to register a module as implementa
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rtm/extensions.rb', line 49 def included(klass) # super # puts "[RTMEXT] including #{self} into #{klass} in #{caller(1).first}" register_implementation(klass) klass.class_eval do def self.included(klass2) # puts "[RTMIMPLEXT] #{self} was included into #{klass2} in #{caller(1).first}" # self.ancestors.select {|anc| anc.respond_to?(:register_implementation)}.each{|anc| anc.register_implementation(klass2) } self.ancestors.find {|anc| anc.respond_to?(:register_implementation)}.register_implementation(klass2) end end end |
#register_extension(mod) ⇒ Object
Register a module as an extension to an Entity in RTM. An entity may be e.g. RTM::TopicMap, RTM::Topic or any Module which was extended with RTM::Extendable
Besides normal ruby inclusion of the module, this module keeps track of its implementations which are also updated in this method.
38 39 40 41 42 43 44 45 |
# File 'lib/rtm/extensions.rb', line 38 def register_extension(mod) include mod return unless @implementations @implementations.each do |impl| next if impl.ancestors.include?(mod) impl.send(:include, mod) end end |
#register_implementation(klass) ⇒ Object
Register an implementation to this module. Registred implementations will be updated if later any extensions are added. This method should normally be called from the included hook. Due to Ruby’s restrictions on calling protected methods from other modules extending the very same module Extendable, this method must be public.
67 68 69 70 71 |
# File 'lib/rtm/extensions.rb', line 67 def register_implementation(klass) # super if self.class.superclass.respond_to?(:register_implementation) @implementations ||= [] @implementations << klass end |