Module: Vedeu::Events

Extended by:
Events
Includes:
Repository
Included in:
Events
Defined in:
lib/vedeu/repositories/events.rb

Overview

Provides a mechanism for storing and retrieving events by name. A single name can contain many events. Also, an event can trigger other events.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Repository

#all, #find, #missing_required, #registered, #registered?, #reset, #storage, #validate_attributes!

Class Method Details

.add(name, opts = {}, &block) ⇒ Object Also known as: event

See Also:



11
12
13
14
15
16
17
18
# File 'lib/vedeu/repositories/events.rb', line 11

def add(name, opts = {}, &block)
  Vedeu.log("Registering event: '#{name}'")

  options = opts.merge!({ event_name: name })

  storage[name][:events] << Event.new(block, options)
  storage[name]
end

.in_memoryHash (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 an empty collection ready for the storing of events by name with associated event instance.

Returns:

  • (Hash)


52
53
54
# File 'lib/vedeu/repositories/events.rb', line 52

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

.remove(name) ⇒ Object Also known as: unevent

See Also:



22
23
24
25
26
27
28
# File 'lib/vedeu/repositories/events.rb', line 22

def remove(name)
  return false unless registered?(name)

  storage.delete(name) { false }

  true
end

.use(name, *args) ⇒ Object Also known as: trigger

See Also:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vedeu/repositories/events.rb', line 32

def use(name, *args)
  results = storage[name][:events].map { |event| event.trigger(*args) }

  if results.one?
    results.first

  else
    results

  end
end

Instance Method Details

#add(name, opts = {}, &block) ⇒ Object Also known as: event

See Also:



11
12
13
14
15
16
17
18
# File 'lib/vedeu/repositories/events.rb', line 11

def add(name, opts = {}, &block)
  Vedeu.log("Registering event: '#{name}'")

  options = opts.merge!({ event_name: name })

  storage[name][:events] << Event.new(block, options)
  storage[name]
end

#in_memoryHash (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 an empty collection ready for the storing of events by name with associated event instance.

Returns:

  • (Hash)


52
53
54
# File 'lib/vedeu/repositories/events.rb', line 52

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

#remove(name) ⇒ Object Also known as: unevent

See Also:



22
23
24
25
26
27
28
# File 'lib/vedeu/repositories/events.rb', line 22

def remove(name)
  return false unless registered?(name)

  storage.delete(name) { false }

  true
end

#use(name, *args) ⇒ Object Also known as: trigger

See Also:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vedeu/repositories/events.rb', line 32

def use(name, *args)
  results = storage[name][:events].map { |event| event.trigger(*args) }

  if results.one?
    results.first

  else
    results

  end
end