Class: Vedeu::Events

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Events

Parameters:

  • block (Proc)


6
7
8
9
10
# File 'lib/vedeu/support/events.rb', line 6

def initialize(&block)
  @handlers = Hash.new { |hash, key| hash[key] = { events: [] } }

  instance_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns [].

Returns:



61
62
63
# File 'lib/vedeu/support/events.rb', line 61

def method_missing(method, *args, &block)
  @self_before_instance_eval.send(method, *args, &block)
end

Instance Attribute Details

#handlersObject (readonly, private)

Returns the value of attribute handlers.



57
58
59
# File 'lib/vedeu/support/events.rb', line 57

def handlers
  @handlers
end

Instance Method Details

#add(object, &block) ⇒ Object

Returns [].

Parameters:

  • object
  • block (Proc)

Returns:



15
16
17
18
19
# File 'lib/vedeu/support/events.rb', line 15

def add(object, &block)
  @self_before_instance_eval = eval('self', block.binding)

  instance_eval(&block)
end

#event(name, opts = {}, &block) ⇒ Object

See Also:



22
23
24
25
# File 'lib/vedeu/support/events.rb', line 22

def event(name, opts = {}, &block)
  handlers[name][:events] << Event.new(block, opts)
  handlers[name]
end

#registeredArray

Returns a collection of the names of all the registered events.

Returns:

  • (Array)


39
40
41
# File 'lib/vedeu/support/events.rb', line 39

def registered
  handlers.keys
end

#resetObject

Remove all registered events. Used for testing purposes.

Returns:



51
52
53
# File 'lib/vedeu/support/events.rb', line 51

def reset
  @handlers = Hash.new { |hash, key| hash[key] = { events: [] } }
end

#trigger(name, *args) ⇒ Object

See Also:



44
45
46
# File 'lib/vedeu/support/events.rb', line 44

def trigger(name, *args)
  handlers[name][:events].each { |event| event.trigger(*args) }
end

#unevent(name) ⇒ Object

Unregisters the event by name, effectively deleting the associated events bound with it also.

Parameters:

  • name (Symbol)

Returns:



32
33
34
# File 'lib/vedeu/support/events.rb', line 32

def unevent(name)
  handlers.delete_if { |k, v| k == name }
end