Class: Dommy::Storage
- Inherits:
-
Object
- Object
- Dommy::Storage
- Includes:
- 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
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
- #__js_set__(key, value) ⇒ Object
- #clear ⇒ Object
- #each(&blk) ⇒ Object
- #entries ⇒ Object
- #get_item(key) ⇒ Object
-
#initialize ⇒ Storage
constructor
A new instance of Storage.
- #key(index) ⇒ Object
-
#keys ⇒ Object
Ruby-idiomatic facade matching ‘Object.keys(storage)` / `Object.values(storage)` / `Object.entries(storage)` semantics that user code reaches for in browser JS.
- #length ⇒ Object (also: #size)
- #remove_item(key) ⇒ Object
- #set_item(key, value) ⇒ Object
- #to_h ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize ⇒ Storage
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[key.to_s] end |
#[]=(key, value) ⇒ Object
77 78 79 |
# File 'lib/dommy/storage.rb', line 77 def []=(key, value) @store[key.to_s] = value.to_s end |
#__js_call__(method, args) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dommy/storage.rb', line 94 def __js_call__(method, args) case method when "getItem" @store[args[0].to_s] when "setItem" @store[args[0].to_s] = args[1].to_s nil when "removeItem" @store.delete(args[0].to_s) nil when "clear" @store.clear nil when "key" @store.keys[args[0].to_i] end end |
#__js_get__(key) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/dommy/storage.rb', line 81 def __js_get__(key) case key when "length" @store.size else @store[key.to_s] end end |
#__js_set__(key, value) ⇒ Object
90 91 92 |
# File 'lib/dommy/storage.rb', line 90 def __js_set__(key, value) @store[key.to_s] = value.to_s end |
#clear ⇒ Object
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 |
#entries ⇒ Object
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[key.to_s] end |
#key(index) ⇒ Object
69 70 71 |
# File 'lib/dommy/storage.rb', line 69 def key(index) @store.keys[index.to_i] end |
#keys ⇒ Object
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 |
#length ⇒ Object 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(key.to_s) nil end |
#set_item(key, value) ⇒ Object
54 55 56 57 |
# File 'lib/dommy/storage.rb', line 54 def set_item(key, value) @store[key.to_s] = value.to_s nil end |
#to_h ⇒ Object
36 37 38 |
# File 'lib/dommy/storage.rb', line 36 def to_h @store.dup end |
#values ⇒ Object
28 29 30 |
# File 'lib/dommy/storage.rb', line 28 def values @store.values end |