ykk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
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,61 @@
1
+ YKK
2
+ ========
3
+
4
+ [http://github.com/jugyo/ykk](http://github.com/jugyo/ykk)
5
+
6
+ Description
7
+ --------
8
+
9
+ YKK is a simple key value store.
10
+
11
+ Install
12
+ --------
13
+
14
+ gem install ykk
15
+
16
+ Usage
17
+ --------
18
+
19
+ use as singleton:
20
+
21
+ YKK.dir = '/tmp/ykk'
22
+
23
+ YKK['foo'] = 'bar'
24
+ puts YKK['foo'] #=> bar
25
+
26
+ key = YKK << 'jugyo'
27
+ puts YKK[key] #=> jugyo
28
+
29
+ YKK.delete('foo')
30
+
31
+ use as instance:
32
+
33
+ YKK_A = YKK.new('/tmp/ykk_a')
34
+ YKK_A['foo'] = 'bar'
35
+ puts YKK_A['foo'] #=> bar
36
+
37
+ License
38
+ --------
39
+
40
+ (The MIT License)
41
+
42
+ Copyright (c) 2008-2009 jugyo
43
+
44
+ Permission is hereby granted, free of charge, to any person obtaining
45
+ a copy of this software and associated documentation files (the
46
+ 'Software'), to deal in the Software without restriction, including
47
+ without limitation the rights to use, copy, modify, merge, publish,
48
+ distribute, sublicense, and/or sell copies of the Software, and to
49
+ permit persons to whom the Software is furnished to do so, subject to
50
+ the following conditions:
51
+
52
+ The above copyright notice and this permission notice shall be
53
+ included in all copies or substantial portions of the Software.
54
+
55
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
56
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
57
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
58
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
59
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
60
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
61
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -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 = "ykk"
8
+ gem.summary = %Q{Simple key value store.}
9
+ gem.description = %Q{YKK is a simple key value store using YAML.}
10
+ gem.email = "jugyo.org@gmail.com"
11
+ gem.homepage = "http://github.com/jugyo/ykk"
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 = "ykk #{version}"
41
+ rdoc.rdoc_files.include('README*')
42
+ rdoc.rdoc_files.include('lib/**/*.rb')
43
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/lib/ykk.rb ADDED
@@ -0,0 +1,75 @@
1
+ require 'digest/sha1'
2
+ require 'yaml'
3
+ require 'fileutils'
4
+
5
+ class YKK
6
+ @instance = self.new
7
+
8
+ def self.method_missing(method, *args, &block)
9
+ if @instance.respond_to?(method)
10
+ @instance.__send__(method, *args, &block)
11
+ else
12
+ super
13
+ end
14
+ end
15
+
16
+ def self.inspect
17
+ @instance.inspect
18
+ end
19
+
20
+ attr_accessor :dir
21
+
22
+ def initialize(dir = nil)
23
+ self.dir = dir
24
+ end
25
+
26
+ def <<(value)
27
+ key = key_gen(value)
28
+ self[key] = value
29
+ key
30
+ end
31
+
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
+ "YKK(#{pairs.join ', '})"
74
+ end
75
+ end
data/spec/ykk_spec.rb ADDED
@@ -0,0 +1,120 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'ykk'
3
+ require 'tmpdir'
4
+ require 'fileutils'
5
+
6
+ describe YKK do
7
+ before do
8
+ @tmpdir = Dir.tmpdir + '/ykk_test'
9
+ FileUtils.rm_rf(@tmpdir)
10
+ YKK.dir = @tmpdir
11
+ end
12
+
13
+ it 'generates file path' do
14
+ YKK.file_of('foo').should == @tmpdir + '/foo'
15
+ end
16
+
17
+ it 'stores data' do
18
+ YKK['foo'] = {:a => 'b', :c => 'd'}
19
+ YKK['foo'].should == {:a => 'b', :c => 'd'}
20
+ File.exists?(YKK.file_of('foo')).should be_true
21
+ YKK['bar'] = 'bar'
22
+ YKK.keys.include?('foo').should be_true
23
+ YKK.keys.include?('bar').should be_true
24
+ end
25
+
26
+ it 'should store data with "<<"' do
27
+ key = YKK << {:a => 'b', :c => 'd'}
28
+ YKK[key].should == {:a => 'b', :c => 'd'}
29
+ File.exists?(YKK.file_of(key)).should be_true
30
+ end
31
+
32
+ it 'can also use Symbol as key' do
33
+ YKK[:foo] = 'test'
34
+ YKK['foo'].should == 'test'
35
+ YKK['bar'] = 'test test'
36
+ YKK[:bar].should == 'test test'
37
+ end
38
+
39
+ describe 'no value exists' do
40
+ it 'should return nil' do
41
+ YKK['x'].should be_nil
42
+ end
43
+ end
44
+
45
+ describe 'delete value' do
46
+ it 'should delete value' do
47
+ YKK['foo'] = 'bar'
48
+ YKK['foo'].should == 'bar'
49
+ YKK.delete('foo')
50
+ YKK['foo'].should be_nil
51
+ File.exists?(YKK.file_of('foo')).should be_false
52
+ end
53
+
54
+ it 'should return nil' do
55
+ YKK['foo'] = 'bar'
56
+ YKK.delete('foo').should be_nil
57
+ YKK.delete('foo').should be_nil
58
+ end
59
+ end
60
+
61
+ describe 'should always return nil' do
62
+ it 'should not raise ArgumentError' do
63
+ lambda { YKK['foo'] }.should_not raise_error(ArgumentError)
64
+ lambda { YKK['_'] }.should_not raise_error(ArgumentError)
65
+ lambda { YKK['1'] }.should_not raise_error(ArgumentError)
66
+ end
67
+ end
68
+
69
+ describe 'use invalid key' do
70
+ it 'raises ArgumentError' do
71
+ lambda { YKK['.'] }.should raise_error(ArgumentError)
72
+ lambda { YKK['../'] }.should raise_error(ArgumentError)
73
+ lambda { YKK['/'] }.should raise_error(ArgumentError)
74
+ end
75
+ end
76
+
77
+ describe 'dir is nil' do
78
+ before do
79
+ YKK.dir = nil
80
+ end
81
+
82
+ it 'raises ArgumentError' do
83
+ lambda { YKK['foo'] = {:a => 'b'} }.should raise_error(RuntimeError)
84
+ lambda { YKK['foo'] }.should raise_error(RuntimeError)
85
+ end
86
+ end
87
+
88
+ describe 'instantiate' do
89
+ before do
90
+ @tmpdir_for_foo = Dir.tmpdir + '/ykk_test_foo'
91
+ FileUtils.rm_rf(@tmpdir_for_foo)
92
+ @ykk = YKK.new(@tmpdir_for_foo)
93
+ end
94
+
95
+ it 'generates file path' do
96
+ @ykk.file_of('foo').should == @tmpdir_for_foo + '/foo'
97
+ end
98
+
99
+ it 'stores data' do
100
+ @ykk['bar'] = 'bar'
101
+ @ykk['bar'].should == 'bar'
102
+ YKK['bar'].should_not == 'bar'
103
+ end
104
+ end
105
+
106
+ describe 'YKK#inspect' do
107
+ before do
108
+ @tmpdir = Dir.tmpdir + '/ykk_test'
109
+ FileUtils.rm_rf(@tmpdir)
110
+ YKK.dir = @tmpdir
111
+ end
112
+
113
+ it 'shows the pairs of key and value' do
114
+ YKK.inspect.should == 'YKK()'
115
+
116
+ YKK['foo'] = 'bar'
117
+ YKK.inspect.should == 'YKK("foo": "bar")'
118
+ end
119
+ end
120
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ykk
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - jugyo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-08 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: YKK is a simple key value store using YAML.
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
+ - lib/ykk.rb
41
+ - spec/ykk_spec.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/jugyo/ykk
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Simple key value store.
70
+ test_files:
71
+ - spec/ykk_spec.rb