remember 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +53 -0
- data/VERSION +1 -0
- data/etc/remember.jpg +0 -0
- data/lib/remember.rb +56 -0
- data/spec/remember_spec.rb +131 -0
- metadata +59 -0
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Remember
|
2
|
+
|
3
|
+
![Remember](/jpignata/remember/raw/master/etc/remember.jpg)
|
4
|
+
|
5
|
+
A simple object persistence wrapper for Moneta. Initialize Remember with any Moneta adapter and serialize any object that implements Ruby's marshal API.
|
6
|
+
|
7
|
+
## Adapters
|
8
|
+
|
9
|
+
Moneta currently supports:
|
10
|
+
|
11
|
+
* File store for xattr
|
12
|
+
* Basic File Store
|
13
|
+
* Memcache store
|
14
|
+
* In-memory store
|
15
|
+
* The xattrs in a file system
|
16
|
+
* DataMapper
|
17
|
+
* S3
|
18
|
+
* Berkeley DB
|
19
|
+
* Redis
|
20
|
+
* SDBM
|
21
|
+
* Tokyo
|
22
|
+
* CouchDB
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
jp@populuxe:~/Projects/remember$ irb -r 'lib/remember' -r 'moneta/redis'
|
27
|
+
>> r = Remember.new(Moneta::Redis)
|
28
|
+
>> r["important_data"] = rand(100000000000000000000000)
|
29
|
+
=> 99924367975800373618177
|
30
|
+
>> r["important_data"]
|
31
|
+
=> 99924367975800373618177
|
32
|
+
>> r["config"] = { :setting => 'value', :other_setting => 'other value' }
|
33
|
+
|
34
|
+
jp@populuxe:~/Projects/remember$ irb -r 'lib/remember' -r 'moneta/redis'
|
35
|
+
>> r = Remember.new(Moneta::Redis)
|
36
|
+
>> r["important_data"]
|
37
|
+
=> 99924367975800373618177
|
38
|
+
>> r["config"]
|
39
|
+
=> {:setting=>"value", :other_setting=>"other value"}
|
40
|
+
|
41
|
+
jp@populuxe:~/Projects/remember$ irb -r 'lib/remember' -r 'moneta/redis'
|
42
|
+
>> r = Remember.new(Moneta::Redis)
|
43
|
+
>> class Duck; def make_noise; "Quack!"; end; end
|
44
|
+
>> d = Duck.new
|
45
|
+
>> d.make_noise
|
46
|
+
=> "Quack!"
|
47
|
+
>> r["duck"] = d
|
48
|
+
|
49
|
+
jp@populuxe:~/Projects/remember$ irb -r 'lib/remember' -r 'moneta/redis'
|
50
|
+
>> r = Remember.new(Moneta::Redis)
|
51
|
+
>> class Duck; def make_noise; "Quack!"; end; end
|
52
|
+
>> r["duck"].make_noise
|
53
|
+
=> "Quack!"
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/etc/remember.jpg
ADDED
Binary file
|
data/lib/remember.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'moneta'
|
2
|
+
|
3
|
+
class Remember
|
4
|
+
def initialize(moneta, options = {})
|
5
|
+
@moneta = moneta.new(options)
|
6
|
+
end
|
7
|
+
|
8
|
+
def []=(key, object)
|
9
|
+
store(key, object)
|
10
|
+
end
|
11
|
+
|
12
|
+
def store(key, object, options = {})
|
13
|
+
@moneta.store(key, Marshal.dump(object), options)
|
14
|
+
object
|
15
|
+
end
|
16
|
+
|
17
|
+
def update_key(key, options = {})
|
18
|
+
if @moneta.key?(key)
|
19
|
+
@moneta.update_key(key, options)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def fetch(key, options, &block)
|
24
|
+
if value = self[key]
|
25
|
+
value
|
26
|
+
else
|
27
|
+
store(key, yield, options)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def [](key)
|
32
|
+
deserialize(@moneta[key])
|
33
|
+
end
|
34
|
+
|
35
|
+
def delete(key)
|
36
|
+
deserialize(@moneta.delete(key))
|
37
|
+
end
|
38
|
+
|
39
|
+
def key?(key)
|
40
|
+
@moneta.key?(key)
|
41
|
+
end
|
42
|
+
|
43
|
+
def clear
|
44
|
+
@moneta.clear
|
45
|
+
end
|
46
|
+
|
47
|
+
alias has_key? key?
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def deserialize(value)
|
52
|
+
if value && value.size > 0
|
53
|
+
Marshal.load(value)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../lib/remember')
|
2
|
+
require 'moneta/memory'
|
3
|
+
|
4
|
+
describe Remember do
|
5
|
+
before do
|
6
|
+
@r = Remember.new(Moneta::Memory)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#[]=" do
|
10
|
+
it "returns the Object originally passed into it" do
|
11
|
+
@r["enum_sizes"] = ['xs', 's', 'm', 'l'].should == ['xs', 's', 'm', 'l']
|
12
|
+
end
|
13
|
+
|
14
|
+
it "persists object values and behaviors" do
|
15
|
+
class Bridge < Struct.new(:name, :year)
|
16
|
+
def to_s; "#{name} opened in #{year}"; end
|
17
|
+
def opened?; year < Time.now.year; end
|
18
|
+
end
|
19
|
+
|
20
|
+
@r["brooklyn"] = Bridge.new("Brooklyn Bridge", 1883)
|
21
|
+
@r["verrazano"] = Bridge.new("Verrazano-Narrows Bridge", 1964)
|
22
|
+
|
23
|
+
@r["brooklyn"].to_s.should == "Brooklyn Bridge opened in 1883"
|
24
|
+
@r["verrazano"].year.should == 1964
|
25
|
+
@r["brooklyn"].opened?.should be_true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#[]" do
|
30
|
+
context "a given key exists" do
|
31
|
+
it "returns a deserialized object" do
|
32
|
+
@r["thirteen"] = 13
|
33
|
+
@r["thirteen"].should == 13
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "a given key does not exist" do
|
38
|
+
it "returns nil" do
|
39
|
+
@r["pretend"].should be_nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#delete" do
|
45
|
+
context "a given key exists" do
|
46
|
+
it "returns the object that the key contained after deletion" do
|
47
|
+
@r["thirteen"] = 13
|
48
|
+
@r.delete("thirteen").should == 13
|
49
|
+
@r["thirteen"].should be_nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "a given key does not exist" do
|
54
|
+
it "returns nil" do
|
55
|
+
@r.delete("pretend").should be_nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#key?" do
|
61
|
+
context "a key that exists" do
|
62
|
+
it "returns true" do
|
63
|
+
@r["real"] = true
|
64
|
+
@r.key?("real").should be_true
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "a key that does not exist" do
|
69
|
+
it "returns false" do
|
70
|
+
@r.key?("pretend").should be_false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "#has_key?" do
|
76
|
+
it "is an alias for key?" do
|
77
|
+
@r.has_key?("pretend").should == @r.key?("pretend")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "#clear" do
|
82
|
+
it "clears all persisted objects" do
|
83
|
+
@r["do"] = 1
|
84
|
+
@r["ray"] = 2
|
85
|
+
@r["egon"] = 3
|
86
|
+
@r.clear
|
87
|
+
@r.key?("ray").should be_false
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "#store" do
|
92
|
+
it "saves an object to key and sets an expiration in seconds" do
|
93
|
+
@r.store("zuul", :zuul, :expires_in => 60).should == :zuul
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "#update_key" do
|
98
|
+
context "a key that exists" do
|
99
|
+
it "updates object identified by key and sets an expiration in seconds and returns the new expiration time" do
|
100
|
+
@r["gozer"] = :gozer
|
101
|
+
@r.update_key("gozer", :expires_in => 60).to_i.should == Time.now.to_i + 60
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "a key that does not exist" do
|
106
|
+
it "returns nil" do
|
107
|
+
@r.update_key("slimer", :expires_in => 60).should be_nil
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "#fetch" do
|
113
|
+
context "a key that exists" do
|
114
|
+
it "fetches the object" do
|
115
|
+
@r["some_data"] = 123
|
116
|
+
@r.fetch("some_data", :expires_in => 120) { 456 }.should == 123
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "a key that does not exist" do
|
121
|
+
it "executes a block and then stores the resulting object" do
|
122
|
+
@r["resource_intensive_data"].should be_nil
|
123
|
+
data = @r.fetch("resource_intensive_data", :expires_in => 120) do
|
124
|
+
['calculation']
|
125
|
+
end
|
126
|
+
data.should == ['calculation']
|
127
|
+
@r["resource_intensive_data"].should == ['calculation']
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: remember
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Pignata
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-16 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A Moneta wrapper for Ruby object persistence
|
17
|
+
email: john.pignata@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.md
|
24
|
+
files:
|
25
|
+
- README.md
|
26
|
+
- VERSION
|
27
|
+
- etc/remember.jpg
|
28
|
+
- lib/remember.rb
|
29
|
+
- spec/remember_spec.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://github.com/jpignata/remember
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --charset=UTF-8
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.3.5
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: A Moneta wrapper for Ruby object persistence
|
58
|
+
test_files:
|
59
|
+
- spec/remember_spec.rb
|