Skip to content

Commit

Permalink
storage: slightly refactor the API
Browse files Browse the repository at this point in the history
meh committed Aug 23, 2014
1 parent a0b9bcc commit 6c53f8d
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions opal/browser/storage.rb
Original file line number Diff line number Diff line change
@@ -43,13 +43,7 @@ def initialize(window, name)
@data = {}

autosave!
init
end

# @!attribute [r] encoded_name
# @return [String] the generated name
def encoded_name
"opal.storage.#@name"
reload
end

# Check if autosaving is enabled.
@@ -123,57 +117,57 @@ def to_h
@data
end

# @!method init
# @private
# @!method reload
# Load the storage.

# @!method save
# Persist the current state to the storage.

if Browser.supports? 'Storage.local'
def init
replace `#@window.localStorage[#{encoded_name}] || '{}'`
def reload
replace `#@window.localStorage[#@name] || '{}'`
end

def save
`#@window.localStorage[#{encoded_name}] = #{JSON.dump(self)}`
`#@window.localStorage[#@name] = #{JSON.dump(self)}`
end
elsif Browser.supports? 'Storage.global'
def init
replace `#@window.globalStorage[#@window.location.hostname][#{encoded_name}] || '{}'`
def reload
replace `#@window.globalStorage[#@window.location.hostname][#@name] || '{}'`
end

def save
`#@window.globalStorage[#@window.location.hostname][#{encoded_name}] = #{JSON.dump(self)}`
`#@window.globalStorage[#@window.location.hostname][#@name] = #{JSON.dump(self)}`
end
elsif Browser.supports? 'Element.addBehavior'
def init
def reload
%x{
#@element = #@window.document.createElement('link');
#@element.addBehavior('#default#userData');
#@window.document.getElementsByTagName('head')[0].appendChild(#@element);
#@element.load(#{encoded_name});
#@element.load(#@name);
}

replace `#@element.getAttribute(#{encoded_name}) || '{}'`
replace `#@element.getAttribute(#@name) || '{}'`
end

def save
%x{
#@element.setAttribute(#{encoded_name}, #{JSON.dump(self)});
#@element.save(#{encoded_name});
#@element.setAttribute(#@name, #{JSON.dump(self)});
#@element.save(#@name);
}
end
else
def init
def reload
$document.cookies.options expires: 60 * 60 * 24 * 365

replace $document.cookies[encoded_name]
replace $document.cookies[@name]
end

def save
$document.cookies[encoded_name] = JSON.dump(self)
$document.cookies[@name] = JSON.dump(self)
end
end

@@ -185,7 +179,7 @@ def to_json

io << JSON.create_id.to_json << ":" << self.class.name.to_json << ","

each {|key, value|
@data.each {|key, value|
io << key.to_json.to_json << ":" << value.to_json << ","
}

@@ -205,12 +199,12 @@ def self.supported?
Browser.supports? 'Storage.session'
end

def init
replace `#@window.sessionStorage[#{encoded_name}] || '{}'`
def reload
replace `#@window.sessionStorage[#@name] || '{}'`
end

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

0 comments on commit 6c53f8d

Please sign in to comment.