Class: TurboBoost::State

Inherits:
Object
  • Object
show all
Defined in:
lib/turbo_boost/state/errors.rb,
lib/turbo_boost/state.rb

Defined Under Namespace

Classes: DeserializationError, Manager, ProvisionalState

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ordinal_payload = nil) ⇒ State

Returns a new instance of State.



38
39
40
41
42
43
44
45
# File 'lib/turbo_boost/state.rb', line 38

def initialize(ordinal_payload = nil)
  @internal_data = {}.with_indifferent_access
  @internal_keys = []

  deserialize(ordinal_payload).each do |(key, value)|
    write key, value
  end
end

Class Method Details

.deserialize(string) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/turbo_boost/state.rb', line 24

def deserialize(string)
  return {} if string.blank?
  decoded = Base64.urlsafe_decode64(string)
  inflated = Zlib::Inflate.inflate(decoded)
  Marshal.load inflated
rescue => error
  raise TurboBoost::State::DeserializationError, "Unable to decode, inflate, and load Base64 string! \"#{string}\" #{error.message}"
end

.deserialize_base64(string) ⇒ Object



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

def deserialize_base64(string)
  return {} if string.blank?
  JSON.parse Base64.urlsafe_decode64(string)
rescue => error
  raise TurboBoost::State::DeserializationError, "Unable to decode and parse Base64 string! \"#{string}\" #{error.message}"
end

.key_for(*keys) ⇒ Object



33
34
35
# File 'lib/turbo_boost/state.rb', line 33

def key_for(*keys)
  keys.map { |key| key.try(:cache_key) || key.to_s }.join("/")
end

.serialize(data) ⇒ Object



18
19
20
21
22
# File 'lib/turbo_boost/state.rb', line 18

def serialize(data)
  dump = Marshal.dump(data)
  deflated = Zlib::Deflate.deflate(dump, Zlib::BEST_COMPRESSION)
  Base64.urlsafe_encode64 deflated
end

.serialize_base64(data) ⇒ Object



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

def serialize_base64(data)
  Base64.urlsafe_encode64 data.to_json, padding: false
end

Instance Method Details

#cache_keyObject



51
52
53
# File 'lib/turbo_boost/state.rb', line 51

def cache_key
  "turbo-boost/ui-state/#{Digest::SHA2.base64digest(payload)}"
end

#clearObject



83
84
85
86
# File 'lib/turbo_boost/state.rb', line 83

def clear
  internal_keys.clear
  internal_data.clear
end

#delete(*keys) ⇒ Object



69
70
71
72
73
# File 'lib/turbo_boost/state.rb', line 69

def delete(*keys)
  key = key_for(*keys)
  internal_keys.delete key
  internal_data.delete key
end

#ordinal_payloadObject



79
80
81
# File 'lib/turbo_boost/state.rb', line 79

def ordinal_payload
  serialize internal_list
end

#payloadObject



75
76
77
# File 'lib/turbo_boost/state.rb', line 75

def payload
  serialize_base64 internal_data
end

#prune!(max_bytesize: 2.kilobytes) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/turbo_boost/state.rb', line 93

def prune!(max_bytesize: 2.kilobytes)
  return if internal_keys.blank?
  return if internal_data.blank?

  percentage = (max_bytesize > 0) ? ordinal_payload.bytesize / max_bytesize.to_f : 0
  while percentage > 1
    keys_to_keep = internal_keys.slice((internal_keys.length - (internal_keys.length / percentage).floor)..-1)
    keys_to_remove = internal_keys - keys_to_keep
    @internal_keys = keys_to_keep
    keys_to_remove.each { |key| internal_data.delete key }
    percentage = (max_bytesize > 0) ? ordinal_payload.bytesize / max_bytesize.to_f : 0
  end
end

#read(*keys, default: nil) ⇒ Object



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

def read(*keys, default: nil)
  value = internal_data[key_for(*keys)]
  value = write(*keys, default) if value.nil? && default
  value
end

#shrink!Object



88
89
90
91
# File 'lib/turbo_boost/state.rb', line 88

def shrink!
  @internal_data = shrink(internal_data).with_indifferent_access
  @internal_keys = internal_keys & internal_data.keys
end

#to_hObject

Returns a copy of the data as a Hash



108
109
110
# File 'lib/turbo_boost/state.rb', line 108

def to_h
  internal_data.deep_dup
end

#write(*keys, value) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/turbo_boost/state.rb', line 61

def write(*keys, value)
  key = key_for(*keys)
  internal_keys.delete key if internal_keys.include?(key)
  internal_keys << key
  internal_data[key] = value
  value
end