Class: Synthra::Functions::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/synthra/functions/registry.rb

Overview

Registry for custom functions used in computed fields

Class Method Summary collapse

Class Method Details

.call(name, context) ⇒ Object

Call a registered function

Parameters:

  • name (Symbol)

    function name

  • context (Context)

    generation context

Returns:

  • (Object)

    result



54
55
56
57
# File 'lib/synthra/functions/registry.rb', line 54

def call(name, context)
  func = lookup(name)
  func.call(context)
end

.clearvoid

This method returns an undefined value.

Clear all registered functions



85
86
87
# File 'lib/synthra/functions/registry.rb', line 85

def clear
  @mutex.synchronize { @functions.clear }
end

.lookup(name) ⇒ Proc

Lookup a registered function

Parameters:

  • name (Symbol)

    function name

Returns:

  • (Proc)

Raises:



39
40
41
42
43
44
45
# File 'lib/synthra/functions/registry.rb', line 39

def lookup(name)
  @mutex.synchronize do
    @functions.fetch(name.to_sym) do
      raise MissingFunctionError, name
    end
  end
end

.namesArray<Symbol>

List all registered function names

Returns:

  • (Array<Symbol>)


76
77
78
# File 'lib/synthra/functions/registry.rb', line 76

def names
  @mutex.synchronize { @functions.keys }
end

.register(name, force: false) {|context| ... } ⇒ void

This method returns an undefined value.

Register a custom function

Parameters:

  • name (Symbol)

    function name

  • force (Boolean) (defaults to: false)

    allow overwriting existing function

Yields:

  • (context)

    block that computes the value

Raises:

  • (ArgumentError)

    if function exists and force is false



21
22
23
24
25
26
27
28
29
30
# File 'lib/synthra/functions/registry.rb', line 21

def register(name, force: false, &block)
  @mutex.synchronize do
    key = name.to_sym
    if @functions.key?(key) && !force
      raise ArgumentError, "Function '#{name}' already registered. Use force: true to override."
    end

    @functions[key] = block
  end
end

.registered?(name) ⇒ Boolean

Check if a function is registered

Parameters:

  • name (Symbol)

    function name

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/synthra/functions/registry.rb', line 65

def registered?(name)
  @mutex.synchronize do
    @functions.key?(name.to_sym)
  end
end

.unregister(name) ⇒ Proc?

Unregister a specific function

Parameters:

  • name (Symbol)

    function name

Returns:

  • (Proc, nil)

    the removed function



95
96
97
98
99
# File 'lib/synthra/functions/registry.rb', line 95

def unregister(name)
  @mutex.synchronize do
    @functions.delete(name.to_sym)
  end
end