Class: AdventureRL::Events::Event
- Inherits:
-
Object
- Object
- AdventureRL::Events::Event
- Defined in:
- lib/AdventureRL/Events/Event.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#add_object(object) ⇒ Object
(also: #add, #<<)
Add one or multiple
object(s) to this Event. -
#get_name ⇒ Object
Returns the
nameof the Event. -
#get_objects ⇒ Object
Returns the objects that subscribed to this Event.
-
#initialize(name) ⇒ Event
constructor
A new instance of Event.
-
#on_trigger(&block) ⇒ Object
Pass a block, which will be called when this Event is triggered (see #trigger).
-
#remove_object(object) ⇒ Object
Remove one or multiple
object(s) from this Event. -
#trigger(*args) ⇒ Object
The block defined with #on_trigger will be called for every subscribed object.
Constructor Details
#initialize(name) ⇒ Event
Returns a new instance of Event.
4 5 6 7 8 |
# File 'lib/AdventureRL/Events/Event.rb', line 4 def initialize name @name = name @objects = [] @trigger_method = nil end |
Instance Method Details
#add_object(object) ⇒ Object Also known as: add, <<
Add one or multiple object(s) to this Event.
21 22 23 24 25 26 27 28 29 |
# File 'lib/AdventureRL/Events/Event.rb', line 21 def add_object object [object].flatten.each do |obj| Helpers::Error.error( "Object `#{obj.inspect}:#{obj.class.name}' cannot be given", "to this Event `#{self.inspect}:#{self.class.name}'." ) unless (valid_object? obj) @objects << obj unless (@objects.include? obj) end end |
#get_name ⇒ Object
Returns the name of the Event.
11 12 13 |
# File 'lib/AdventureRL/Events/Event.rb', line 11 def get_name return @name end |
#get_objects ⇒ Object
Returns the objects that subscribed to this Event. (see #add_object)
16 17 18 |
# File 'lib/AdventureRL/Events/Event.rb', line 16 def get_objects return @objects end |
#on_trigger(&block) ⇒ Object
Pass a block, which will be called when this Event is triggered (see #trigger). The passed block takes an argument, which is a subscribed object.
42 43 44 45 46 47 |
# File 'lib/AdventureRL/Events/Event.rb', line 42 def on_trigger &block Helpers::Error.error( "Method #on_trigger needs a block to be passed." ) unless (block_given?) @trigger_method = block end |
#remove_object(object) ⇒ Object
Remove one or multiple object(s) from this Event.
34 35 36 37 38 |
# File 'lib/AdventureRL/Events/Event.rb', line 34 def remove_object object [object].flatten.each do |obj| @objects.delete obj end end |
#trigger(*args) ⇒ Object
The block defined with #on_trigger will be called for every subscribed object. Optionally, additional args arguments can be passed, which will be passed to the trigger method.
53 54 55 56 57 |
# File 'lib/AdventureRL/Events/Event.rb', line 53 def trigger *args get_objects.each do |object| @trigger_method.call object, *args end end |