object_pool 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
Binary file
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in object_pool.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Nick Hsu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ObjectPool
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'object_pool'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install object_pool
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module ObjectPool
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,49 @@
1
+ require "object_pool/version"
2
+
3
+ module ObjectPool
4
+
5
+ require 'digest/md5'
6
+
7
+ class Pool
8
+ def initialize(pool_path)
9
+ @pool_path = pool_path
10
+ Dir.mkdir(@pool_path) unless Dir.exist?(@pool_path)
11
+ end
12
+
13
+ def put(row_data)
14
+ md5 = Digest::MD5.hexdigest(row_data)
15
+ object_path = get_object_path(md5)
16
+ object_dir = get_object_dir(md5)
17
+ Dir.mkdir(object_dir) unless Dir.exist?(object_dir)
18
+ File.open(object_path, 'w+') { |f| f.write(row_data) }
19
+
20
+ return md5
21
+ end
22
+
23
+ def delete(object_id)
24
+ File.unlink(get_object_path(object_id))
25
+ end
26
+
27
+ def exist? object_id
28
+ File.exist?(get_object_path(object_id))
29
+ end
30
+
31
+ # get object object by object_id, read only
32
+ def get(object_id)
33
+ return File.open(get_object_path(object_id), "r")
34
+ end
35
+
36
+ private
37
+
38
+ def get_object_dir(object_id)
39
+ File.join(@pool_path, object_id[0..1])
40
+ end
41
+
42
+ def get_object_path(object_id)
43
+ File.join(@pool_path, object_id[0..1], object_id)
44
+ end
45
+ end
46
+
47
+
48
+ end
49
+
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'object_pool/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "object_pool"
8
+ gem.version = ObjectPool::VERSION
9
+ gem.authors = ["Nick Hsu"]
10
+ gem.email = ["nickhsu@gmail.com"]
11
+ gem.description = %q{}
12
+ gem.summary = %q{}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,63 @@
1
+ #encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), '..' , 'lib', 'object_pool')
3
+ require 'digest/md5'
4
+
5
+ describe ObjectPool do
6
+ before(:all) do
7
+ @pool_path = '/tmp/object_pool'
8
+ FileUtils.rm_rf @pool_path if Dir.exist? @pool_path
9
+ Dir.mkdir @pool_path
10
+
11
+ @pool = ObjectPool::Pool.new @pool_path
12
+ @fixture_data = "This is a test file."
13
+ @fixture_md5 = Digest::MD5.hexdigest(@fixture_data)
14
+ end
15
+
16
+ describe '#initialize' do
17
+ it 'pool資料夾應該要存在' do
18
+ Dir.exist?(@pool_path).should == true
19
+ end
20
+ end
21
+
22
+ describe '#put' do
23
+ before(:all) do
24
+ @object_id = @pool.put(@fixture_data)
25
+ end
26
+
27
+ it '回傳值應該是資料的MD5' do
28
+ @object_id.should == @fixture_md5
29
+ end
30
+
31
+ it '新增後檔案應該存在' do
32
+ @pool.exist?(@fixture_md5).should == true
33
+ end
34
+ end
35
+
36
+ describe '#get' do
37
+ it '讀取到的資料應該跟寫入的一樣' do
38
+ @pool.get(@fixture_md5).read.should == @fixture_data
39
+ end
40
+
41
+ it "傳入的object_id不存在的時候,應該要丟出Errno::ENOENT" do
42
+ expect {
43
+ @pool.get("hahaha")
44
+ }.to raise_error(Errno::ENOENT)
45
+ end
46
+ end
47
+
48
+ describe '刪除檔案 - delete' do
49
+ before(:all) do
50
+ @pool.delete(@fixture_md5)
51
+ end
52
+
53
+ it '刪除之後檔案應該不存在' do
54
+ @pool.exist?(@fixture_md5).should == false
55
+ end
56
+
57
+ it "刪除的檔案不存在,應該要丟出Errno::ENOENT" do
58
+ expect {
59
+ @pool.delete("hahaha")
60
+ }.to raise_error(Errno::ENOENT)
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: object_pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nick Hsu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ''
15
+ email:
16
+ - nickhsu@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .object_pool.gemspec.swp
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/object_pool.rb
28
+ - lib/object_pool/version.rb
29
+ - object_pool.gemspec
30
+ - spec/object_pool_spec.rb
31
+ homepage: ''
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: ''
55
+ test_files:
56
+ - spec/object_pool_spec.rb