Class: Inertia::ProtocolBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/inertia/protocol_builder.rb

Overview

Builds the Inertia page object for a rendered component.

ProtocolBuilder resolves the server-side props hash into the shape expected by the Inertia protocol. It applies partial reload filtering, evaluates lazy values, walks nested hashes and arrays, and collects protocol metadata such as deferredProps and onceProps.

Nested prop paths are tracked with dot notation, e.g. auth.user.name, so request headers can target either a top-level prop or a nested value.

Instance Method Summary collapse

Constructor Details

#initialize(component, props, context:) ⇒ ProtocolBuilder

Creates a new protocol builder.

Parameters:

  • component (String)

    the Inertia component name being rendered

  • props (Hash)

    the props provided by the controller

  • context (RequestContext)

    request-specific partial reload context



30
31
32
33
34
35
# File 'lib/inertia/protocol_builder.rb', line 30

def initialize(component, props, context:)
  @props = props
  @context = context

  @response = { component:, url: context.url, version: Frontend.version }
end

Instance Method Details

#callHash

Builds the page object hash.

Returns:

  • (Hash)

    the Inertia page object payload



40
41
42
43
44
45
46
47
48
49
# File 'lib/inertia/protocol_builder.rb', line 40

def call
  props = resolve_props
  props[:errors] = EMPTY_ERRORS unless props.has_key?(:errors)

  @response[:props] = props
  @response[:deferredProps] = @deferred_props_metadata if @deferred_props_metadata
  @response[:onceProps] = @once_props_metadata if @once_props_metadata

  @response
end