omnistore 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -6,8 +6,17 @@ module OmniStore
6
6
  class Mountpoint
7
7
  attr_reader :dir
8
8
 
9
- def initialize(dir)
10
- @dir = dir
9
+ def initialize(name, dir)
10
+ @name = name
11
+ @dir = dir
12
+ end
13
+
14
+ def name
15
+ @name
16
+ end
17
+
18
+ def url
19
+ "file://#{dir}"
11
20
  end
12
21
 
13
22
  def exist?(path)
@@ -34,22 +43,30 @@ module OmniStore
34
43
  def mount!
35
44
  @@mountpoint = {}
36
45
  case mountpoint = OmniStore::Config.mountpoint
37
- when Array then mountpoint.each {|m| validate(m); @@mountpoint[m] = Mountpoint.new(m) }
38
- when Hash then mountpoint.each {|k,v| validate(v); @@mountpoint[k] = Mountpoint.new(v) }
39
- else m = mountpoint.to_s; validate(m); @@mountpoint[m] = Mountpoint.new(m)
46
+ when Array then mountpoint.each {|m| validate(m); @@mountpoint[m] = Mountpoint.new(File.basename(m), m) }
47
+ when Hash then mountpoint.each {|k,v| validate(v); @@mountpoint[k] = Mountpoint.new(k, v) }
48
+ else m = mountpoint.to_s; validate(m); @@mountpoint[m] = Mountpoint.new(File.basename(m), m)
40
49
  end
41
50
  end
42
51
 
43
- def mountpoint(key)
52
+ def mountpoint(key = @@mountpoint.keys[0])
44
53
  @@mountpoint[key]
45
54
  end
46
55
 
47
- def exist?(path)
48
- @@mountpoint.values.find {|m| m.exist?(path) }
56
+ def exist?(path, mp = mountpoint)
57
+ mp.exist?(path)
49
58
  end
50
59
 
51
- def delete(path)
52
- @@mountpoint.values.each {|m| m.delete(path) }
60
+ def delete(path, mp = mountpoint)
61
+ mp.delete(path)
62
+ end
63
+
64
+ def each(&block)
65
+ if block_given?
66
+ @@mountpoint.each{|m| yield m }
67
+ else
68
+ Enumerator.new(@@mountpoint.values)
69
+ end
53
70
  end
54
71
 
55
72
  private
@@ -8,10 +8,19 @@ module OmniStore
8
8
  class Mountpoint
9
9
  attr_reader :bucket
10
10
 
11
- def initialize(bucket)
11
+ def initialize(name, bucket)
12
+ @name = name
12
13
  @bucket = bucket
13
14
  end
14
15
 
16
+ def name
17
+ @name
18
+ end
19
+
20
+ def url
21
+ bucket.url
22
+ end
23
+
15
24
  def exist?(key)
16
25
  bucket.objects[key].exists?
17
26
  end
@@ -20,7 +29,7 @@ module OmniStore
20
29
  bucket.objects[key].delete(options)
21
30
  end
22
31
 
23
- def write(key, options_or_data = nil, options = nil)
32
+ def write(key, options_or_data = nil, options = {})
24
33
  bucket.objects[key].write(options_or_data, options)
25
34
  end
26
35
 
@@ -33,27 +42,35 @@ module OmniStore
33
42
  def mount!
34
43
  @@buckets = {}
35
44
  case mountpoint = OmniStore::Config.mountpoint
36
- when Array then mountpoint.each {|m| b = validate(m); @@buckets[m] = Mountpoint.new(b) }
37
- when Hash then mountpoint.each {|k,v| b = validate(v); @@buckets[k] = Mountpoint.new(b) }
38
- else m = mountpoint.to_s; b = validate(m); @@buckets[m] = Mountpoint.new(b)
45
+ when Array then mountpoint.each {|m| b = validate(m); @@buckets[m] = Mountpoint.new(m, b) }
46
+ when Hash then mountpoint.each {|k,v| b = validate(v); @@buckets[k] = Mountpoint.new(k, b) }
47
+ else m = mountpoint.to_s; b = validate(m); @@buckets[m] = Mountpoint.new(m, b)
39
48
  end
40
49
  end
41
50
 
42
- def mountpoint(key)
51
+ def mountpoint(key = @@buckets.keys[0])
43
52
  @@buckets[key]
44
53
  end
45
54
 
46
- def exist?(path)
47
- @@buckets.values.find {|b| b.exist?(path) }
55
+ def exist?(path, mp = mountpoint)
56
+ mp.exist?(path)
48
57
  end
49
58
  alias :find :exist?
50
59
 
51
- def delete(path, options = {})
52
- @@buckets.values.each {|b| b.delete(path, options) }
60
+ def delete(path, options = {}, mp = mountpoint)
61
+ mp.delete(path, options)
53
62
  end
54
63
 
55
- def write(path, options_or_data = nil, options = nil)
56
- @@buckets.values.each {|b| b.write(path, options_or_data, options) }
64
+ def write(path, options_or_data = nil, options = {}, mp = mountpoint)
65
+ mp.write(path, options_or_data, options)
66
+ end
67
+
68
+ def each(&block)
69
+ if block_given?
70
+ @@buckets.each{|b| yield b }
71
+ else
72
+ Enumerator.new(@@buckets.values)
73
+ end
57
74
  end
58
75
 
59
76
  private
@@ -1,3 +1,3 @@
1
1
  module OmniStore
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -2,118 +2,118 @@ require 'spec_helper'
2
2
  require 'fileutils'
3
3
 
4
4
  describe OmniStore::Storage::Local do
5
-
6
- def expand_path(path, mountpoint = OmniStore::Config.mountpoint)
7
- File.expand_path(path, mountpoint)
8
- end
9
-
10
5
  before(:each) do
11
6
  OmniStore::Config.storage = 'local'
12
7
  OmniStore::Config.mountpoint = MOUNTPOINT
8
+ OmniStore::Storage.remount!
13
9
  end
14
10
 
15
- describe "#mount!" do
16
- it 'should not raise error' do
17
- OmniStore::Config.mountpoint = MOUNTPOINT
18
- lambda { OmniStore::Storage::Local.mount! }.should_not raise_error
11
+ let(:src) { TEST_FILENAME }
12
+ let(:src_fullpath) { expand_path(src, MOUNTPOINT) }
13
+
14
+ describe '#mount!' do
15
+ subject { lambda { OmniStore::Storage::Local.mount! } }
16
+
17
+ context 'when specified a directory path that exists' do
18
+ it { should_not raise_error }
19
+ end
20
+
21
+ context 'when specified two directory paths that exists' do
22
+ before { OmniStore::Config.mountpoint = { :a => MOUNTPOINT, :b => TMPDIR } }
23
+ it { should_not raise_error }
19
24
  end
20
25
 
21
- it 'should not raise error when hash mountpoint' do
22
- OmniStore::Config.mountpoint = { :a => MOUNTPOINT, :b => TMPDIR }
23
- lambda { OmniStore::Storage::Local.mount! }.should_not raise_error
26
+ context 'when specified a directory path that does not exists' do
27
+ before { OmniStore::Config.mountpoint = MOUNTPOINT + Time.new.to_i.to_s }
28
+ it { should raise_error }
24
29
  end
25
30
 
26
- it 'should raise error when mount fails' do
27
- OmniStore::Config.mountpoint = nil
28
- lambda { OmniStore::Storage::Local.mount! }.should raise_error(OmniStore::Errors::InvalidMountpoint)
31
+ context 'when specified a file path that exists' do
32
+ before { OmniStore::Config.mountpoint = File.expand_path(__FILE__) }
33
+ it { should raise_error }
29
34
  end
30
35
  end
31
36
 
32
- context "single mountpoint" do
33
- let(:src) { 'test.txt' }
34
- let(:src_fullpath) { expand_path(src) }
37
+ describe '#mountpoint' do
38
+ subject { OmniStore::Storage::Local.mountpoint }
39
+ it { should be_a OmniStore::Storage::Local::Mountpoint }
40
+ end
41
+
42
+ describe '#exist?' do
43
+ subject { OmniStore::Storage::Local.exist?(src) }
35
44
 
36
- before(:each) do
37
- OmniStore::Config.mountpoint = MOUNTPOINT
38
- OmniStore::Storage.remount!
39
- FileUtils.touch(src_fullpath)
45
+ context 'when specified a file path that does not exist' do
46
+ let(:src) { TEST_FILENAME + Time.new.to_i.to_s }
47
+ it { should be_false }
40
48
  end
41
- after(:each) { FileUtils.rm_f(src_fullpath) }
42
49
 
43
- describe "#exist?" do
44
- it 'should return true' do
45
- OmniStore::Storage::Local.exist?(src).should be_true
46
- end
50
+ context 'when specified a file path that exist' do
51
+ it { should be_true }
52
+ end
53
+ end
47
54
 
48
- it 'should return false' do
49
- OmniStore::Storage::Local.exist?(src + '.bk').should be_false
50
- end
55
+ describe '#delete' do
56
+ subject { lambda { OmniStore::Storage::Local.delete(src) } }
57
+
58
+ context 'when specified a file path that does not exist' do
59
+ let(:src) { TEST_FILENAME + Time.new.to_i.to_s }
60
+ it { should raise_error }
51
61
  end
52
62
 
53
- describe "#delete" do
54
- it 'should return true' do
55
- lambda { OmniStore::Storage::Local.delete(src) }.should_not raise_error
56
- File.exist?(src_fullpath).should be_false
57
- end
63
+ context 'when specified a file path that exist' do
64
+ let(:src) { t = Tempfile.new(TEST_FILENAME, MOUNTPOINT); File.basename(t.path) }
65
+ it { should_not raise_error }
58
66
  end
67
+ end
59
68
 
60
- describe "#move" do
61
- let(:dst) { src + '.mv' }
62
- let(:dst_fullpath) { expand_path(dst) }
63
- let(:mountpoint) { OmniStore::Storage::Local.mountpoint(MOUNTPOINT) }
64
- after(:each) { FileUtils.rm_f(dst_fullpath) }
69
+ describe '#each' do
70
+ subject { OmniStore::Storage::Local.each }
71
+ it { should be_a Enumerator }
72
+ end
65
73
 
66
- it 'should move to target path' do
67
- lambda { mountpoint.move(src, dst) }.should_not raise_error
68
- File.exist?(src_fullpath).should be_false
69
- File.exist?(dst_fullpath).should be_true
70
- end
74
+ describe OmniStore::Storage::Local::Mountpoint do
75
+ subject { OmniStore::Storage::Local.mountpoint }
71
76
 
72
- it 'should raise error when source file is not exists' do
73
- lambda { mountpoint.move(dst, src) }.should raise_error
74
- end
77
+ context 'when single mountpoit' do
78
+ its(:name) { should eq File.basename(MOUNTPOINT) }
79
+ its(:url) { should eq "file://#{File.expand_path(MOUNTPOINT)}" }
75
80
  end
76
- end
77
81
 
78
- context "dounble mountpoint" do
79
- let(:src) { 'test.txt' }
80
- let(:src_fullpath) { expand_path(src, MOUNTPOINT) }
82
+ context 'when double mountpoint' do
83
+ before do
84
+ OmniStore::Config.mountpoint = { :a => MOUNTPOINT, :b => TMPDIR }
85
+ OmniStore::Storage.remount!
86
+ end
87
+ subject { OmniStore::Storage::Local.mountpoint(:b) }
81
88
 
82
- before(:each) do
83
- OmniStore::Config.mountpoint = { :a => MOUNTPOINT, :b => TMPDIR }
84
- OmniStore::Storage.remount!
85
- FileUtils.touch(src_fullpath)
89
+ its(:name) { should eq :b }
90
+ its(:url) { should eq "file://#{File.expand_path(TMPDIR)}" }
86
91
  end
87
- after(:each) { FileUtils.rm_f(src_fullpath) }
88
92
 
89
- describe "#exist?" do
90
- it 'should return true' do
91
- OmniStore::Storage::Local.exist?(src).should be_true
93
+ describe '#move' do
94
+ let(:src) { t = Tempfile.new(TEST_FILENAME, MOUNTPOINT); File.basename(t.path) }
95
+ let(:dst) { TEST_FILENAME + Time.new.to_i.to_s }
96
+ let(:other) { OmniStore::Storage::Local.mountpoint(:a) }
97
+ subject { lambda { OmniStore::Storage::Local.mountpoint.move(src, dst, other) } }
98
+
99
+ before do
100
+ OmniStore::Config.mountpoint = { :a => MOUNTPOINT, :b => TMPDIR }
101
+ OmniStore::Storage.remount!
92
102
  end
103
+ after { other.delete(dst) rescue nil }
93
104
 
94
- it 'should return false' do
95
- OmniStore::Storage::Local.exist?(src + '.bk').should be_false
105
+ context 'when specified a file path that does not exist' do
106
+ let(:src) { TEST_FILENAME + Time.new.to_i.to_s }
107
+ it { should raise_error }
96
108
  end
97
- end
98
109
 
99
- describe "#delete" do
100
- it 'should return true' do
101
- lambda { OmniStore::Storage::Local.delete(src) }.should raise_error
110
+ context 'when specified a file path that exist' do
111
+ it { should_not raise_error }
102
112
  end
103
- end
104
113
 
105
- describe "#move" do
106
- let(:dst) { src + '.mv' }
107
- let(:dst_fullpath) { expand_path(dst, TMPDIR) }
108
- let(:mountpoint) { OmniStore::Storage::Local.mountpoint(:a) }
109
- let(:another) { OmniStore::Storage::Local.mountpoint(:b) }
110
- after(:each) { FileUtils.rm_f(dst_fullpath) }
111
-
112
- it 'should move to target path of another mountpoint' do
113
- lambda { mountpoint.move(src, dst, another) }.should_not raise_error
114
- File.exist?(src_fullpath).should be_false
115
- File.exist?(File.join(MOUNTPOINT, dst)).should be_false
116
- File.exist?(File.join(TMPDIR, dst)).should be_true
114
+ context 'when move to another mountpoint' do
115
+ let(:other) { OmniStore::Storage::Local.mountpoint(:b) }
116
+ it { should_not raise_error }
117
117
  end
118
118
  end
119
119
  end
@@ -3,11 +3,14 @@ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
3
 
4
4
  require 'rubygems'
5
5
  require 'rspec'
6
+ require 'tempfile'
6
7
  require 'aws-sdk'
7
8
  require 'omnistore'
8
9
 
9
10
  TMPDIR = ENV['TMPDIR'] || ENV['TMP'] || ENV['TEMP'] || '/tmp'
10
- MOUNTPOINT = File.join(File.dirname(__FILE__), '/../data')
11
+ #MOUNTPOINT = File.join(File.dirname(__FILE__), '/../data')
12
+ MOUNTPOINT = File.expand_path(File.join(File.dirname(__FILE__), '/../data'))
13
+ TEST_FILENAME = 'test.txt'
11
14
  AWS_BUCKET = ENV['AWS_BUCKET']
12
15
 
13
16
  OmniStore.configure do |config|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omnistore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-08 00:00:00.000000000 Z
12
+ date: 2012-08-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
@@ -89,6 +89,7 @@ files:
89
89
  - README.md
90
90
  - Rakefile
91
91
  - data/.gitkeep
92
+ - data/test.txt
92
93
  - lib/omnistore.rb
93
94
  - lib/omnistore/config.rb
94
95
  - lib/omnistore/config/option.rb
@@ -118,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
119
  version: '0'
119
120
  segments:
120
121
  - 0
121
- hash: -704812863
122
+ hash: 411340815
122
123
  required_rubygems_version: !ruby/object:Gem::Requirement
123
124
  none: false
124
125
  requirements:
@@ -127,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  version: '0'
128
129
  segments:
129
130
  - 0
130
- hash: -704812863
131
+ hash: 411340815
131
132
  requirements: []
132
133
  rubyforge_project:
133
134
  rubygems_version: 1.8.24