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.
38 39 40 41 42 43 44 45 46 47 |
# File 'opal/browser/storage.rb', line 38 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
80 81 82 |
# File 'opal/browser/storage.rb', line 80 def method_missing(*args, &block) @data.__send__(*args, &block) end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns the name of the storage.
32 33 34 |
# File 'opal/browser/storage.rb', line 32 def name @name end |
Class Method Details
Instance Method Details
#[]=(key, value) ⇒ Object
Set a value in the storage.
85 86 87 88 89 |
# File 'opal/browser/storage.rb', line 85 def []=(key, value) @data[key] = value save if autosave? end |
#autosave! ⇒ Object
Enable autosaving.
58 59 60 |
# File 'opal/browser/storage.rb', line 58 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.
53 54 55 |
# File 'opal/browser/storage.rb', line 53 def autosave? @autosave end |
#clear ⇒ Object
Clear the storage.
99 100 101 102 103 |
# File 'opal/browser/storage.rb', line 99 def clear @data.clear.tap { save if autosave? } end |
#commit(&block) ⇒ Object
Call the block between a [#reload] and [#save].
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'opal/browser/storage.rb', line 117 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.
92 93 94 95 96 |
# File 'opal/browser/storage.rb', line 92 def delete(key) @data.delete(key).tap { save if autosave? } end |
#each {|key, value| ... } ⇒ Object
Iterate over the (key, value) pairs in the storage.
72 73 74 75 76 77 78 |
# File 'opal/browser/storage.rb', line 72 def each(&block) return enum_for :each unless block @data.each(&block) self end |
#no_autosave! ⇒ Object
Disable autosaving.
63 64 65 |
# File 'opal/browser/storage.rb', line 63 def no_autosave! @autosave = false end |
#replace(new) ⇒ Object
Replace the current storage with the given one.
108 109 110 111 112 113 114 |
# File 'opal/browser/storage.rb', line 108 def replace(new) if String === new @data.replace(JSON.parse(new)) else @data.replace(new) end end |
#save ⇒ Object
152 153 154 |
# File 'opal/browser/storage.rb', line 152 def save `#@window.localStorage[#@name] = #{JSON.dump(self)}` end |
#to_h ⇒ Object
137 138 139 |
# File 'opal/browser/storage.rb', line 137 def to_h @data end |
#to_json ⇒ String
Convert the storage to JSON.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'opal/browser/storage.rb', line 198 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_json << ":" << value.to_json << "," } io.seek(-1, IO::SEEK_CUR) io << "}" io.string end |