Class: LibSL::EventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/events.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler, type = :all) ⇒ EventHandler

New event handler

Parameters:

  • handler (Proc)

    Called to handle an event when it is fired

  • type (Symbol/Array) (defaults to: :all)

    (optional) The type(s) of event to handle



9
10
11
12
# File 'lib/events.rb', line 9

def initialize(handler, type=:all)
	@handler = handler
	@types = [*type]
end

Instance Attribute Details

#typesObject

Returns the value of attribute types.



4
5
6
# File 'lib/events.rb', line 4

def types
  @types
end

Instance Method Details

#accept?(type) ⇒ Boolean

Check if this handler can handle one specific event

Parameters:

  • type (Symbol)

    Event type

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
# File 'lib/events.rb', line 16

def accept?(type)
	if @types.include? :all
		return true
	elsif @types.include? type
		return true
	else
		return false
	end
end

#handle(type, *args) ⇒ Object

Handle the event

Parameters:

  • type (Symbol)

    Event type

  • args (args)

    (optional) Arguments passed to the handler



29
30
31
# File 'lib/events.rb', line 29

def handle(type, *args)
	@handler.call(type, *args)
end