Class: Sinatra::Inertia::Prop

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

Overview

Wrappers that mark props with Inertia v2 transport modes. They are plain value objects with a Proc payload; resolution happens inside PropsResolver (which knows whether the current request is a partial reload, what component it targets, what fields are requested, etc.).

Usage:

inertia 'Page', props: {
  todos:  -> { Todo.all },                # plain lazy: included by default, resolved on demand
  stats:  Inertia.defer { compute_stats } # excluded from initial response, fetched in 2nd request
  csrf:   Inertia.always { csrf_token }   # included even on partial reloads that omit it
  filter: Inertia.optional { params[:f] } # only included when explicitly requested via partial
  feed:   Inertia.merge(page_items)       # array merged with existing client-side feed
  once:   Inertia.once { current_time }   # delivered exactly once; subsequent visits suppress
}

Direct Known Subclasses

AlwaysProp, DeferredProp, LazyProp, MergeProp, OptionalProp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block: nil, value: nil, group: 'default') ⇒ Prop

Returns a new instance of Prop.



22
23
24
25
26
# File 'lib/sinatra/inertia/deferred.rb', line 22

def initialize(block: nil, value: nil, group: 'default')
  @block = block
  @value = value
  @group = group
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



20
21
22
# File 'lib/sinatra/inertia/deferred.rb', line 20

def block
  @block
end

#groupObject (readonly)

Returns the value of attribute group.



20
21
22
# File 'lib/sinatra/inertia/deferred.rb', line 20

def group
  @group
end

#valueObject (readonly)

Returns the value of attribute value.



20
21
22
# File 'lib/sinatra/inertia/deferred.rb', line 20

def value
  @value
end

Instance Method Details

#always?Boolean

Should this prop be included in every response (including partials that did not request it)?

Returns:

  • (Boolean)


34
# File 'lib/sinatra/inertia/deferred.rb', line 34

def always? = false

#deferred?Boolean

Is the value sent in the initial response, or deferred to a second roundtrip?

Returns:

  • (Boolean)


38
# File 'lib/sinatra/inertia/deferred.rb', line 38

def deferred? = false

#merge?Boolean

Should arrays returned by this prop be merged with the client’s existing array (Inertia 2 merge semantics)?

Returns:

  • (Boolean)


46
# File 'lib/sinatra/inertia/deferred.rb', line 46

def merge? = false

#once?Boolean

Once-only delivery (cleared from session/state after first emission).

Returns:

  • (Boolean)


49
# File 'lib/sinatra/inertia/deferred.rb', line 49

def once? = false

#optional?Boolean

Is this prop only included when explicitly requested via X-Inertia-Partial-Data?

Returns:

  • (Boolean)


42
# File 'lib/sinatra/inertia/deferred.rb', line 42

def optional? = false

#resolveObject



28
29
30
# File 'lib/sinatra/inertia/deferred.rb', line 28

def resolve
  block ? block.call : value
end