Class: Browser::Storage
- Includes:
- Enumerable
- Defined in:
- opal/browser/storage.rb
Overview
remove method_defined? checks when require order is fixed
A Storage allows you to store data across page loads and browser restarts.
Compatibility
The compatibility layer will try various implementations in the following order.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
The name of the storage.
Class Method Summary collapse
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Set a value in the storage.
-
#autosave! ⇒ Object
Enable autosaving.
-
#autosave? ⇒ Boolean
Check if autosaving is enabled.
-
#clear ⇒ Object
Clear the storage.
-
#commit(&block) ⇒ Object
Call the block between a [#reload] and [#save].
-
#delete(key) ⇒ Object
Delete a value from the storage.
-
#each {|key, value| ... } ⇒ Object
Iterate over the (key, value) pairs in the storage.
-
#initialize(window, name) ⇒ Storage
constructor
Create a new storage on the given window with the given name.
- #method_missing(*args, &block) ⇒ Object
-
#no_autosave! ⇒ Object
Disable autosaving.
-
#reload ⇒ Object
Load the storage.
-
#replace(new) ⇒ Object
Replace the current storage with the given one.
- #save ⇒ Object
- #to_h ⇒ Object
-
#to_json ⇒ String
Convert the storage to JSON.
Constructor Details
#initialize(window, name) ⇒ Storage
Create a new storage on the given window with the given name.
39 40 41 42 43 44 45 46 47 48 |
# File 'opal/browser/storage.rb', line 39 def initialize(window, name) super() @window = window @name = name @data = {} autosave! reload end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
81 82 83 |
# File 'opal/browser/storage.rb', line 81 def method_missing(*args, &block) @data.__send__(*args, &block) end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns the name of the storage.
33 34 35 |
# File 'opal/browser/storage.rb', line 33 def name @name end |
Class Method Details
Instance Method Details
#[]=(key, value) ⇒ Object
Set a value in the storage.
86 87 88 89 90 |
# File 'opal/browser/storage.rb', line 86 def []=(key, value) @data[key] = value save if autosave? end |
#autosave! ⇒ Object
Enable autosaving.
59 60 61 |
# File 'opal/browser/storage.rb', line 59 def autosave! @autosave = true end |
#autosave? ⇒ Boolean
Check if autosaving is enabled.
When autosaving is enabled the Browser::Storage is saved every time a change is made, otherwise you'll have to save it manually yourself.
54 55 56 |
# File 'opal/browser/storage.rb', line 54 def autosave? @autosave end |
#clear ⇒ Object
Clear the storage.
100 101 102 103 104 |
# File 'opal/browser/storage.rb', line 100 def clear @data.clear.tap { save if autosave? } end |
#commit(&block) ⇒ Object
Call the block between a [#reload] and [#save].
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'opal/browser/storage.rb', line 118 def commit(&block) autosave = @autosave @autosave = false result = nil reload begin result = block.call save rescue reload raise ensure @autosave = autosave end result end |
#delete(key) ⇒ Object
Delete a value from the storage.
93 94 95 96 97 |
# File 'opal/browser/storage.rb', line 93 def delete(key) @data.delete(key).tap { save if autosave? } end |
#each {|key, value| ... } ⇒ Object
Iterate over the (key, value) pairs in the storage.
73 74 75 76 77 78 79 |
# File 'opal/browser/storage.rb', line 73 def each(&block) return enum_for :each unless block @data.each(&block) self end |
#no_autosave! ⇒ Object
Disable autosaving.
64 65 66 |
# File 'opal/browser/storage.rb', line 64 def no_autosave! @autosave = false end |
#replace(new) ⇒ Object
Replace the current storage with the given one.
109 110 111 112 113 114 115 |
# File 'opal/browser/storage.rb', line 109 def replace(new) if String === new @data.replace(JSON.parse(new)) else @data.replace(new) end end |
#save ⇒ Object
153 154 155 |
# File 'opal/browser/storage.rb', line 153 def save `#@window.localStorage[#@name] = #{JSON.dump(self)}` end |
#to_h ⇒ Object
138 139 140 |
# File 'opal/browser/storage.rb', line 138 def to_h @data end |
#to_json ⇒ String
Convert the storage to JSON.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'opal/browser/storage.rb', line 199 def to_json io = StringIO.new << "{" io << JSON.create_id.to_json << ":" << self.class.name.to_json << "," @data.each {|key, value| io << key.to_json.to_s << ":" << value.to_json << "," } io.seek(-1, IO::SEEK_CUR) io << "}" io.string end |