store.coffee | |
---|---|
Template | |
Private functions | |
Attempt to dig down in to the | dig = (root, path, force, parseFirst = yes) ->
result = [root]
if path and path.indexOf('.') isnt -1
path = path.split '.'
object = base = root[basePath = path.shift()]
object = base = tryParse object if parseFirst
while object and path.length > 1
object = object[path.shift()]
object = {} if not object? and force
result.push base, basePath, object, path.shift()
else
result.push root, path, root, path
result |
Attempt to parse | tryParse = (value) -> if value? then JSON.parse value else value |
Attempt to stringify | tryStringify = (value) -> if value? then JSON.stringify value else value |
Store setup | store = window.store = new class Store extends utils.Class |
Public functions | |
Create a backup string containing all the information contained within
| backup: ->
data = {}
data[key] = value for own key, value of localStorage
encodeURIComponent JSON.stringify data |
Clear all keys from | clear: -> delete localStorage[key] for own key of localStorage |
Determine whether or not the specified | exists: (keys...) ->
return no for key in keys when not localStorage.hasOwnProperty key
yes |
Retrieve the value associated with the specified | get: (key) ->
parts = dig localStorage, key
if parts[3]
value = parts[3][parts[4]] |
Ensure that the value is parsed if retrieved directly from
| value = tryParse value if parts[3] is parts[0]
value |
Initialize the value of the specified key(s) in | init: (keys, defaultValue) -> switch typeof keys
when 'object'
@init key, defaultValue for own key, defaultValue of keys
when 'string' then @set keys, @get(keys) ? defaultValue |
For each of the specified | modify: (keys..., callback) -> for key in keys
value = @get key
callback? value, key
@set key, value |
Remove the specified | remove: (keys...) ->
if keys.length is 1
value = @get keys[0]
delete localStorage[keys[0]]
return value
delete localStorage[key] for key in keys |
Copy the value of the existing key to that of the new key then remove the
old key from | rename: (oldKey, newKey, defaultValue) ->
if @exists oldKey
@set newKey, @get oldKey
@remove oldKey
else
@set newKey, defaultValue |
Restore | restore: (str) ->
data = JSON.parse decodeURIComponent str
localStorage[key] = value for own key, value of data |
Search | search: (regex) -> key for own key of localStorage when regex.test key |
Set the value of the specified key(s) in | set: (keys, value) -> switch typeof keys
when 'object' then @set key, value for own key, value of keys
when 'string'
oldValue = @get keys
localStorage[keys] = tryStringify value
oldValue |
Public classes | |
| class store.Updater extends utils.Class |
Create a new instance of | constructor: (@namespace) -> @isNew = not @exists() |
Determine whether or not this namespace exists. | exists: -> store.get("updates.#{@namespace}")? |
Remove this namespace. | remove: -> store.modify 'updates', (updates) => delete updates[@namespace] |
Rename this namespace to | rename: (namespace) -> store.modify 'updates', (updates) =>
updates[namespace] = updates[@namespace] if updates[@namespace]?
delete updates[@namespace]
@namespace = namespace |
Update this namespace to | update: (version, processor) -> store.modify 'updates', (updates) =>
updates[@namespace] ?= ''
if updates[@namespace] < version
processor?()
updates[@namespace] = version |
Configuration | |
Initialize updates. | store.init 'updates', {}
|