kvs 0.3.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/README.markdown +9 -1
  2. data/VERSION +1 -1
  3. data/lib/kvs.rb +63 -29
  4. data/spec/kvs_spec.rb +40 -5
  5. metadata +2 -3
  6. data/kvs.gemspec +0 -28
@@ -11,11 +11,13 @@ KVS is a simple key value store.
11
11
  Install
12
12
  --------
13
13
 
14
- sudo gem install jugyo-kvs --source http://gems.github.com
14
+ gem install kvs
15
15
 
16
16
  Usage
17
17
  --------
18
18
 
19
+ use as singleton:
20
+
19
21
  KVS.dir = '/tmp/kvs'
20
22
 
21
23
  KVS['foo'] = 'bar'
@@ -26,6 +28,12 @@ Usage
26
28
 
27
29
  KVS.delete('foo')
28
30
 
31
+ use as instance:
32
+
33
+ KVS_A = KVS.new('/tmp/kvs_a')
34
+ KVS_A['foo'] = 'bar'
35
+ puts KVS_A['foo'] #=> bar
36
+
29
37
  License
30
38
  --------
31
39
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.1
data/lib/kvs.rb CHANGED
@@ -1,41 +1,75 @@
1
1
  require 'digest/sha1'
2
2
  require 'yaml'
3
+ require 'fileutils'
3
4
 
4
- module KVS
5
- class <<self
6
- attr_accessor :dir
5
+ class KVS
6
+ @instance = self.new
7
7
 
8
- def <<(value)
9
- key = key_gen(value)
10
- self[key] = value
11
- key
8
+ def self.method_missing(method, *args, &block)
9
+ if @instance.respond_to?(method)
10
+ @instance.__send__(method, *args, &block)
11
+ else
12
+ super
12
13
  end
14
+ end
13
15
 
14
- def [](key)
15
- path = file_of(key)
16
- return nil unless File.exists?(path)
17
- YAML.load(File.read(path))
18
- end
16
+ def self.inspect
17
+ @instance.inspect
18
+ end
19
19
 
20
- def []=(key, value)
21
- File.open(file_of(key), 'wb') { |f| f << value.to_yaml }
22
- end
20
+ attr_accessor :dir
23
21
 
24
- def delete(key)
25
- path = file_of(key)
26
- File.delete(path) if File.exists?(path)
27
- nil
28
- end
22
+ def initialize(dir = nil)
23
+ self.dir = dir
24
+ end
29
25
 
30
- def file_of(key)
31
- key = key.to_s
32
- raise ArgumentError, 'invalid key' unless key =~ /^[\w]+$/
33
- raise "dir is not specified" unless dir
34
- File.join(dir, key)
35
- end
26
+ def <<(value)
27
+ key = key_gen(value)
28
+ self[key] = value
29
+ key
30
+ end
36
31
 
37
- def key_gen(value)
38
- Digest::SHA1.hexdigest(value.to_s)
39
- end
32
+ def [](key)
33
+ path = file_of(key)
34
+ return nil unless File.exists?(path)
35
+ YAML.load(File.read(path))
36
+ end
37
+
38
+ def key?(key)
39
+ !!self[key]
40
+ end
41
+
42
+ def []=(key, value)
43
+ path = file_of(key)
44
+ FileUtils.mkdir_p(dir) unless File.exists?(dir)
45
+ File.open(path, 'wb') { |f| f << value.to_yaml }
46
+ end
47
+
48
+ def delete(key)
49
+ path = file_of(key)
50
+ File.delete(path) if File.exists?(path)
51
+ nil
52
+ end
53
+
54
+ def file_of(key)
55
+ key = key.to_s
56
+ raise ArgumentError, 'invalid key' unless key =~ /^[\w]+$/
57
+ raise "dir is not specified" unless dir
58
+ File.join(dir, key)
59
+ end
60
+
61
+ def key_gen(value)
62
+ Digest::SHA1.hexdigest(value.to_s)
63
+ end
64
+
65
+ def keys
66
+ Dir.glob(dir + '/*').map {|f| File.basename(f) }
67
+ end
68
+
69
+ def inspect
70
+ pairs = Dir.glob(dir + '/*').map {|f|
71
+ "#{File.basename(f).inspect}: #{YAML.load_file(f).inspect}"
72
+ }
73
+ "KVS(#{pairs.join ', '})"
40
74
  end
41
75
  end
@@ -7,18 +7,20 @@ describe KVS do
7
7
  before do
8
8
  @tmpdir = Dir.tmpdir + '/kvs_test'
9
9
  FileUtils.rm_rf(@tmpdir)
10
- FileUtils.mkdir_p(@tmpdir)
11
10
  KVS.dir = @tmpdir
12
11
  end
13
12
 
14
- it 'should generate file path' do
13
+ it 'generates file path' do
15
14
  KVS.file_of('foo').should == @tmpdir + '/foo'
16
15
  end
17
16
 
18
- it 'should store data' do
17
+ it 'stores data' do
19
18
  KVS['foo'] = {:a => 'b', :c => 'd'}
20
19
  KVS['foo'].should == {:a => 'b', :c => 'd'}
21
20
  File.exists?(KVS.file_of('foo')).should be_true
21
+ KVS['bar'] = 'bar'
22
+ KVS.keys.include?('foo').should be_true
23
+ KVS.keys.include?('bar').should be_true
22
24
  end
23
25
 
24
26
  it 'should store data with "<<"' do
@@ -65,7 +67,7 @@ describe KVS do
65
67
  end
66
68
 
67
69
  describe 'use invalid key' do
68
- it 'should raise ArgumentError' do
70
+ it 'raises ArgumentError' do
69
71
  lambda { KVS['.'] }.should raise_error(ArgumentError)
70
72
  lambda { KVS['../'] }.should raise_error(ArgumentError)
71
73
  lambda { KVS['/'] }.should raise_error(ArgumentError)
@@ -77,9 +79,42 @@ describe KVS do
77
79
  KVS.dir = nil
78
80
  end
79
81
 
80
- it 'should raise ArgumentError' do
82
+ it 'raises ArgumentError' do
81
83
  lambda { KVS['foo'] = {:a => 'b'} }.should raise_error(RuntimeError)
82
84
  lambda { KVS['foo'] }.should raise_error(RuntimeError)
83
85
  end
84
86
  end
87
+
88
+ describe 'instantiate' do
89
+ before do
90
+ @tmpdir_for_foo = Dir.tmpdir + '/kvs_test_foo'
91
+ FileUtils.rm_rf(@tmpdir_for_foo)
92
+ @kvs = KVS.new(@tmpdir_for_foo)
93
+ end
94
+
95
+ it 'generates file path' do
96
+ @kvs.file_of('foo').should == @tmpdir_for_foo + '/foo'
97
+ end
98
+
99
+ it 'stores data' do
100
+ @kvs['bar'] = 'bar'
101
+ @kvs['bar'].should == 'bar'
102
+ KVS['bar'].should_not == 'bar'
103
+ end
104
+ end
105
+
106
+ describe 'KVS#inspect' do
107
+ before do
108
+ @tmpdir = Dir.tmpdir + '/kvs_test'
109
+ FileUtils.rm_rf(@tmpdir)
110
+ KVS.dir = @tmpdir
111
+ end
112
+
113
+ it 'shows the pairs of key and value' do
114
+ KVS.inspect.should == 'KVS()'
115
+
116
+ KVS['foo'] = 'bar'
117
+ KVS.inspect.should == 'KVS("foo": "bar")'
118
+ end
119
+ end
85
120
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kvs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-03 00:00:00 +09:00
12
+ date: 2010-01-07 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -37,7 +37,6 @@ files:
37
37
  - README.markdown
38
38
  - Rakefile
39
39
  - VERSION
40
- - kvs.gemspec
41
40
  - lib/kvs.rb
42
41
  - spec/kvs_spec.rb
43
42
  has_rdoc: true
@@ -1,28 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- Gem::Specification.new do |s|
4
- s.name = %q{kvs}
5
- s.version = "0.3.0"
6
-
7
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["jugyo"]
9
- s.date = %q{2009-09-16}
10
- s.description = %q{KVS is a simple key value store.}
11
- s.email = %q{jugyo.org@gmail.com}
12
- s.files = ["Rakefile", "README.markdown", "ChangeLog", "lib/kvs.rb", "spec/kvs_spec.rb"]
13
- s.homepage = %q{http://github.com/jugyo/kvs}
14
- s.require_paths = ["lib"]
15
- s.rubyforge_project = %q{kvs}
16
- s.rubygems_version = %q{1.3.4}
17
- s.summary = %q{simple key value store.}
18
-
19
- if s.respond_to? :specification_version then
20
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
21
- s.specification_version = 3
22
-
23
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
24
- else
25
- end
26
- else
27
- end
28
- end