key_value 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -43,6 +43,14 @@ Usage
43
43
  # cache
44
44
  KeyValue.cache('xxx'){ ..something expensive.. }
45
45
 
46
+ # defaults (in case the value is not stored yet)
47
+ KeyValue.defaults['xxx'] = 1
48
+ KeyValue['xxx'] -> 1
49
+ KeyValue['xxx'] = 2
50
+ KeyValue['xxx'] -> 2
51
+ KeyValue['xxx'] = nil
52
+ KeyValue['xxx'] -> 1
53
+
46
54
  HandlerSocket ([Ubuntu natty guide](http://grosser.it/2011/05/14/installing-mysql-handlersocket-in-ubuntu-natty-for-ruby/)):
47
55
 
48
56
  KeyValue.handler_socket = true
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.2
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.4.1"
8
+ s.version = "0.4.2"
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-17}
12
+ s.date = %q{2011-05-18}
13
13
  s.email = %q{michael@grosser.it}
14
14
  s.files = [
15
15
  "Gemfile",
data/lib/key_value.rb CHANGED
@@ -10,6 +10,8 @@ class KeyValue < ActiveRecord::Base
10
10
  set_primary_key :key
11
11
 
12
12
  cattr_accessor :handler_socket
13
+ cattr_accessor :defaults
14
+ @@defaults = {}.with_indifferent_access
13
15
 
14
16
  # serialize would treat false the same as nil
15
17
  def value=(x)
@@ -22,16 +24,22 @@ class KeyValue < ActiveRecord::Base
22
24
  end
23
25
 
24
26
  def self.get(key)
25
- if handler_socket
27
+ key = key.to_s
28
+
29
+ found = if handler_socket
26
30
  open_key_index
27
31
  result = hs_find_by_key(key)
28
32
  YAML.load(result) if result
29
33
  else
30
34
  KeyValue.find_by_key(key).try(:value)
31
35
  end
36
+
37
+ found.nil? ? defaults[key] : found
32
38
  end
33
39
 
34
40
  def self.set(key, value)
41
+ key = key.to_s
42
+
35
43
  if value.nil?
36
44
  KeyValue.delete_all(:key => key)
37
45
  else
@@ -23,6 +23,13 @@ describe KeyValue do
23
23
  KeyValue.get('xxx').should == 1
24
24
  end
25
25
 
26
+ it "can set & get with symbols/strings" do
27
+ KeyValue[:xxx] = 1
28
+ KeyValue['xxx'].should == 1
29
+ KeyValue['xxx'] = 1
30
+ KeyValue[:xxx].should == 1
31
+ end
32
+
26
33
  it "can set & get all kinds of objects" do
27
34
  KeyValue.set('xxx', '1')
28
35
  KeyValue.get('xxx').should == '1'
@@ -115,6 +122,21 @@ describe KeyValue do
115
122
  end
116
123
  end
117
124
 
125
+ describe :defaults do
126
+ it "uses them when nothing is set" do
127
+ KeyValue.defaults.merge!(:x => 1)
128
+ KeyValue['x'].should == 1
129
+ end
130
+
131
+ it "does not use them when something is set" do
132
+ KeyValue.defaults.merge!(:x => 1)
133
+ KeyValue['x'] = false
134
+ KeyValue['x'].should == false
135
+ KeyValue['x'] = 2
136
+ KeyValue['x'].should == 2
137
+ end
138
+ end
139
+
118
140
  if ENV['DB'] == 'mysql'
119
141
  describe 'with handlersocket' do
120
142
  before do
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: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 1
10
- version: 0.4.1
9
+ - 2
10
+ version: 0.4.2
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-17 00:00:00 +02:00
19
+ date: 2011-05-18 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency