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:   merge(page_items)               # array merged with existing client-side feed
}

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.



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

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.



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

def block
  @block
end

#groupObject (readonly)

Returns the value of attribute group.



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

def group
  @group
end

#valueObject (readonly)

Returns the value of attribute value.



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

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)


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

def always? = false

#deferred?Boolean

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

Returns:

  • (Boolean)


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

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)


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

def merge? = false

#optional?Boolean

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

Returns:

  • (Boolean)


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

def optional? = false

#resolveObject



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

def resolve
  block ? block.call : value
end