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



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

#nameString (readonly)

Returns the name of the storage.

Returns:

  • (String)

    the name of the storage



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

def name
  @name
end

Class Method Details

.json_create(data) ⇒ Object



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

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.



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.

Returns:

  • (Boolean)


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

def autosave?
  @autosave
end

#clearObject

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.

Yields:

  • (key, value)


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

#reloadObject

Load the storage.



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

#replace(new) ⇒ Object

Replace the current storage with the given one.

Parameters:



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

#saveObject



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

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

#to_hObject



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

def to_h
  @data
end

#to_jsonString

Convert the storage to JSON.

Returns:

  • (String)

    the JSON representation



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