Class: DCI::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/dci-ruby/dci/context.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Context

Instances of a defined subclass of Context are initialized checking first that all subclass defined roles are provided in the creation invocation raising an error if any of them is missing. Once the previous check is met, every object playing in the context instance is associated to the stated role. Non players args are associated to instance_variables and readers defined.



80
81
82
83
84
85
# File 'lib/dci-ruby/dci/context.rb', line 80

def initialize(args={})
  check_all_roles_provided_in!(args)
  players, noplayers = args.partition {|key, *| roles.has_key?(key)}.map {|group| Hash[*group.flatten]}
  assign_roles_to_players(players)
  @settings = noplayers
end

Class Method Details

.[](*args) ⇒ Object

A short way for ContextSubclass.new(players_and_extra_args).run(extra_args)



19
20
21
# File 'lib/dci-ruby/dci/context.rb', line 19

def [](*args)
  new(*args).run
end

.inherited(subklass) ⇒ Object

Every subclass of Context has is own class and instance method roles defined. The instance method delegates value to the class.



10
11
12
13
14
15
16
# File 'lib/dci-ruby/dci/context.rb', line 10

def inherited(subklass)
  subklass.class_eval do
    @roles ||= {}
    def self.roles; @roles end
    def roles; self.class.roles end
  end
end