Module: Rolable

Included in:
Object
Defined in:
lib/drsi/rolable.rb

Overview

This module defines a mechanism to extend and ‘unextend’ modules in an object.

The idea is to provide an object with a heap of extended modules:

the highest ones filled with the methods associated to the roles the object currently plays,
and the lowest ones, clean (no methods) when the object finishes playing roles.

reusing the empty ones or adding and extending new ones when it is needed.

Instance Method Summary collapse

Instance Method Details

#__play_role!(a_rolekey, role_mod, a_context) ⇒ Object

Make an object play (in the given ‘a_context’) the role defined as a module in ‘role_mod’:

- Get or extend a new empty role module,
- Copy role_mod instance_methods into it,
- Inject __context and settings methods.


15
16
17
18
19
20
21
22
23
# File 'lib/drsi/rolable.rb', line 15

def __play_role!(a_rolekey, role_mod, a_context)
  new_role = __next_role_for(role_mod)
  new_role.class_exec(a_context, a_rolekey) do |the_context, the_rolekey|
    private
    define_method(:__rolekey) {the_rolekey}
    define_method(:__context) {the_context}
    define_method(:settings) {|*keys| __context.send(:settings, *keys)}
  end
end

#__unplay_last_role!Object

Make an object stop playing the last role it plays, if any.



26
27
28
29
30
31
32
# File 'lib/drsi/rolable.rb', line 26

def __unplay_last_role!
  if role = __last_role
    methods = role.public_instance_methods(false) + role.protected_instance_methods(false) + role.private_instance_methods(false)
    methods.each {|name| role.send(:remove_method, name)}
    @__last_role_index = __last_role_index - 1
  end
end