class DCI::Role
Public Class Methods
Defines a public instance_method named role_method_name delegating its behaviour to player#player_methodname Ex: delegate_to_player
:fullname, :name #=> def name(*args, &block)
player.send(:fullname, *args, &block) end
# File lib/dci-ruby/dci/role.rb, line 19 def delegate_to_player(player_methodname, role_method_name = player_methodname) class_eval("def #{role_method_name}(*args, &block); player.send(:#{player_methodname}, *args, &block) end") end
Defines multiple public instance_methods whose names are in the methodnames list delegating their behaviour to their counterparts in player. Ex: delegates_to_player
:fullname, :age #=> def fullname(*args, &block)
player.send(:fullname, *args, &block) end def age(*args, &block) player.send(:age, *args, &block) end
# File lib/dci-ruby/dci/role.rb, line 41 def delegates_to_player(*methodnames) methodnames.each {|methodname| delegate_to_player(methodname)} end
Make this class abstract: will not allow create instances.
# File lib/dci-ruby/dci/role.rb, line 10 def new(*args, &block) raise 'This class is meant to be abstract and not instantiable' if self == DCI::Role super end
Opts:
player => the object to adquire this role, context => the context instance in which this role instance will play role_mate_keys => list of keys of the rest of roles playing in the given context
# File lib/dci-ruby/dci/role.rb, line 74 def initialize(opts={}) @player = opts[:player] @context = opts[:context] end
Defines a private instance_method named role_method_name delegating its behaviour to player#player_methodname Ex: delegate_to_player
:fullname, :name #=> def name(*args, &block)
player.send(:fullname, *args, &block) end private :name
# File lib/dci-ruby/dci/role.rb, line 28 def private_delegate_to_player(player_methodname, role_method_name = player_methodname) delegate_to_player(player_methodname, role_method_name) private role_method_name end
Defines multiple private instance_methods whose names are in the methodnames list delegating their behaviour to their counterparts in player. Ex: private_delegates_to_player
:fullname, :age #=> def fullname(*args, &block)
player.send(:fullname, *args, &block) end def age(*args, &block) player.send(:age, *args, &block) end private :fullname, :age
# File lib/dci-ruby/dci/role.rb, line 54 def private_delegates_to_player(*methodnames) methodnames.each {|methodname| private_delegate_to_player(methodname)} end
Private Class Methods
Defines a new private reader instance method for a context mate role, delegating it to the context object.
# File lib/dci-ruby/dci/role.rb, line 62 def add_role_reader_for!(rolekey) return if private_method_defined?(rolekey) define_method(rolekey) {@context.send(rolekey)} private rolekey end
Private Instance Methods
Make the original object playing the role accessible only inside role definition code!
# File lib/dci-ruby/dci/role.rb, line 83 def player @player end
The role definition code also have private access to the extra args given in the context instantiation.
# File lib/dci-ruby/dci/role.rb, line 88 def settings(*keys) @context.send(:settings, *keys) end