Class: Browser::Storage

Inherits:
Object show all
Includes:
Enumerable
Defined in:
opal/browser/storage.rb

Overview

TODO:

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

SessionStorage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, name) ⇒ Storage

Create a new storage on the given window with the given name.

Parameters:

  • window (native)

    the window to save the storage to

  • name (String)

    the name to use to discern different storages


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

#nameString (readonly)

Returns the name of the storage.

Returns:

  • (String)

    the name of the storage


32
33
34
# File 'opal/browser/storage.rb', line 32

def name
  @name
end

Class Method Details

.json_create(data) ⇒ Object


22
23
24
25
26
27
28
# File 'opal/browser/storage.rb', line 22

def self.json_create(data)
  data.delete(JSON.create_id)

  Hash[data.map {|key, value|
    [JSON.parse(key), value]
  }]
end

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.

Returns:

  • (Boolean)

53
54
55
# File 'opal/browser/storage.rb', line 53

def autosave?
  @autosave
end

#clearObject

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.

Yields:

  • (key, value)

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

#reloadObject

Load the storage.


# File 'opal/browser/storage.rb', line 141

#replace(new) ⇒ Object

Replace the current storage with the given one.

Parameters:


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

#saveObject


152
153
154
# File 'opal/browser/storage.rb', line 152

def save
  `#@window.localStorage[#@name] = #{JSON.dump(self)}`
end

#to_hObject


137
138
139
# File 'opal/browser/storage.rb', line 137

def to_h
  @data
end

#to_jsonString

Convert the storage to JSON.

Returns:

  • (String)

    the JSON representation


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_s << ":" << value.to_json << ","
  }

  io.seek(-1, IO::SEEK_CUR)
  io << "}"

  io.string
end