Class: Synthra::Functions::Registry
- Inherits:
-
Object
- Object
- Synthra::Functions::Registry
- Defined in:
- lib/synthra/functions/registry.rb
Overview
Registry for custom functions used in computed fields
Class Method Summary collapse
-
.call(name, context) ⇒ Object
Call a registered function.
-
.clear ⇒ void
Clear all registered functions.
-
.lookup(name) ⇒ Proc
Lookup a registered function.
-
.names ⇒ Array<Symbol>
List all registered function names.
-
.register(name, force: false) {|context| ... } ⇒ void
Register a custom function.
-
.registered?(name) ⇒ Boolean
Check if a function is registered.
-
.unregister(name) ⇒ Proc?
Unregister a specific function.
Class Method Details
.call(name, context) ⇒ Object
Call a registered function
54 55 56 57 |
# File 'lib/synthra/functions/registry.rb', line 54 def call(name, context) func = lookup(name) func.call(context) end |
.clear ⇒ void
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
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 |
.names ⇒ Array<Symbol>
List all registered function names
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
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
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
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 |