Class: Livetext::FunctionRegistry
- Defined in:
- lib/livetext/function_registry.rb
Overview
Function Registry - Unified function management for Livetext
Instance Method Summary collapse
- #call(name, param) ⇒ Object
- #function_exists?(name) ⇒ Boolean
- #get_function_info(name) ⇒ Object
-
#initialize ⇒ FunctionRegistry
constructor
A new instance of FunctionRegistry.
- #list_functions ⇒ Object
- #register_builtin(name, function) ⇒ Object
- #register_user(name, function, source: :inline, filename: nil) ⇒ Object
Constructor Details
#initialize ⇒ FunctionRegistry
Returns a new instance of FunctionRegistry.
3 4 5 6 7 8 9 |
# File 'lib/livetext/function_registry.rb', line 3 def initialize @user_functions = {} @builtin_functions = {} @metadata = {} register_builtin_functions # puts "DEBUG: Registered #{@builtin_functions.size} builtin functions" if ENV['DEBUG'] end |
Instance Method Details
#call(name, param) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/livetext/function_registry.rb', line 22 def call(name, param) # Convert name to symbol for consistent lookup name_sym = name.to_sym if @user_functions[name_sym] call_function(name, @user_functions[name_sym], param) elsif @builtin_functions[name_sym] call_function(name, @builtin_functions[name_sym], param) else # Fall back to Livetext::Functions for backward compatibility fobj = ::Livetext::Functions.new if fobj.respond_to?(name_sym) method = fobj.method(name_sym) if method.parameters.empty? result = fobj.send(name_sym) else result = fobj.send(name_sym, param) end return result.to_s if result end "[Error evaluating $$#{name}(#{param})]" end end |
#function_exists?(name) ⇒ Boolean
67 68 69 70 |
# File 'lib/livetext/function_registry.rb', line 67 def function_exists?(name) name_sym = name.to_sym @user_functions.key?(name_sym) || @builtin_functions.key?(name_sym) end |
#get_function_info(name) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/livetext/function_registry.rb', line 54 def get_function_info(name) name_sym = name.to_sym = @metadata[name_sym] return nil unless { name: name.to_s, source: get_source(name), filename: [:filename], type: @user_functions.key?(name_sym) ? :user : :builtin } end |
#list_functions ⇒ Object
47 48 49 50 51 52 |
# File 'lib/livetext/function_registry.rb', line 47 def list_functions result = [] @user_functions.each { |name, _| result << { name: name.to_s, source: get_source(name) } } @builtin_functions.each { |name, _| result << { name: name.to_s, source: get_source(name) } } result.sort_by { |f| f[:name] } end |
#register_builtin(name, function) ⇒ Object
17 18 19 20 |
# File 'lib/livetext/function_registry.rb', line 17 def register_builtin(name, function) @builtin_functions[name] = function @metadata[name] = { source: :builtin, filename: "builtin" } end |
#register_user(name, function, source: :inline, filename: nil) ⇒ Object
11 12 13 14 15 |
# File 'lib/livetext/function_registry.rb', line 11 def register_user(name, function, source: :inline, filename: nil) name_sym = name.to_sym @user_functions[name_sym] = function @metadata[name_sym] = { source: source, filename: filename } end |