Class: Livetext::FunctionCaller

Inherits:
Object
  • Object
show all
Defined in:
lib/livetext/function_caller.rb

Overview

FunctionCaller - Provides simple method-style access to Livetext functions

Instance Method Summary collapse

Constructor Details

#initialize(function_registry, api) ⇒ FunctionCaller

Returns a new instance of FunctionCaller.



3
4
5
6
# File 'lib/livetext/function_caller.rb', line 3

def initialize(function_registry, api)
  @registry = function_registry
  @api = api
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Dynamically handle method calls to function names



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/livetext/function_caller.rb', line 9

def method_missing(name, *args)
  # Convert method name to string and call the function registry
  function_name = name.to_s
  param = args.first || ""
  
  # Set api on Livetext::Functions so all functions can access it
  Livetext::Functions.api = @api
  
  @registry.call(function_name, param)
rescue => e
  "[Error calling function #{function_name}: #{e.message}]"
end

Instance Method Details

#exists?(name) ⇒ Boolean

Check if a specific function exists

Returns:

  • (Boolean)


33
34
35
# File 'lib/livetext/function_caller.rb', line 33

def exists?(name)
  @registry.function_exists?(name.to_s)
end

#listObject

List all available functions



28
29
30
# File 'lib/livetext/function_caller.rb', line 28

def list
  @registry.list_functions.map { |f| f[:name] }
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Check if a function exists

Returns:

  • (Boolean)


23
24
25
# File 'lib/livetext/function_caller.rb', line 23

def respond_to_missing?(name, include_private = false)
  @registry.function_exists?(name.to_s) || super
end