Class: Sinatra::Inertia::Response
- Inherits:
-
Object
- Object
- Sinatra::Inertia::Response
- Defined in:
- lib/sinatra/inertia/response.rb
Overview
Builds the Inertia page object from a (component, props, request) tuple, applying partial-reload selection, deferred-prop excision, merge/once metadata, encrypted history flags, etc.
The output is a Hash that gets serialized as JSON for X-Inertia responses, or interpolated into the layout’s ‘data-page` attribute for full HTML responses.
Instance Attribute Summary collapse
-
#clear_history ⇒ Object
readonly
Returns the value of attribute clear_history.
-
#component ⇒ Object
readonly
Returns the value of attribute component.
-
#encrypt_history ⇒ Object
readonly
Returns the value of attribute encrypt_history.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#props ⇒ Object
readonly
Returns the value of attribute props.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#shared ⇒ Object
readonly
Returns the value of attribute shared.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#initialize(component:, props:, request:, version:, url: nil, encrypt_history: false, clear_history: false, shared: {}, errors: nil) ⇒ Response
constructor
A new instance of Response.
- #to_h ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(component:, props:, request:, version:, url: nil, encrypt_history: false, clear_history: false, shared: {}, errors: nil) ⇒ Response
Returns a new instance of Response.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sinatra/inertia/response.rb', line 18 def initialize(component:, props:, request:, version:, url: nil, encrypt_history: false, clear_history: false, shared: {}, errors: nil) @component = component @props = props || {} @request = request @version = version.to_s @url = url || request.fullpath @encrypt_history = encrypt_history @clear_history = clear_history @shared = shared || {} @errors = errors end |
Instance Attribute Details
#clear_history ⇒ Object (readonly)
Returns the value of attribute clear_history.
15 16 17 |
# File 'lib/sinatra/inertia/response.rb', line 15 def clear_history @clear_history end |
#component ⇒ Object (readonly)
Returns the value of attribute component.
15 16 17 |
# File 'lib/sinatra/inertia/response.rb', line 15 def component @component end |
#encrypt_history ⇒ Object (readonly)
Returns the value of attribute encrypt_history.
15 16 17 |
# File 'lib/sinatra/inertia/response.rb', line 15 def encrypt_history @encrypt_history end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
15 16 17 |
# File 'lib/sinatra/inertia/response.rb', line 15 def errors @errors end |
#props ⇒ Object (readonly)
Returns the value of attribute props.
15 16 17 |
# File 'lib/sinatra/inertia/response.rb', line 15 def props @props end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
15 16 17 |
# File 'lib/sinatra/inertia/response.rb', line 15 def request @request end |
#shared ⇒ Object (readonly)
Returns the value of attribute shared.
15 16 17 |
# File 'lib/sinatra/inertia/response.rb', line 15 def shared @shared end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
15 16 17 |
# File 'lib/sinatra/inertia/response.rb', line 15 def url @url end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
15 16 17 |
# File 'lib/sinatra/inertia/response.rb', line 15 def version @version end |
Instance Method Details
#to_h ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sinatra/inertia/response.rb', line 32 def to_h merged_props = deep_merge(shared, props) merged_props = merged_props.merge(errors: errors) if errors partial = partial_request? partial_data = partial_data_keys partial_except = partial_except_keys reset = reset_keys resolved = {} deferred_groups = {} merge_keys = [] # NOTE: avoid block-based iteration here. On Opal, when this # method runs as `async function` (because `await_if_promise` # awaits a JS Promise), inner blocks like `Hash#each { ... }` # do not natively suspend the outer function until the block # body's awaits complete. A plain index-based loop preserves # ordering and lets each iteration's `__await__` properly # gate the next. keys = merged_props.keys i = 0 while i < keys.length key = keys[i] value = merged_props[key] k = key.to_sym included, materialized = decide(value, k, partial, partial_data, partial_except) if included resolved[k] = await_if_promise(materialized) if value.is_a?(Prop) && value.merge? && !reset.include?(k) merge_keys << k.to_s end elsif value.is_a?(Prop) && value.deferred? (deferred_groups[value.group] ||= []) << k.to_s end i += 1 end page = { component: component, props: resolved, url: url, version: version } page[:encryptHistory] = true if encrypt_history page[:clearHistory] = true if clear_history page[:deferredProps] = deferred_groups unless deferred_groups.empty? page[:mergeProps] = merge_keys unless merge_keys.empty? page end |
#to_json ⇒ Object
83 |
# File 'lib/sinatra/inertia/response.rb', line 83 def to_json(*) = to_h.to_json |