class DCI::Role

Public Class Methods

delegate_to_player(player_methodname, role_method_name = player_methodname) click to toggle source

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
delegates_to_player(*methodnames) click to toggle source

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
new(*args, &block) click to toggle source

Make this class abstract: will not allow create instances.

Calls superclass method
# 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
new(opts={}) click to toggle source

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
private_delegate_to_player(player_methodname, role_method_name = player_methodname) click to toggle source

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
private_delegates_to_player(*methodnames) click to toggle source

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

add_role_reader_for!(rolekey) click to toggle source

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

player() click to toggle source

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
settings(*keys) click to toggle source

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