Class: Lich::Common::DownstreamHook

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

Overview

A class that manages downstream hooks for processing strings.

Class Method Summary collapse

Class Method Details

.add(name, action) ⇒ Boolean

Adds a new downstream hook with a given name and action.

Examples:

DownstreamHook.add("example_hook", Proc.new { |str| str.upcase })

Parameters:

  • name (String)

    the name of the hook

  • action (Proc)

    the action to be executed when the hook is triggered

Returns:

  • (Boolean)

    returns false if the action is not a Proc, otherwise returns nil



18
19
20
21
22
23
24
25
# File 'lib/common/downstreamhook.rb', line 18

def DownstreamHook.add(name, action)
  unless action.class == Proc
    echo "DownstreamHook: not a Proc (#{action})"
    return false
  end
  @@downstream_hook_sources[name] = (Script.current.name || "Unknown")
  @@downstream_hooks[name] = action
end

.hook_sourcesHash

Retrieves the sources of all registered downstream hooks.

Examples:

sources = DownstreamHook.hook_sources

Returns:

  • (Hash)

    a hash mapping hook names to their sources



85
86
87
# File 'lib/common/downstreamhook.rb', line 85

def DownstreamHook.hook_sources
  @@downstream_hook_sources
end

.listArray<String>

Lists all registered downstream hooks.

Examples:

hooks = DownstreamHook.list

Returns:

  • (Array<String>)

    an array of hook names



64
65
66
# File 'lib/common/downstreamhook.rb', line 64

def DownstreamHook.list
  @@downstream_hooks.keys.dup
end

.remove(name) ⇒ nil

Removes a downstream hook by its name.

Examples:

DownstreamHook.remove("example_hook")

Parameters:

  • name (String)

    the name of the hook to be removed

Returns:

  • (nil)

    returns nil



54
55
56
57
# File 'lib/common/downstreamhook.rb', line 54

def DownstreamHook.remove(name)
  @@downstream_hook_sources.delete(name)
  @@downstream_hooks.delete(name)
end

.run(server_string) ⇒ String?

Runs all registered downstream hooks on the provided server string.

Examples:

processed_string = DownstreamHook.run("example string")

Parameters:

  • server_string (String)

    the string to be processed by the hooks

Returns:

  • (String, nil)

    the processed string or nil if the input is nil

Raises:

  • (StandardError)

    if an error occurs during the execution of a hook



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/common/downstreamhook.rb', line 34

def DownstreamHook.run(server_string)
  for key in @@downstream_hooks.keys
    return nil if server_string.nil?
    begin
      server_string = @@downstream_hooks[key].call(server_string.dup) if server_string.is_a?(String)
    rescue
      @@downstream_hooks.delete(key)
      respond "--- Lich: DownstreamHook: #{$!}"
      respond $!.backtrace.first
    end
  end
  return server_string
end

.sourcesString

Displays the sources of all registered downstream hooks in a table format.

Examples:

DownstreamHook.sources

Returns:

  • (String)

    a formatted string representation of the hook sources



73
74
75
76
77
78
# File 'lib/common/downstreamhook.rb', line 73

def DownstreamHook.sources
  info_table = Terminal::Table.new :headings => ['Hook', 'Source'],
                                   :rows     => @@downstream_hook_sources.to_a,
                                   :style    => { :all_separators => true }
  Lich::Messaging.mono(info_table.to_s)
end