Module: Ruact::ServerFunctions::Codegen::V2

Defined in:
lib/ruact/server_functions/codegen_v2.rb

Overview

Story 9.3 — renders a version-2 (route-driven) snapshot into the TS module. Each entry is an action targeting a real path + verb, emitted as ‘_makeServerFunction({ method, path, segments })` instead of v1’s ‘_makeRef(“<sym>”)`. Lives in its own module (nested in Ruact::ServerFunctions::Codegen) so the v1 singleton class stays within its size budget; render delegates here when `snapshot.version == 2`.

The output MUST stay byte-identical to the JS-side ‘renderV2` in `gem/vendor/javascript/vite-plugin-ruact/server-functions-codegen.mjs`; the parity test (“Story 9.3 — route-driven (v2) render + parity”) asserts this. Constants (ACTION_SIGNATURE, RUNTIME_IMPORT, REVALIDATE_REEXPORT, LINE_TERMINATORS, VALID_JS_IDENTIFIER) are reused from Ruact::ServerFunctions::Codegen via lexical scope.

Constant Summary collapse

HTTP_METHODS =

Verbs a v2 entry may carry (mirrors RouteSource::MUTATION_VERBS).

%w[POST PUT PATCH DELETE].to_set.freeze

Class Method Summary collapse

Class Method Details

.render(version, generated_at, functions) ⇒ String

Returns TS module bytes (single trailing newline).

Parameters:

  • version (Integer, String)
  • generated_at (String)
  • functions (Array<Hash>)

    route-derived entries.

Returns:

  • (String)

    TS module bytes (single trailing newline).

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruact/server_functions/codegen_v2.rb', line 32

def render(version, generated_at, functions)
  validate_functions!(functions)

  io = +""
  io << "// AUTO-GENERATED by vite-plugin-ruact (Story 9.3). DO NOT EDIT.\n"
  io << "// Source: Rails route table (version #{version})\n"
  io << "// Generated at: #{generated_at}\n"
  io << "import { _makeServerFunction } from #{RUNTIME_IMPORT};\n"

  if functions.empty?
    io << "\n// (no server functions exposed yet — add a non-GET route on a Ruact::Server controller)\n"
    io << "void _makeServerFunction;\n"
  else
    io << "\n"
    functions.each { |entry| io << render_export(entry) }
  end

  io << "\n"
  io << REVALIDATE_REEXPORT
  io
end