Module: IMW::Utils::DynamicallyExtendable

Included in:
Resource
Defined in:
lib/imw/utils/dynamically_extendable.rb

Overview

Provides an including class with a class-level array of “handlers” that it can use to dynamically extend its instances with specific modules only if certain conditions are met.

This allows different instances of a class to implement very different behavior at runtime.

An example use case might be a Database class which dynamically extends its instances with an adaptor module appropriate to the particular database the object refers to.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(obj) ⇒ Object



16
17
18
# File 'lib/imw/utils/dynamically_extendable.rb', line 16

def self.included obj
  obj.extend(ClassMethods)
end

Instance Method Details

#extend(mod) ⇒ Object

Works just like Object#extend except it keeps track of the modules it has extended.

See Also:



31
32
33
34
# File 'lib/imw/utils/dynamically_extendable.rb', line 31

def extend mod
  modules << mod
  super mod
end

#extend_appropriately!(options = {}) ⇒ Object

Iterate through this object’s class’s handlers and extend this object with the module referred to by any matching handlers.



38
39
40
# File 'lib/imw/utils/dynamically_extendable.rb', line 38

def extend_appropriately! options={}
  self.class.extend_instance! self, options
end

#modulesArray

Return the modules this object has been extended by.

Returns:



23
24
25
# File 'lib/imw/utils/dynamically_extendable.rb', line 23

def modules
  @modules ||= []
end