kvs 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ pkg
@@ -0,0 +1,3 @@
1
+ == 0.1.0 2009-09-15
2
+
3
+ * first release!
@@ -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.
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "kvs"
8
+ gem.summary = %Q{Key Value Store.}
9
+ gem.description = %Q{KVS is a simple key value store.}
10
+ gem.email = "jugyo.org@gmail.com"
11
+ gem.homepage = "http://github.com/jugyo/kvs"
12
+ gem.authors = ["jugyo"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+ task :spec => :check_dependencies
32
+
33
+ task :default => :spec
34
+
35
+ require 'rake/rdoctask'
36
+ Rake::RDocTask.new do |rdoc|
37
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
38
+
39
+ rdoc.rdoc_dir = 'rdoc'
40
+ rdoc.title = "kvs #{version}"
41
+ rdoc.rdoc_files.include('README*')
42
+ rdoc.rdoc_files.include('lib/**/*.rb')
43
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.1
@@ -0,0 +1,28 @@
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
@@ -0,0 +1,41 @@
1
+ require 'digest/sha1'
2
+ require 'yaml'
3
+
4
+ module KVS
5
+ class <<self
6
+ attr_accessor :dir
7
+
8
+ def <<(value)
9
+ key = key_gen(value)
10
+ self[key] = value
11
+ key
12
+ end
13
+
14
+ def [](key)
15
+ path = file_of(key)
16
+ return nil unless File.exists?(path)
17
+ YAML.load(File.read(path))
18
+ end
19
+
20
+ def []=(key, value)
21
+ File.open(file_of(key), 'wb') { |f| f << value.to_yaml }
22
+ end
23
+
24
+ def delete(key)
25
+ path = file_of(key)
26
+ File.delete(path) if File.exists?(path)
27
+ nil
28
+ end
29
+
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
36
+
37
+ def key_gen(value)
38
+ Digest::SHA1.hexdigest(value.to_s)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,85 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'kvs'
3
+ require 'tmpdir'
4
+ require 'fileutils'
5
+
6
+ describe KVS do
7
+ before do
8
+ @tmpdir = Dir.tmpdir + '/kvs_test'
9
+ FileUtils.rm_rf(@tmpdir)
10
+ FileUtils.mkdir_p(@tmpdir)
11
+ KVS.dir = @tmpdir
12
+ end
13
+
14
+ it 'should generate file path' do
15
+ KVS.file_of('foo').should == @tmpdir + '/foo'
16
+ end
17
+
18
+ it 'should store data' do
19
+ KVS['foo'] = {:a => 'b', :c => 'd'}
20
+ KVS['foo'].should == {:a => 'b', :c => 'd'}
21
+ File.exists?(KVS.file_of('foo')).should be_true
22
+ end
23
+
24
+ it 'should store data with "<<"' do
25
+ key = KVS << {:a => 'b', :c => 'd'}
26
+ KVS[key].should == {:a => 'b', :c => 'd'}
27
+ File.exists?(KVS.file_of(key)).should be_true
28
+ end
29
+
30
+ it 'can also use Symbol as key' do
31
+ KVS[:foo] = 'test'
32
+ KVS['foo'].should == 'test'
33
+ KVS['bar'] = 'test test'
34
+ KVS[:bar].should == 'test test'
35
+ end
36
+
37
+ describe 'no value exists' do
38
+ it 'should return nil' do
39
+ KVS['x'].should be_nil
40
+ end
41
+ end
42
+
43
+ describe 'delete value' do
44
+ it 'should delete value' do
45
+ KVS['foo'] = 'bar'
46
+ KVS['foo'].should == 'bar'
47
+ KVS.delete('foo')
48
+ KVS['foo'].should be_nil
49
+ File.exists?(KVS.file_of('foo')).should be_false
50
+ end
51
+
52
+ it 'should return nil' do
53
+ KVS['foo'] = 'bar'
54
+ KVS.delete('foo').should be_nil
55
+ KVS.delete('foo').should be_nil
56
+ end
57
+ end
58
+
59
+ describe 'should always return nil' do
60
+ it 'should not raise ArgumentError' do
61
+ lambda { KVS['foo'] }.should_not raise_error(ArgumentError)
62
+ lambda { KVS['_'] }.should_not raise_error(ArgumentError)
63
+ lambda { KVS['1'] }.should_not raise_error(ArgumentError)
64
+ end
65
+ end
66
+
67
+ describe 'use invalid key' do
68
+ it 'should raise ArgumentError' do
69
+ lambda { KVS['.'] }.should raise_error(ArgumentError)
70
+ lambda { KVS['../'] }.should raise_error(ArgumentError)
71
+ lambda { KVS['/'] }.should raise_error(ArgumentError)
72
+ end
73
+ end
74
+
75
+ describe 'dir is nil' do
76
+ before do
77
+ KVS.dir = nil
78
+ end
79
+
80
+ it 'should raise ArgumentError' do
81
+ lambda { KVS['foo'] = {:a => 'b'} }.should raise_error(RuntimeError)
82
+ lambda { KVS['foo'] }.should raise_error(RuntimeError)
83
+ end
84
+ end
85
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kvs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ platform: ruby
6
+ authors:
7
+ - jugyo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-03 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ description: KVS is a simple key value store.
26
+ email: jugyo.org@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - ChangeLog
33
+ - README.markdown
34
+ files:
35
+ - .gitignore
36
+ - ChangeLog
37
+ - README.markdown
38
+ - Rakefile
39
+ - VERSION
40
+ - kvs.gemspec
41
+ - lib/kvs.rb
42
+ - spec/kvs_spec.rb
43
+ has_rdoc: true
44
+ homepage: http://github.com/jugyo/kvs
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.5
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Key Value Store.
71
+ test_files:
72
+ - spec/kvs_spec.rb