Class: TurboBoost::Commands::State

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/turbo_boost/commands/state.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store = nil, provisional: false) ⇒ State

Returns a new instance of State.



12
13
14
15
16
# File 'lib/turbo_boost/commands/state.rb', line 12

def initialize(store = nil, provisional: false)
  @store = store || ActiveSupport::Cache::MemoryStore.new(expires_in: 1.day, size: 16.kilobytes)
  @store.cleanup
  @provisional = provisional
end

Class Method Details

.from_sgid_param(sgid) ⇒ Object



7
8
9
# File 'lib/turbo_boost/commands/state.rb', line 7

def from_sgid_param(sgid)
  new URI::UID.from_sgid(sgid, for: name)&.decode
end

Instance Method Details

#[]Object



53
54
55
# File 'lib/turbo_boost/commands/state.rb', line 53

def [](...)
  read(...)
end

#[]=Object



57
58
59
# File 'lib/turbo_boost/commands/state.rb', line 57

def []=(...)
  write(...)
end

#cache_keyObject



45
46
47
# File 'lib/turbo_boost/commands/state.rb', line 45

def cache_key
  "TurboBoost::Commands::State/#{Digest::SHA2.base64digest(to_json)}"
end

#dig(*keys) ⇒ Object



21
22
23
# File 'lib/turbo_boost/commands/state.rb', line 21

def dig(*keys)
  to_h.with_indifferent_access.dig(*keys)
end

#eachObject



30
31
32
# File 'lib/turbo_boost/commands/state.rb', line 30

def each
  data.keys.each { |key| yield(key, self[key]) }
end

#merge!(hash = {}) ⇒ Object



25
26
27
28
# File 'lib/turbo_boost/commands/state.rb', line 25

def merge!(hash = {})
  hash.to_h.each { |key, val| self[key] = val }
  self
end

#nowObject



40
41
42
43
# File 'lib/turbo_boost/commands/state.rb', line 40

def now
  return nil if provisional? # provisional state cannot hold child provisional state
  @now ||= self.class.new(provisional: true)
end

#provisional?Boolean

Provisional state is for the current request/response and is exposed as ‘State#now` Standard state is preserved across multiple requests

Returns:

  • (Boolean)


36
37
38
# File 'lib/turbo_boost/commands/state.rb', line 36

def provisional?
  !!@provisional
end

#readObject



49
50
51
# File 'lib/turbo_boost/commands/state.rb', line 49

def read(...)
  now&.read(...) || store.read(...)
end

#to_sgid_paramObject



61
62
63
64
# File 'lib/turbo_boost/commands/state.rb', line 61

def to_sgid_param
  store.cleanup
  URI::UID.build(store, include_blank: false).to_sgid_param for: self.class.name, expires_in: 1.week
end