livetext 0.9.61 → 0.9.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d0ba9b287008c8827a9a1d30881507700dceffae0edd52e4dd5274b5fb4f004
4
- data.tar.gz: eb988c908dcdbbbff10710a4a62bd51cdd13b832a9121814150a484448d9c5f7
3
+ metadata.gz: 61c11b22070e565c5c25735ea1134d58249a5f441f0ed3c1f9eeb0f8bbf7c628
4
+ data.tar.gz: feff45c934b1ecb8757dcd1ea3fad4947e7383063faa9f3f68506dc73c99ff9e
5
5
  SHA512:
6
- metadata.gz: 1fcc995144794c8fac0b87c42a4cd8913d57bc31c6f26cc1bd564871366c1c3d260eda87966301d81776f1322fd2c22d453b807ffc06a90e4e1f4ceca3184598
7
- data.tar.gz: 40fe697a385ba6684df3746558674793766801933fcbd8b42766a9c2ff1121aebb75942898f32fe6184de3fb96ad2d6c9ecef980df2cf2bd8697c9f377123f08
6
+ metadata.gz: 6e9fa6aeec991337d703e223f20daef4b65b0d261be3cdcfbce99854ab263c0660e2288de725411ba3eaa29c1e8e65ae421dddd433c187e813b10b052f658f1b
7
+ data.tar.gz: f899d4de12921521b8d06adb35d10ed1f530cab3f9bc1d8c665ddb9bd7eb54b397d6bf7bc0a8686ecdfbd71a9aba18b470da0da38f2687d09ca03ba662b30f7b
@@ -1,7 +1,8 @@
1
1
  # FunctionCaller - Provides simple method-style access to Livetext functions
2
2
  class Livetext::FunctionCaller
3
- def initialize(function_registry)
3
+ def initialize(function_registry, api)
4
4
  @registry = function_registry
5
+ @api = api
5
6
  end
6
7
 
7
8
  # Dynamically handle method calls to function names
@@ -10,6 +11,9 @@ class Livetext::FunctionCaller
10
11
  function_name = name.to_s
11
12
  param = args.first || ""
12
13
 
14
+ # Set api on Livetext::Functions so all functions can access it
15
+ Livetext::Functions.api = @api
16
+
13
17
  @registry.call(function_name, param)
14
18
  rescue => e
15
19
  "[Error calling function #{function_name}: #{e.message}]"
@@ -28,6 +28,18 @@ class Livetext::FunctionRegistry
28
28
  elsif @builtin_functions[name_sym]
29
29
  call_function(name, @builtin_functions[name_sym], param)
30
30
  else
31
+ # Fall back to Livetext::Functions for backward compatibility
32
+ fobj = ::Livetext::Functions.new
33
+ if fobj.respond_to?(name_sym)
34
+ method = fobj.method(name_sym)
35
+ if method.parameters.empty?
36
+ result = fobj.send(name_sym)
37
+ else
38
+ result = fobj.send(name_sym, param)
39
+ end
40
+ return result.to_s if result
41
+ end
42
+
31
43
  "[Error evaluating $$#{name}(#{param})]"
32
44
  end
33
45
  end
@@ -9,6 +9,7 @@ class Livetext::Functions
9
9
 
10
10
  class << self
11
11
  attr_accessor :param # kill this?
12
+ attr_accessor :api
12
13
  end
13
14
 
14
15
  # Instance variables for accessing the Livetext instance and its variables
@@ -20,7 +20,7 @@ class Livetext::UserAPI
20
20
  @vars = live.vars
21
21
  @html = Livetext::HTML.new(self)
22
22
  @expander = Livetext::Expansion.new(live)
23
- @funcs = Livetext::FunctionCaller.new(live.function_registry)
23
+ @funcs = Livetext::FunctionCaller.new(live.function_registry, self)
24
24
  end
25
25
 
26
26
  def api
@@ -2,5 +2,5 @@
2
2
  # Defining VERSION
3
3
 
4
4
  class Livetext
5
- VERSION = "0.9.61"
5
+ VERSION = "0.9.62"
6
6
  end
@@ -4,7 +4,7 @@
4
4
  4 /<p>Directory: .*livetext.*</p>/
5
5
  5 <p>Platform: universal.x86_64-darwin22</p>
6
6
  6 <p>Ruby version: 2.6.10</p>
7
- 7 <p>Livetext version: 0.9.60</p>
7
+ 7 <p>Livetext version: 0.9.61</p>
8
8
  8 <p>Reverse of 'hello': olleh</p>
9
9
  9 <p>Square root of 16: 4</p>
10
10
  10 /<p>Random \(1-10\): \d+</p>/
@@ -29,3 +29,4 @@
29
29
  .end
30
30
 
31
31
  .test_simple_func_api
32
+
@@ -3,7 +3,7 @@
3
3
  3 Hostname: HAL9000
4
4
  4 /Platform: .*x86_64-darwin\d+/
5
5
  5 /Ruby Version: \d+\.\d+\.\d+/
6
- 6 Livetext Version: 0.9.60
6
+ 6 Livetext Version: 0.9.61
7
7
  7 </p>
8
8
  8
9
9
  9 <p>
data/test/unit/all.rb CHANGED
@@ -11,6 +11,7 @@ require_relative 'variable_manager'
11
11
  require_relative 'functions'
12
12
  require_relative 'function_registry'
13
13
  require_relative 'function_caller'
14
+ require_relative 'function_api_access'
14
15
  require_relative 'mixin_functions_class'
15
16
  require_relative 'formatter'
16
17
  require_relative 'formatter_component'
@@ -0,0 +1,82 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../../lib/livetext'
3
+
4
+ class TestFunctionAPIAccess < Minitest::Test
5
+ def setup
6
+ @live = Livetext.new
7
+ @live.vars.set(:View, "test_view")
8
+ @live.vars.set(:"post.id", "0001")
9
+ end
10
+
11
+ def test_scriptorium_image_scenario
12
+ # This test mimics the exact scenario from Scriptorium's image function
13
+ # Define the asset function directly in Livetext::Functions (like in Scriptorium)
14
+ Livetext::Functions.class_eval do
15
+ def asset(param)
16
+ # Get view and post information (exactly like in Scriptorium)
17
+ vname = self.class.api.vars.to_h[:View]
18
+ postid = self.class.api.vars.to_h[:"post.id"]
19
+ "assets/#{postid}/#{param}" # Simplified version of the asset function
20
+ end
21
+ end
22
+
23
+ # Test calling it through api.funcs (like the image function in Scriptorium)
24
+ result = @live.api.funcs.asset("test.jpg")
25
+
26
+ assert_equal "assets/0001/test.jpg", result
27
+ end
28
+
29
+ def test_function_can_access_api_vars
30
+ # Define a function that needs access to api.vars (like the asset function in Scriptorium)
31
+ Livetext::Functions.class_eval do
32
+ def test_asset_function(param)
33
+ # This mimics the asset function from Scriptorium that needs api.vars
34
+ vname = self.class.api.vars.to_h[:View]
35
+ postid = self.class.api.vars.to_h[:"post.id"]
36
+ "view=#{vname}, post=#{postid}, file=#{param}"
37
+ end
38
+ end
39
+
40
+ # Test calling it through api.funcs (like the image function in Scriptorium)
41
+ result = @live.api.funcs.test_asset_function("test.jpg")
42
+
43
+ assert_equal "view=test_view, post=0001, file=test.jpg", result
44
+ end
45
+
46
+ def test_function_can_access_api_vars_without_params
47
+ # Define a function that needs access to api.vars but takes no parameters
48
+ Livetext::Functions.class_eval do
49
+ def test_view_info
50
+ vname = self.class.api.vars.to_h[:View]
51
+ postid = self.class.api.vars.to_h[:"post.id"]
52
+ "view=#{vname}, post=#{postid}"
53
+ end
54
+ end
55
+
56
+ # Test calling it through api.funcs
57
+ result = @live.api.funcs.test_view_info()
58
+
59
+ assert_equal "view=test_view, post=0001", result
60
+ end
61
+
62
+ def test_function_handles_missing_api_gracefully
63
+ # Define a function that tries to access api but it's not set
64
+ Livetext::Functions.class_eval do
65
+ def test_no_api
66
+ if self.class.api
67
+ "api available"
68
+ else
69
+ "no api"
70
+ end
71
+ end
72
+ end
73
+
74
+ # Clear the api to test the fallback
75
+ Livetext::Functions.api = nil
76
+
77
+ # Test calling it through api.funcs - should still work because we set it in method_missing
78
+ result = @live.api.funcs.test_no_api()
79
+
80
+ assert_equal "api available", result
81
+ end
82
+ end
@@ -5,7 +5,8 @@ require_relative '../../lib/livetext/function_caller'
5
5
  class TestFunctionCaller < Minitest::Test
6
6
  def setup
7
7
  @registry = Livetext::FunctionRegistry.new
8
- @caller = Livetext::FunctionCaller.new(@registry)
8
+ @api = Object.new # Mock api object for testing
9
+ @caller = Livetext::FunctionCaller.new(@registry, @api)
9
10
  end
10
11
 
11
12
  def test_calls_builtin_functions
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: livetext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.61
4
+ version: 0.9.62
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hal Fulton
@@ -241,6 +241,7 @@ files:
241
241
  - test/unit/double.rb
242
242
  - test/unit/formatter.rb
243
243
  - test/unit/formatter_component.rb
244
+ - test/unit/function_api_access.rb
244
245
  - test/unit/function_caller.rb
245
246
  - test/unit/function_registry.rb
246
247
  - test/unit/functions.rb