jugyo-kvs 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/ChangeLog +3 -0
  2. data/README.markdown +53 -0
  3. data/Rakefile +56 -0
  4. data/lib/kvs.rb +43 -0
  5. data/spec/kvs_spec.rb +81 -0
  6. metadata +58 -0
data/ChangeLog ADDED
@@ -0,0 +1,3 @@
1
+ == 0.1.0 2009-09-15
2
+
3
+ * first release!
data/README.markdown ADDED
@@ -0,0 +1,53 @@
1
+ KVS
2
+ ========
3
+
4
+ [http://github.com/jugyo/kvs](http://github.com/jugyo/kvs)
5
+
6
+ Description
7
+ --------
8
+
9
+ KVS is a simple key value store.
10
+
11
+ Install
12
+ --------
13
+
14
+ sudo gem install jugyo-kvs --source http://gems.github.com
15
+
16
+ Usage
17
+ --------
18
+
19
+ KVS.dir = '/tmp/kvs'
20
+
21
+ KVS['foo'] = 'bar'
22
+ puts KVS['foo'] #=> bar
23
+
24
+ key = KVS << 'jugyo'
25
+ puts KVS[key] #=> jugyo
26
+
27
+ KVS.delete('foo')
28
+
29
+ License
30
+ --------
31
+
32
+ (The MIT License)
33
+
34
+ Copyright (c) 2008-2009 jugyo
35
+
36
+ Permission is hereby granted, free of charge, to any person obtaining
37
+ a copy of this software and associated documentation files (the
38
+ 'Software'), to deal in the Software without restriction, including
39
+ without limitation the rights to use, copy, modify, merge, publish,
40
+ distribute, sublicense, and/or sell copies of the Software, and to
41
+ permit persons to whom the Software is furnished to do so, subject to
42
+ the following conditions:
43
+
44
+ The above copyright notice and this permission notice shall be
45
+ included in all copies or substantial portions of the Software.
46
+
47
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
48
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
50
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
51
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
52
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
53
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ $:.unshift File.dirname(__FILE__) + '/lib'
2
+ require 'rubygems'
3
+ require 'kvs'
4
+ require 'spec/rake/spectask'
5
+ require 'rake/clean'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+
9
+ name = 'kvs'
10
+ version = KVS::VERSION
11
+
12
+ spec = Gem::Specification.new do |s|
13
+ s.name = name
14
+ s.version = version
15
+ s.summary = "simple key value store."
16
+ s.description = "KVS is a simple key value store."
17
+ s.files = %w(Rakefile README.markdown ChangeLog) + Dir.glob("{lib,spec}/**/*.rb")
18
+ s.authors = %w(jugyo)
19
+ s.email = 'jugyo.org@gmail.com'
20
+ s.homepage = 'http://github.com/jugyo/kvs'
21
+ s.rubyforge_project = 'kvs'
22
+ s.has_rdoc = false
23
+ end
24
+
25
+ Rake::GemPackageTask.new(spec) do |p|
26
+ p.need_tar = true
27
+ end
28
+
29
+ task :gemspec do
30
+ filename = "#{name}.gemspec"
31
+ open(filename, 'w') do |f|
32
+ f.write spec.to_ruby
33
+ end
34
+ puts <<-EOS
35
+ Successfully generated gemspec
36
+ Name: #{name}
37
+ Version: #{version}
38
+ File: #{filename}
39
+ EOS
40
+ end
41
+
42
+ task :install => [ :package ] do
43
+ sh %{sudo gem install pkg/#{name}-#{version}.gem}
44
+ end
45
+
46
+ task :uninstall => [ :clean ] do
47
+ sh %{sudo gem uninstall #{name}}
48
+ end
49
+
50
+ desc 'run all specs'
51
+ Spec::Rake::SpecTask.new do |t|
52
+ t.spec_files = FileList['spec/**/*_spec.rb']
53
+ t.spec_opts = ['-c']
54
+ end
55
+
56
+ CLEAN.include ['pkg']
data/lib/kvs.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'digest/sha1'
2
+ require 'tmpdir'
3
+
4
+ module KVS
5
+ VERSION = '0.2.0'
6
+
7
+ class <<self
8
+ attr_accessor :dir
9
+
10
+ def <<(value)
11
+ key = key_gen(value)
12
+ self[id] = value
13
+ key
14
+ end
15
+
16
+ def [](key)
17
+ path = file_of(key)
18
+ return nil unless File.exists?(path)
19
+ File.read(path)
20
+ end
21
+
22
+ def []=(key, value)
23
+ File.open(file_of(key), 'wb') { |f| f << value.to_s }
24
+ end
25
+
26
+ def delete(key)
27
+ path = file_of(key)
28
+ File.delete(path) if File.exists?(path)
29
+ nil
30
+ end
31
+
32
+ def file_of(key)
33
+ key = key.to_s
34
+ raise ArgumentError, 'invalid key' unless key =~ /^[\w]+$/
35
+ raise "dir is not specified" unless dir
36
+ File.join(dir, key)
37
+ end
38
+
39
+ def key_gen(value)
40
+ Digest::SHA1.hexdigest(value.to_s)
41
+ end
42
+ end
43
+ end
data/spec/kvs_spec.rb ADDED
@@ -0,0 +1,81 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'kvs'
3
+
4
+ describe KVS do
5
+ before do
6
+ @tmpdir = Dir.tmpdir
7
+ KVS.dir = @tmpdir
8
+ end
9
+
10
+ it 'should generate file path' do
11
+ KVS.file_of('foo').should == @tmpdir + '/foo'
12
+ end
13
+
14
+ it 'should store data' do
15
+ KVS['foo'] = 'bar'
16
+ KVS['foo'].should == 'bar'
17
+ File.exists?(KVS.file_of('foo')).should be_true
18
+ end
19
+
20
+ it 'should store data with "<<"' do
21
+ key = KVS << 'test'
22
+ KVS[key].should == 'test'
23
+ File.exists?(KVS.file_of(key)).should be_true
24
+ end
25
+
26
+ it 'can also use Symbol as key' do
27
+ KVS[:foo] = 'test'
28
+ KVS['foo'].should == 'test'
29
+ KVS['bar'] = 'test test'
30
+ KVS[:bar].should == 'test test'
31
+ end
32
+
33
+ describe 'no value exists' do
34
+ it 'should return nil' do
35
+ KVS['x'].should be_nil
36
+ end
37
+ end
38
+
39
+ describe 'delete value' do
40
+ it 'should delete value' do
41
+ KVS['foo'] = 'bar'
42
+ KVS['foo'].should == 'bar'
43
+ KVS.delete('foo')
44
+ KVS['foo'].should be_nil
45
+ File.exists?(KVS.file_of('foo')).should be_false
46
+ end
47
+
48
+ it 'should return nil' do
49
+ KVS['foo'] = 'bar'
50
+ KVS.delete('foo').should be_nil
51
+ KVS.delete('foo').should be_nil
52
+ end
53
+ end
54
+
55
+ describe 'should always return nil' do
56
+ it 'should not raise ArgumentError' do
57
+ lambda { KVS['foo'] }.should_not raise_error(ArgumentError)
58
+ lambda { KVS['_'] }.should_not raise_error(ArgumentError)
59
+ lambda { KVS['1'] }.should_not raise_error(ArgumentError)
60
+ end
61
+ end
62
+
63
+ describe 'use invalid key' do
64
+ it 'should raise ArgumentError' do
65
+ lambda { KVS['.'] }.should raise_error(ArgumentError)
66
+ lambda { KVS['../'] }.should raise_error(ArgumentError)
67
+ lambda { KVS['/'] }.should raise_error(ArgumentError)
68
+ end
69
+ end
70
+
71
+ describe 'dir is nil' do
72
+ before do
73
+ KVS.dir = nil
74
+ end
75
+
76
+ it 'should raise ArgumentError' do
77
+ lambda { KVS['foo'] = 'bar' }.should raise_error(RuntimeError)
78
+ lambda { KVS['foo'] }.should raise_error(RuntimeError)
79
+ end
80
+ end
81
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jugyo-kvs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - jugyo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-15 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: KVS is a simple key value store.
17
+ email: jugyo.org@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - Rakefile
26
+ - README.markdown
27
+ - ChangeLog
28
+ - lib/kvs.rb
29
+ - spec/kvs_spec.rb
30
+ has_rdoc: false
31
+ homepage: http://github.com/jugyo/kvs
32
+ licenses:
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project: kvs
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: simple key value store.
57
+ test_files: []
58
+