vfs 0.3.13 → 0.3.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,7 +40,8 @@ module Vfs
40
40
  raise Error, "entry #{self} already exist!"
41
41
  end
42
42
  elsif attrs[:dir]
43
- # do nothing
43
+ # dir already exist, no need to recreate it
44
+ return self
44
45
  else
45
46
  parent = self.parent
46
47
  if parent.exist?
@@ -51,7 +52,7 @@ module Vfs
51
52
  end
52
53
  end
53
54
 
54
- retry if try < 2
55
+ try < 2 ? retry : raise(error)
55
56
  end
56
57
  end
57
58
  self
@@ -53,41 +53,46 @@ module Vfs
53
53
  end
54
54
 
55
55
  def write *args, &block
56
+ if block
57
+ options = args.first || {}
58
+ else
59
+ data, options = *args
60
+ options ||= {}
61
+ end
62
+ raise "can't do :override and :append at the same time!" if options[:override] and options[:append]
63
+
56
64
  storage.open_fs do |fs|
65
+ # TODO2 Performance lost, extra call to check file existence
66
+ # We need to check if the file exist before writing to it, otherwise it's
67
+ # impossible to distinguish if the StandardError caused by the 'already exist' error or
68
+ # some other error.
69
+ entry = self.entry
70
+ if entry.exist?
71
+ if options[:override]
72
+ entry.destroy
73
+ else
74
+ raise Error, "entry #{self} already exist!"
75
+ end
76
+ end
77
+
57
78
  try = 0
58
79
  begin
59
80
  try += 1
60
- if block
61
- options = args.first || {}
62
- else
63
- data, options = *args
64
- options ||= {}
65
- end
66
- raise "can't do :override and :append at the same time!" if options[:override] and options[:append]
67
81
  if block
68
82
  fs.write_file(path, options[:append], &block)
69
83
  else
70
84
  fs.write_file(path, options[:append]){|writer| writer.write data}
71
85
  end
72
86
  rescue StandardError => error
73
- entry = self.entry
74
- if entry.exist?
75
- if options[:override]
76
- entry.destroy
77
- else
78
- raise Error, "entry #{self} already exist!"
79
- end
87
+ parent = self.parent
88
+ if parent.exist?
89
+ # some unknown error
90
+ raise error
80
91
  else
81
- parent = self.parent
82
- if parent.exist?
83
- # some unknown error
84
- raise error
85
- else
86
- parent.create(options)
87
- end
92
+ parent.create(options)
88
93
  end
89
94
 
90
- retry if try < 2
95
+ try < 2 ? retry : raise(error)
91
96
  end
92
97
  end
93
98
  self
@@ -98,6 +103,18 @@ module Vfs
98
103
  write *args, &block
99
104
  end
100
105
 
106
+ def append *args, &block
107
+ options = (args.last.is_a?(Hash) && args.pop) || {}
108
+ options[:append] = true
109
+ write(*(args << options), &block)
110
+ end
111
+
112
+ def update options = {}, &block
113
+ options[:override] = true
114
+ data = read options
115
+ write block.call(data), options
116
+ end
117
+
101
118
  def destroy options = {}
102
119
  storage.open_fs do |fs|
103
120
  begin
@@ -126,24 +143,6 @@ module Vfs
126
143
  destroy options
127
144
  end
128
145
 
129
- def append *args, &block
130
- if block
131
- options = args.first || {}
132
- else
133
- data, options = *args
134
- options ||= {}
135
- end
136
-
137
- options[:append] = true
138
- write data, options, &block
139
- end
140
-
141
- def update options = {}, &block
142
- options[:override] = true
143
- data = read options
144
- write block.call(data), options
145
- end
146
-
147
146
 
148
147
  #
149
148
  # Transfers
@@ -12,7 +12,7 @@ module Vfs
12
12
  #
13
13
  # CRUD
14
14
  #
15
- def destroy
15
+ def destroy options = {}
16
16
  storage.open_fs do |fs|
17
17
  attrs = get
18
18
  fs.delete_dir path if attrs[:dir]
@@ -20,5 +20,6 @@ module Vfs
20
20
  end
21
21
  self
22
22
  end
23
+ alias_method :destroy!, :destroy
23
24
  end
24
25
  end
@@ -1,10 +1,9 @@
1
1
  #
2
- # It allows you dynamically (magically) switch between UniversalEntry/Dir/File
2
+ # It allows dynamically (magically) switching between UniversalEntry/Dir/File
3
3
  #
4
4
  module Vfs
5
5
  class EntryProxy < BasicObject
6
6
  attr_reader :_target
7
- # WRAP = [:[], :entry, :dir, :file].to_set
8
7
 
9
8
  def initialize entry
10
9
  raise 'something wrong happening here!' if entry.respond_to?(:proxy?) and entry.proxy?
@@ -31,8 +30,6 @@ module Vfs
31
30
  end
32
31
 
33
32
  _target.send m, *a, &b
34
-
35
- # return WRAP.include?(m) ? EntryProxy.new(result) : result
36
33
  end
37
34
  end
38
35
  end
@@ -3,7 +3,8 @@ class String
3
3
  path = self
4
4
  storage ||= Vfs.default_storage
5
5
 
6
- Vfs::Dir.new(storage, '/')[path]
6
+ path = "./#{path}" unless path =~ /^[\/\.\~]/
7
+ Vfs::Dir.new(storage, path)
7
8
  end
8
9
  alias_method :to_entry, :to_entry_on
9
10
 
@@ -1,17 +1,12 @@
1
- warn 'remove trailing spaces'
2
1
  require 'tempfile'
3
2
 
4
3
  module Vfs
5
4
  module Storages
6
5
  class Local
7
6
  class Writer
8
- def initialize out
9
- @out = out
10
- end
7
+ def initialize out; @out = out end
11
8
 
12
- def write data
13
- @out.write data
14
- end
9
+ def write data; @out.write data end
15
10
  end
16
11
 
17
12
  module LocalVfsHelper
@@ -133,7 +128,7 @@ module Vfs
133
128
  def local?; true end
134
129
 
135
130
  def tmp &block
136
- tmp_dir = "#{::Dir.tmpdir}/#{rand(10**3)}"
131
+ tmp_dir = "#{::Dir.tmpdir}/#{rand(10**6)}"
137
132
  if block
138
133
  begin
139
134
  create_dir tmp_dir
data/readme.md CHANGED
@@ -32,32 +32,23 @@ $ gem install vos
32
32
  ## Code samples:
33
33
 
34
34
  ``` ruby
35
- gem 'vfs' # Virtual File System
36
- require 'vfs'
37
-
38
- gem 'vos' # Virtual Operating System
39
- require 'vos'
40
- ```
35
+ require 'vfs' # Virtual File System
36
+ require 'vos' # Virtual Operating System
41
37
 
42
38
  # Connections, let's deploy our 'cool_app' project from our local box to remote server
43
39
 
44
- ``` ruby
45
40
  server = Box.new('cool_app.com') # it will use id_rsa, or You can add {user: 'me', password: 'secret'}
46
41
  me = '~'.to_dir # handy shortcut for local FS
47
42
 
48
43
  deploy_dir = server['apps/cool_app']
49
44
  projects = me['projects']
50
- ```
51
45
 
52
46
  # Working with dirs, copying dir from any source to any destination (local/remote/custom_storage_type)
53
47
 
54
- ``` ruby
55
48
  projects['cool_app'].copy_to deploy_dir
56
- ```
57
49
 
58
50
  # Working with files
59
51
 
60
- ``` ruby
61
52
  dbc = deploy_dir.file('config/database.yml') # <= the 'config' dir not exist yet
62
53
  dbc.write("user: root\npassword: secret") # <= now the 'database.yml' and parent 'config' has been created
63
54
  dbc.content =~ /database/ # => false, we forgot to add the database
@@ -74,15 +65,19 @@ projects['cool_app/config/database.yml']. # or just overwrite it with our loc
74
65
  There are also streaming support (read/write/append) with &block, please go to specs for details
75
66
 
76
67
  # Checks
68
+
69
+ ``` ruby
77
70
  deploy_dir['config'].exist? # => true
78
71
  deploy_dir.dir('config').exist? # => true
79
72
  deploy_dir.file('config').exist? # => false
80
73
 
81
74
  deploy_dir['config'].dir? # => true
82
75
  deploy_dir['config'].file? # => false
83
-
76
+ ```
84
77
 
85
78
  # Navigation
79
+
80
+ ``` ruby
86
81
  config = deploy_dir['config']
87
82
  config.parent # => </apps/cool_app>
88
83
  config['../..'] # => </>
@@ -115,9 +110,9 @@ is to provide 1-to-1 clone of underlying OS API, instead of provididing handy to
115
110
  And if you want to use remote FS - things are getting even worse and more complicated (Net::SSH & Net::SFTP use a little
116
111
  different API than local FS, and you has to remember all thouse little quirks).
117
112
 
118
- Copyright (c) Alexey Petrushin http://petrush.in, released under the MIT license.
113
+ ## License
119
114
 
120
- ## Please let me know about bugs and Your proposals, there's the 'Issues' tab at the top, feel free to submit.
115
+ Copyright (c) Alexey Petrushin http://petrush.in, released under the MIT license.
121
116
 
122
117
  [vos]: http://github.com/alexeypetrushin/vos
123
118
  [cluster_management]: http://github.com/alexeypetrushin/cluster_management
data/spec/entry_spec.rb CHANGED
@@ -11,6 +11,11 @@ describe 'Entry' do
11
11
  @path.name.should == 'c'
12
12
  end
13
13
 
14
+ it "string integration" do
15
+ '/'.to_entry.path.should == '/'
16
+ 'a'.to_entry.path.should == "./a"
17
+ end
18
+
14
19
  it 'tmp' do
15
20
  tmp = test_fs.tmp
16
21
  tmp.should be_dir
data/spec/file_spec.rb CHANGED
@@ -100,6 +100,11 @@ describe 'File' do
100
100
  file.should_receive(:write).with('something', append: true)
101
101
  file.append 'something'
102
102
  end
103
+
104
+ it 'should correctly display errors (from error)' do
105
+ -> {test_fs['test'].write{|writer| raise 'some error'}}.should raise_error(/some error/)
106
+ -> {test_fs['test'].write!{|writer| raise 'some error'}}.should raise_error(/some error/)
107
+ end
103
108
  end
104
109
 
105
110
  it 'update' do
@@ -31,6 +31,10 @@ describe 'UniversalEntry' do
31
31
  @path.should be_file
32
32
  @path.destroy
33
33
  @path.should_not exist
34
+
35
+ @path.file.create
36
+ @path.destroy!
37
+ @path.file.should_not exist
34
38
  end
35
39
 
36
40
  it "shouldn't raise if file not exist" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vfs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.13
4
+ version: 0.3.14
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: 2011-08-10 00:00:00.000000000Z
12
+ date: 2011-08-20 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: