Module: Vedeu::Events::Aliases

Extended by:
Aliases, Repositories::Storage
Includes:
Common
Included in:
Aliases
Defined in:
lib/vedeu/events/aliases.rb

Overview

Allows the storing of event aliases. Each alias can contain multiple event names.

Instance Method Summary collapse

Methods included from Repositories::Storage

reset!, storage

Methods included from Common

#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?

Instance Method Details

#bind_alias(alias_name, event_name) ⇒ Hash<Symbol => Array<Symbol>> Also known as: add

Parameters:

  • alias_name (Symbol)

    The name of the alias. This can represent a single event or group of events.

  • event_name (Symbol)

    The name of the event to bind to the alias. When the alias is triggered, all events bound will also be triggered.

Returns:

  • (Hash<Symbol => Array<Symbol>>)


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

def bind_alias(alias_name, event_name)
  storage[alias_name] << event_name
end

#empty?Boolean

Return a boolean indicating whether the storage is empty.

Returns:



31
32
33
# File 'lib/vedeu/events/aliases.rb', line 31

def empty?
  storage.empty?
end

#find(alias_name) ⇒ Array<Symbol>

Parameters:

  • alias_name (Symbol)

Returns:

  • (Array<Symbol>)


37
38
39
# File 'lib/vedeu/events/aliases.rb', line 37

def find(alias_name)
  storage[alias_name]
end

#in_memoryHash<Symbol => Array<Symbol>> (private)

Returns:

  • (Hash<Symbol => Array<Symbol>>)


80
81
82
# File 'lib/vedeu/events/aliases.rb', line 80

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

#registered?(alias_name) ⇒ Boolean

Returns a boolean indicating whether the alias is registered.

Parameters:

  • alias_name (Symbol)

Returns:



45
46
47
48
49
# File 'lib/vedeu/events/aliases.rb', line 45

def registered?(alias_name)
  return false if empty? || absent?(alias_name)

  storage.include?(alias_name)
end

#trigger(alias_name, *args) ⇒ Boolean|Array<void>|void

Parameters:

  • alias_name (Symbol)
  • args (void)

Returns:



66
67
68
69
70
71
72
73
74
75
# File 'lib/vedeu/events/aliases.rb', line 66

def trigger(alias_name, *args)
  return [] unless registered?(alias_name)

  find(alias_name).map do |event_name|
    Vedeu.log(type:    :event,
              message: "Triggering: '#{event_name}' from alias " \
                       "'#{alias_name}'")
    Vedeu::Events::Trigger.trigger(event_name, *args)
  end
end

#unbind_alias(alias_name) ⇒ Boolean|Hash<Symbol => Array<Symbol>> Also known as: remove

Parameters:

  • alias_name (Symbol)

    The name of the alias.

Returns:

  • (Boolean|Hash<Symbol => Array<Symbol>>)


54
55
56
57
58
59
60
# File 'lib/vedeu/events/aliases.rb', line 54

def unbind_alias(alias_name)
  return false if empty?
  return false unless registered?(alias_name)

  storage.delete(alias_name)
  storage
end