Class: Dommy::Storage

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods, Enumerable
Defined in:
lib/dommy/storage.rb

Overview

Hash-backed Storage polyfill for localStorage / sessionStorage. Mirrors the Web Storage API: getItem(key), setItem(key, value), removeItem(key), clear(), key(index), length. Values are coerced to String to match browser semantics (browser stores everything as String).

No persistence across Window instances — each fresh Window gets an empty Storage. Tests that depend on cross-instance behaviour (none currently) would need explicit hydration.

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Constructor Details

#initializeStorage

Returns a new instance of Storage.



16
17
18
# File 'lib/dommy/storage.rb', line 16

def initialize
  @store = {}
end

Instance Method Details

#[](key) ⇒ Object



73
74
75
# File 'lib/dommy/storage.rb', line 73

def [](key)
  @store[web_string(key)]
end

#[]=(key, value) ⇒ Object



77
78
79
# File 'lib/dommy/storage.rb', line 77

def []=(key, value)
  @store[web_string(key)] = web_string(value)
end

#__js_call__(method, args) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dommy/storage.rb', line 113

def __js_call__(method, args)
  case method
  when "getItem"
    require_args!(method, args, 1)
    @store[web_string(args[0])]
  when "setItem"
    require_args!(method, args, 2)
    @store[web_string(args[0])] = web_string(args[1])
    nil
  when "removeItem"
    require_args!(method, args, 1)
    @store.delete(web_string(args[0]))
    nil
  when "clear"
    @store.clear
    nil
  when "key"
    require_args!(method, args, 1)
    @store.keys[to_index(args[0])]
  end
end

#__js_delete__(key) ⇒ Object

Named deleter (delete storage[key]): the browser's Storage removes the entry. Always "succeeds" so the JS delete returns true.



99
100
101
102
# File 'lib/dommy/storage.rb', line 99

def __js_delete__(key)
  @store.delete(key.to_s)
  true
end

#__js_get__(key) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/dommy/storage.rb', line 81

def __js_get__(key)
  case key
  when "length"
    @store.size
  else
    # A named-property miss is JS `undefined` (and `"k" in storage` false).
    @store.key?(key.to_s) ? @store[key.to_s] : Bridge::ABSENT
  end
end

#__js_named_props__Object

WebIDL "supported property names": the current keys, so Object.keys / for…in / spread enumerate only the stored entries (not the builtins, which live on Storage.prototype).



107
108
109
# File 'lib/dommy/storage.rb', line 107

def __js_named_props__
  @store.keys
end

#__js_set__(key, value) ⇒ Object

Named setter: the proxy key is already a String; the value is ToString- coerced JS-side (WebIDL DOMString named setter) before crossing.



93
94
95
# File 'lib/dommy/storage.rb', line 93

def __js_set__(key, value)
  @store[key.to_s] = value.to_s
end

#clearObject



64
65
66
67
# File 'lib/dommy/storage.rb', line 64

def clear
  @store.clear
  nil
end

#each(&blk) ⇒ Object



40
41
42
# File 'lib/dommy/storage.rb', line 40

def each(&blk)
  @store.each(&blk)
end

#entriesObject



32
33
34
# File 'lib/dommy/storage.rb', line 32

def entries
  @store.to_a
end

#get_item(key) ⇒ Object



50
51
52
# File 'lib/dommy/storage.rb', line 50

def get_item(key)
  @store[web_string(key)]
end

#key(index) ⇒ Object



69
70
71
# File 'lib/dommy/storage.rb', line 69

def key(index)
  @store.keys[to_index(index)]
end

#keysObject

Ruby-idiomatic facade matching Object.keys(storage) / Object.values(storage) / Object.entries(storage) semantics that user code reaches for in browser JS.



24
25
26
# File 'lib/dommy/storage.rb', line 24

def keys
  @store.keys
end

#lengthObject Also known as: size



44
45
46
# File 'lib/dommy/storage.rb', line 44

def length
  @store.size
end

#remove_item(key) ⇒ Object



59
60
61
62
# File 'lib/dommy/storage.rb', line 59

def remove_item(key)
  @store.delete(web_string(key))
  nil
end

#set_item(key, value) ⇒ Object



54
55
56
57
# File 'lib/dommy/storage.rb', line 54

def set_item(key, value)
  @store[web_string(key)] = web_string(value)
  nil
end

#to_hObject



36
37
38
# File 'lib/dommy/storage.rb', line 36

def to_h
  @store.dup
end

#valuesObject



28
29
30
# File 'lib/dommy/storage.rb', line 28

def values
  @store.values
end