Class: TurboBoost::Commands::State
- Inherits:
-
Object
- Object
- TurboBoost::Commands::State
- Includes:
- Enumerable
- Defined in:
- lib/turbo_boost/commands/state.rb
Overview
Class that encapsulates all the various forms of state.
page- Client-side transient page state used for rendering remembered element attributesnow- Server-side state for the current render only (discarded after rendering)signed- Server-side state that persists across renders (state that was used for the last server-side render)unsigned- Client-side state (optimistic client-side changes)current- Combined server-side state (signed + now)all- All state except unsigned (signed + now + page)
Instance Attribute Summary collapse
-
#now ⇒ HashWithIndifferentAccess
readonly
Server-side state for the current render only (similar to flash.now).
-
#page ⇒ HashWithIndifferentAccess
readonly
Client-side transient page state used for rendering remembered element attributes.
-
#signed ⇒ TurboBoost::Commands::StateStore
readonly
Server-side state that persists across renders This is the state that was used for the last server-side render (untampered by the client).
-
#unsigned ⇒ HashWithIndifferentAccess
(also: #optimistic)
readonly
Client-side state (optimistic client-side changes).
Instance Method Summary collapse
-
#all ⇒ HashWithIndifferentAccess
All state except unsigned (page + current).
-
#cache_key ⇒ Object
Returns a cache key representing "all" state.
-
#current ⇒ HashWithIndifferentAccess
Combined server-side state (signed + now).
-
#initialize(payload = {}) ⇒ State
constructor
A new instance of State.
- #tag_options(options = {}) ⇒ Object
-
#to_json ⇒ String
A JSON representation of state that can be sent to the client.
Constructor Details
#initialize(payload = {}) ⇒ State
Returns a new instance of State.
18 19 20 21 22 23 24 25 26 |
# File 'lib/turbo_boost/commands/state.rb', line 18 def initialize(payload = {}) payload = payload.respond_to?(:to_unsafe_h) ? payload.to_unsafe_h : payload.to_h payload = payload.with_indifferent_access @now = {}.with_indifferent_access @page = payload.fetch(:page, {}).with_indifferent_access @signed = TurboBoost::Commands::StateStore.new(payload.fetch(:signed, {})) @unsigned = payload.fetch(:unsigned, {}).with_indifferent_access end |
Instance Attribute Details
#now ⇒ HashWithIndifferentAccess (readonly)
Discarded after rendering
Server-side state for the current render only (similar to flash.now)
35 36 37 |
# File 'lib/turbo_boost/commands/state.rb', line 35 def now @now end |
#page ⇒ HashWithIndifferentAccess (readonly)
Client-side transient page state used for rendering remembered element attributes
30 31 32 |
# File 'lib/turbo_boost/commands/state.rb', line 30 def page @page end |
#signed ⇒ TurboBoost::Commands::StateStore (readonly)
Server-side state that persists across renders This is the state that was used for the last server-side render (untampered by the client)
40 41 42 |
# File 'lib/turbo_boost/commands/state.rb', line 40 def signed @signed end |
#unsigned ⇒ HashWithIndifferentAccess (readonly) Also known as: optimistic
There is a hook on Command instances to resolve state Command#resolve_state,
where Command authors can determine how to properly handle optimistic client-side state.
Client-side state (optimistic client-side changes)
49 50 51 |
# File 'lib/turbo_boost/commands/state.rb', line 49 def unsigned @unsigned end |
Instance Method Details
#all ⇒ HashWithIndifferentAccess
All state except unsigned (page + current).
62 63 64 |
# File 'lib/turbo_boost/commands/state.rb', line 62 def all page.merge current end |
#cache_key ⇒ Object
Returns a cache key representing "all" state
67 68 69 |
# File 'lib/turbo_boost/commands/state.rb', line 67 def cache_key "TurboBoost::Commands::State/#{Digest::MD5.base64digest(all.to_s)}" end |
#current ⇒ HashWithIndifferentAccess
Combined server-side state (signed + now)
54 55 56 |
# File 'lib/turbo_boost/commands/state.rb', line 54 def current signed.to_h.merge now end |
#tag_options(options = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/turbo_boost/commands/state.rb', line 82 def ( = {}) return unless .is_a?(Hash) = .deep_symbolize_keys return unless .key?(:turbo_boost) config = .delete(:turbo_boost) return unless config.is_a?(Hash) attributes = config[:remember] return if attributes.blank? attributes = begin attributes.is_a?(Array) ? attributes : JSON.parse(attributes.to_s) rescue raise TurboBoost::Commands::StateError, "Invalid `turbo_boost` options! `attributes` must be an Array of attributes to remember!" end attributes ||= [] attributes.map!(&:to_s).uniq! return if attributes.blank? if [:id].blank? raise TurboBoost::Commands::StateError, "An `id` attribute is required for remembering state!" end [:aria] ||= {} [:data] ||= {} [:data][:turbo_boost_state_attributes] = attributes.to_json attributes.each do |name| value = page.dig([:id], name) case name in String if name.start_with?("aria-") then [:aria][name.delete_prefix("aria-").to_sym] = value in String if name.start_with?("data-") then [:data][name.delete_prefix("data-").to_sym] = value else [name.to_sym] = value end end end |
#to_json ⇒ String
A JSON representation of state that can be sent to the client
Includes the following keys:
signed- The signed state (String)unsigned- The unsigned state (Hash)
78 79 80 |
# File 'lib/turbo_boost/commands/state.rb', line 78 def to_json {signed: signed.to_sgid_param, unsigned: signed.to_h}.to_json(camelize: false) end |