module Rolable
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.
Public Instance Methods
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.
# 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
Make an object stop playing the last role it plays, if any.
# 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
Private Instance Methods
Creates and extends a new module ready to be filled with role instance methods.
# File lib/drsi/rolable.rb, line 58 def __add_role_for(mod) role = mod.dup extend(role) __roles << role role end
The context within this object is playing its last role. This method must be overidden in every __role definition module.
# File lib/drsi/rolable.rb, line 66 def __context nil end
# File lib/drsi/rolable.rb, line 45 def __last_role __roles[__last_role_index] end
# File lib/drsi/rolable.rb, line 41 def __last_role_index @__last_role_index ||= -1 end
Returns the highest role module free of methods. If none, creates a new empty module ready to be filled with role instance methods.
# File lib/drsi/rolable.rb, line 51 def __next_role_for(mod) @__last_role_index = __last_role_index + 1 new_role = __last_role new_role ? new_role.__copy_instance_methods_from(mod) : __add_role_for(mod) end
The rolekey this object is playing its last role. This method must be overidden in every __role definition module.
# File lib/drsi/rolable.rb, line 71 def __rolekey nil end
# File lib/drsi/rolable.rb, line 37 def __roles @__roles ||= Array.new end