Class: TurboBoost::Commands::StateStore

Inherits:
ActiveSupport::Cache::MemoryStore
  • Object
show all
Includes:
Enumerable
Defined in:
lib/turbo_boost/commands/state_store.rb

Defined Under Namespace

Classes: NoCoder

Constant Summary collapse

SGID_PURPOSE =
name.dup.freeze

Instance Method Summary collapse

Constructor Details

#initialize(payload = {}) ⇒ StateStore

Returns a new instance of StateStore.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/turbo_boost/commands/state_store.rb', line 18

def initialize(payload = {})
  super(coder: NoCoder.new, compress: false, expires_in: 1.day, size: 16.kilobytes)

  begin
    payload = case payload
    when SignedGlobalID then URI::UID.from_sgid(payload, for: SGID_PURPOSE)&.decode
    when GlobalID then URI::UID.from_gid(payload)&.decode
    when String then URI::UID.from_sgid(payload, for: SGID_PURPOSE)&.decode || URI::UID.from_gid(payload, for: SGID_PURPOSE)&.decode
    else payload
    end
  rescue => error
    Rails.logger.error "Failed to decode URI::UID when creating a TurboBoost::Commands::StateStore! #{error.message}"
    payload = {}
  end

  merge! payload
end

Instance Method Details

#merge!(other = {}) ⇒ Object



47
48
49
50
# File 'lib/turbo_boost/commands/state_store.rb', line 47

def merge!(other = {})
  other.to_h.each { |key, val| write key, val }
  self
end

#to_gidObject



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

def to_gid
  to_uid.to_gid
end

#to_gid_paramObject



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

def to_gid_param
  to_gid.to_param
end

#to_hObject



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

def to_h
  @data
    .each_with_object({}) { |(key, entry), memo| memo[key] = entry.value }
    .with_indifferent_access
end

#to_sgidObject



65
66
67
# File 'lib/turbo_boost/commands/state_store.rb', line 65

def to_sgid
  to_uid.to_sgid for: SGID_PURPOSE, expires_in: 1.day
end

#to_sgid_paramObject



69
70
71
# File 'lib/turbo_boost/commands/state_store.rb', line 69

def to_sgid_param
  to_sgid.to_param
end

#to_uidObject



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

def to_uid
  cleanup
  URI::UID.build to_h, include_blank: false
end