Module: RTM::Extendable

Defined in:
rtm/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

Instance Method Details

- (Object) included(klass)

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
# File 'rtm/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 "def self.included(klass2)\nself.ancestors.find {|anc| anc.respond_to?(:register_implementation)}.register_implementation(klass2)\nend\n"
end

- (Object) register_extension(mod)

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 'rtm/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

- (Object) register_implementation(klass)

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.



66
67
68
69
# File 'rtm/lib/rtm/extensions.rb', line 66

def register_implementation(klass)
  @implementations ||= []
  @implementations << klass
end