Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add localstorage implementation
  • Loading branch information
adambeynon committed Jan 14, 2014
1 parent 3c7dca1 commit eab45e2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@
* Add `Event` methods: `prevented?`, `prevent`, `stopped?` and `stop` to
replace longer javascript names.

* Add `LocalStorage` implementation.

## 0.1.2 2013-12-01

* Support setting html content through `Element#html()`.
Expand Down
28 changes: 28 additions & 0 deletions opal/opal-jquery/local_storage.rb
@@ -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`)
22 changes: 22 additions & 0 deletions spec/local_storage_spec.rb
@@ -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

0 comments on commit eab45e2

Please sign in to comment.