Class: DCI::Role

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dci-ruby/dci/role.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Role

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


74
75
76
77
# File 'lib/dci-ruby/dci/role.rb', line 74

def initialize(opts={})
  @player  = opts[:player]
  @context = opts[:context]
end

Class Method Details

.delegate_to_player(player_methodname, role_method_name = player_methodname) ⇒ Object

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


19
20
21
# 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) ⇒ Object

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


41
42
43
# 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) ⇒ Object

Make this class abstract: will not allow create instances.



10
11
12
13
# 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

.private_delegate_to_player(player_methodname, role_method_name = player_methodname) ⇒ Object

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


28
29
30
31
# 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) ⇒ Object

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


54
55
56
# File 'lib/dci-ruby/dci/role.rb', line 54

def private_delegates_to_player(*methodnames)
  methodnames.each {|methodname| private_delegate_to_player(methodname)}
end