Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c7dca1
commit eab45e2
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module DOM | ||
class LocalStorage | ||
def initialize(storage) | ||
@storage = storage | ||
end | ||
|
||
def []=(key, value) | ||
`#@storage.setItem(key, value)` | ||
end | ||
|
||
def [](key) | ||
%x{ | ||
var value = #@storage.getItem(key); | ||
return value == null ? nil : value; | ||
} | ||
end | ||
|
||
def delete(key) | ||
`#@storage.removeItem(key)` | ||
end | ||
|
||
def clear | ||
`#@storage.clear()` | ||
end | ||
end | ||
end | ||
|
||
LocalStorage = DOM::LocalStorage.new(`window.localStorage`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'spec_helper' | ||
require 'opal-jquery/local_storage' | ||
|
||
describe LocalStorage do | ||
before { subject.clear } | ||
|
||
it "returns nil for undefined values" do | ||
expect(subject['foo']).to be_nil | ||
end | ||
|
||
it "should be able to create items" do | ||
subject['foo'] = 'Ford Prefect' | ||
expect(subject['foo']).to eq('Ford Prefect') | ||
end | ||
|
||
it "should be able to delete items" do | ||
subject['name'] = 'Arthur' | ||
subject.delete 'name' | ||
|
||
expect(subject['name']).to be_nil | ||
end | ||
end |