key_value 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -25,13 +25,23 @@ Migration
25
25
  Usage
26
26
  =====
27
27
  KeyValue['xxx'] = {:baz=>'foo'})
28
- KeyValue.set('xxx', {:baz=>'foo'})
28
+ or KeyValue.set('xxx', {:baz=>'foo'})
29
29
 
30
30
  KeyValue['xxx'] -> {:baz=>'foo'}
31
- KeyValue.get('xxx') -> {:baz=>'foo'}
31
+ or KeyValue.get('xxx') -> {:baz=>'foo'}
32
32
 
33
33
  KeyValue['xxx'] = nil
34
- KeyValue.del('xxx')
34
+ or KeyValue.del('xxx')
35
+
36
+ KeyValue.inc('xxx') # !! Not atomic
37
+ or KeyValue.inc('xxx', 5)
38
+
39
+ KeyValue.cache('xxx'){ ..something expensive.. }
40
+
41
+ TODO
42
+ ====
43
+ - setting / caching of false <-> AR serialize does not handle false
44
+ - HandlerSocket support
35
45
 
36
46
  Authors
37
47
  =======
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/key_value.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{key_value}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Roman Heinrich", "Michael Grosser"]
12
- s.date = %q{2011-05-03}
12
+ s.date = %q{2011-05-12}
13
13
  s.email = %q{michael@grosser.it}
14
14
  s.files = [
15
15
  "Gemfile",
data/lib/key_value.rb CHANGED
@@ -15,6 +15,7 @@ class KeyValue < ActiveRecord::Base
15
15
  record = KeyValue.find_by_key(key) || KeyValue.new(:key => key)
16
16
  record.value = value
17
17
  record.save!
18
+ value
18
19
  else
19
20
  KeyValue.delete_all(:key => key)
20
21
  end
@@ -28,4 +29,17 @@ class KeyValue < ActiveRecord::Base
28
29
  def self.del(key)
29
30
  set(key, nil)
30
31
  end
32
+
33
+ def self.inc(key, offset=1)
34
+ set(key, (get(key) || 0) + offset)
35
+ end
36
+
37
+ def self.cache(key)
38
+ value = get(key)
39
+ if value
40
+ value
41
+ else
42
+ set(key, yield)
43
+ end
44
+ end
31
45
  end
@@ -22,12 +22,22 @@ describe KeyValue do
22
22
  KeyValue.get('xxx').should == 1
23
23
  end
24
24
 
25
+ it "can set & get false" do
26
+ pending
27
+ KeyValue.set('xxx', false)
28
+ KeyValue.get('xxx').should == false
29
+ end
30
+
25
31
  it "overwrites on set" do
26
32
  KeyValue.set('xxx', 1)
27
33
  KeyValue.set('xxx', 2)
28
34
  KeyValue.get('xxx').should == 2
29
35
  end
30
36
 
37
+ it "returns set value" do
38
+ KeyValue.set('xxx',1).should == 1
39
+ end
40
+
31
41
  it "can unset" do
32
42
  KeyValue.set('yyy', 1)
33
43
  KeyValue.set('xxx', 1)
@@ -49,4 +59,46 @@ describe KeyValue do
49
59
  KeyValue['xxx'] = 1
50
60
  KeyValue['xxx'].should == 1
51
61
  end
62
+
63
+ describe :inc do
64
+ it "can inc on nil" do
65
+ KeyValue.inc('xxx')
66
+ KeyValue['xxx'].should == 1
67
+ end
68
+
69
+ it "can inc on existing value" do
70
+ KeyValue['xxx'] = 1
71
+ KeyValue.inc('xxx')
72
+ KeyValue['xxx'].should == 2
73
+ end
74
+
75
+ it "can inc with offset" do
76
+ KeyValue.inc('xxx', 5)
77
+ KeyValue['xxx'].should == 5
78
+ end
79
+
80
+ it "does not inc on non-numbers" do
81
+ KeyValue['xxx'] = '1'
82
+ lambda{
83
+ KeyValue.inc('xxx')
84
+ }.should raise_error(TypeError)
85
+ end
86
+ end
87
+
88
+ describe :cache do
89
+ it "fetches" do
90
+ KeyValue['xxx'] = 1
91
+ KeyValue.cache('xxx'){2}.should == 1
92
+ end
93
+
94
+ it "stores" do
95
+ KeyValue.cache('xxx'){2}.should == 2
96
+ end
97
+
98
+ it "can store false" do
99
+ pending
100
+ KeyValue.cache('xxx'){false}
101
+ KeyValue.cache('xxx'){true}.should == false
102
+ end
103
+ end
52
104
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: key_value
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Roman Heinrich
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-05-03 00:00:00 +02:00
19
+ date: 2011-05-12 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency