Module: Vedeu::Events::Aliases

Extended by:
Aliases
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.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.absent?(variable) ⇒ Boolean Originally defined in module Common

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 a boolean indicating whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

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

Add events by name to the alias name group. When an alias is triggered, all the events stored in the group are also triggered.

Examples:

Vedeu.bind_alias(alias_name, event_name)

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>>)


26
27
28
# File 'lib/vedeu/events/aliases.rb', line 26

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

.demodulize(klass) ⇒ String Originally defined in module Common

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.

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)

.empty?Boolean

Return a boolean indicating whether the storage is empty.

Returns:

  • (Boolean)


34
35
36
# File 'lib/vedeu/events/aliases.rb', line 34

def empty?
  storage.empty?
end

.find(alias_name) ⇒ Array<Symbol>

Parameters:

  • alias_name (Symbol)

Returns:

  • (Array<Symbol>)


40
41
42
# File 'lib/vedeu/events/aliases.rb', line 40

def find(alias_name)
  storage[alias_name]
end

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

Returns:

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


99
100
101
# File 'lib/vedeu/events/aliases.rb', line 99

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

.present?(variable) ⇒ Boolean Originally defined in module Common

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 a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

.registered?(alias_name) ⇒ Boolean

Returns a boolean indicating whether the alias is registered.

Parameters:

  • alias_name (Symbol)

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/vedeu/events/aliases.rb', line 48

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

  storage.include?(alias_name)
end

.resetHash<Symbol => Array<Symbol>> Also known as: reset!

Returns:

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


72
73
74
# File 'lib/vedeu/events/aliases.rb', line 72

def reset
  @storage = in_memory
end

.snake_case(name) ⇒ String Originally defined in module Common

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.

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (String)

.storageHash<Symbol => Array<Symbol>>

Access to the storage for this repository.

Returns:

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


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

def storage
  @storage ||= in_memory
end

.trigger(alias_name, *args) ⇒ FalseClass|Array<void>|void

Parameters:

  • alias_name (Symbol)
  • args (void)

Returns:

  • (FalseClass|Array<void>|void)


87
88
89
90
91
92
93
94
# File 'lib/vedeu/events/aliases.rb', line 87

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

  find(alias_name).map do |event_name|
    Vedeu.log(type: :debug, message: "#{event_name}".freeze)
    Vedeu::Events::Trigger.trigger(event_name, *args)
  end
end

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

Remove an alias by name. The alias name group is destroyed,

but events stored within this alias are not.

Examples:

Vedeu.unbind_alias(alias_name)

Parameters:

  • alias_name (Symbol)

    The name of the alias.

Returns:

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


62
63
64
65
66
67
68
# File 'lib/vedeu/events/aliases.rb', line 62

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

  storage.delete(alias_name)
  storage
end

Instance Method Details

#absent?(variable) ⇒ Boolean Originally defined in module Common

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 a boolean indicating whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

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

Add events by name to the alias name group. When an alias is triggered, all the events stored in the group are also triggered.

Examples:

Vedeu.bind_alias(alias_name, event_name)

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>>)


26
27
28
# File 'lib/vedeu/events/aliases.rb', line 26

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

#demodulize(klass) ⇒ String Originally defined in module Common

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.

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)

#empty?Boolean

Return a boolean indicating whether the storage is empty.

Returns:

  • (Boolean)


34
35
36
# File 'lib/vedeu/events/aliases.rb', line 34

def empty?
  storage.empty?
end

#find(alias_name) ⇒ Array<Symbol>

Parameters:

  • alias_name (Symbol)

Returns:

  • (Array<Symbol>)


40
41
42
# File 'lib/vedeu/events/aliases.rb', line 40

def find(alias_name)
  storage[alias_name]
end

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

Returns:

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


99
100
101
# File 'lib/vedeu/events/aliases.rb', line 99

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

#present?(variable) ⇒ Boolean Originally defined in module Common

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 a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#registered?(alias_name) ⇒ Boolean

Returns a boolean indicating whether the alias is registered.

Parameters:

  • alias_name (Symbol)

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/vedeu/events/aliases.rb', line 48

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

  storage.include?(alias_name)
end

#resetHash<Symbol => Array<Symbol>> Also known as: reset!

Returns:

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


72
73
74
# File 'lib/vedeu/events/aliases.rb', line 72

def reset
  @storage = in_memory
end

#snake_case(name) ⇒ String Originally defined in module Common

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.

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (String)

#storageHash<Symbol => Array<Symbol>>

Access to the storage for this repository.

Returns:

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


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

def storage
  @storage ||= in_memory
end

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

Parameters:

  • alias_name (Symbol)
  • args (void)

Returns:

  • (FalseClass|Array<void>|void)


87
88
89
90
91
92
93
94
# File 'lib/vedeu/events/aliases.rb', line 87

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

  find(alias_name).map do |event_name|
    Vedeu.log(type: :debug, message: "#{event_name}".freeze)
    Vedeu::Events::Trigger.trigger(event_name, *args)
  end
end

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

Remove an alias by name. The alias name group is destroyed,

but events stored within this alias are not.

Examples:

Vedeu.unbind_alias(alias_name)

Parameters:

  • alias_name (Symbol)

    The name of the alias.

Returns:

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


62
63
64
65
66
67
68
# File 'lib/vedeu/events/aliases.rb', line 62

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

  storage.delete(alias_name)
  storage
end