vfs 0.3.12 → 0.3.13

Sign up to get free protection for your applications and to get access to all the features.
data/lib/vfs.rb CHANGED
@@ -3,18 +3,18 @@
3
3
 
4
4
  path
5
5
  error
6
-
6
+
7
7
  entries/entry/special_attributes
8
8
  entries/entry
9
9
  entries/file
10
10
  entries/dir
11
11
  entries/universal_entry
12
-
12
+
13
13
  entry_proxy
14
-
14
+
15
15
  storages/local
16
-
16
+
17
17
  integration/string
18
-
18
+
19
19
  vfs
20
20
  ).each{|f| require "vfs/#{f}"}
@@ -1,26 +1,26 @@
1
1
  module Vfs
2
2
  class Dir < Entry
3
- #
3
+ #
4
4
  # Container
5
- #
5
+ #
6
6
  def [] path
7
7
  path = path.to_s
8
8
  if path =~ /.+[\/]$/
9
9
  path = path.sub /\/$/, ''
10
10
  dir path
11
11
  else
12
- entry path
13
- end
12
+ entry path
13
+ end
14
14
  end
15
15
  alias_method :/, :[]
16
-
17
-
18
- #
16
+
17
+
18
+ #
19
19
  # Attributes
20
- #
20
+ #
21
21
  alias_method :exist?, :dir?
22
-
23
-
22
+
23
+
24
24
  #
25
25
  # CRUD
26
26
  #
@@ -30,9 +30,9 @@ module Vfs
30
30
  begin
31
31
  try += 1
32
32
  fs.create_dir path
33
- rescue StandardError => error
33
+ rescue StandardError => error
34
34
  entry = self.entry
35
- attrs = entry.get
35
+ attrs = entry.get
36
36
  if attrs[:file] #entry.exist?
37
37
  if options[:override]
38
38
  entry.destroy
@@ -45,31 +45,31 @@ module Vfs
45
45
  parent = self.parent
46
46
  if parent.exist?
47
47
  # some unknown error
48
- raise error
48
+ raise error
49
49
  else
50
- parent.create(options)
51
- end
50
+ parent.create(options)
51
+ end
52
52
  end
53
-
53
+
54
54
  retry if try < 2
55
55
  end
56
56
  end
57
57
  self
58
- end
58
+ end
59
59
  def create! options = {}
60
60
  options[:override] = true
61
61
  create options
62
62
  end
63
-
63
+
64
64
  def destroy options = {}
65
65
  storage.open_fs do |fs|
66
66
  begin
67
- fs.delete_dir path
67
+ fs.delete_dir path
68
68
  rescue StandardError => e
69
69
  attrs = get
70
70
  if attrs[:file]
71
71
  if options[:force]
72
- file.destroy
72
+ file.destroy
73
73
  else
74
74
  raise Error, "can't destroy File #{dir} (You are trying to destroy it as if it's a Dir)"
75
75
  end
@@ -80,28 +80,28 @@ module Vfs
80
80
  # do nothing, file already not exist
81
81
  end
82
82
  end
83
- end
83
+ end
84
84
  self
85
85
  end
86
86
  def destroy! options = {}
87
87
  options[:force] = true
88
88
  destroy options
89
89
  end
90
-
91
-
92
- #
90
+
91
+
92
+ #
93
93
  # Content
94
- #
94
+ #
95
95
  def entries *args, &block
96
96
  raise "invalid arguments #{args.inspect}!" if args.size > 2
97
97
  options = args.last.is_a?(Hash) ? args.pop : {}
98
98
  query = args.first
99
- options[:bang] = true unless options.include? :bang
100
-
101
- storage.open_fs do |fs|
99
+ options[:bang] = true unless options.include? :bang
100
+
101
+ storage.open_fs do |fs|
102
102
  begin
103
103
  list = []
104
- # query option is optional and supported only for some storages (local fs for example)
104
+ # query option is optional and supported only for some storages (local fs for example)
105
105
  fs.each_entry path, query do |name, type|
106
106
  next if options[:filter] and options[:filter] != type
107
107
  entry = if type == :dir
@@ -123,45 +123,45 @@ module Vfs
123
123
  raise error
124
124
  else
125
125
  raise Error, "'#{self}' not exist!" if options[:bang]
126
- []
126
+ []
127
127
  end
128
128
  end
129
129
  end
130
130
  end
131
131
  alias_method :each, :entries
132
-
132
+
133
133
  def files *args, &block
134
- options = args.last.is_a?(Hash) ? args.pop : {}
135
-
134
+ options = args.last.is_a?(Hash) ? args.pop : {}
135
+
136
136
  options[:filter] = :file
137
137
  args << options
138
138
  entries *args, &block
139
139
  end
140
-
140
+
141
141
  def dirs *args, &block
142
142
  options = args.last.is_a?(Hash) ? args.pop : {}
143
-
143
+
144
144
  options[:filter] = :dir
145
145
  args << options
146
146
  entries *args, &block
147
- end
148
-
147
+ end
148
+
149
149
  def include? name
150
150
  entry[name].exist?
151
151
  end
152
152
  alias_method :has?, :include?
153
-
153
+
154
154
  def empty?
155
155
  entries.empty?
156
156
  end
157
-
158
-
159
- #
157
+
158
+
159
+ #
160
160
  # Transfers
161
- #
161
+ #
162
162
  def copy_to to, options = {}
163
163
  options[:bang] = true unless options.include? :bang
164
-
164
+
165
165
  raise Error, "invalid argument, destination should be a Entry (#{to})!" unless to.is_a? Entry
166
166
  raise Error, "you can't copy to itself" if self == to
167
167
 
@@ -176,10 +176,10 @@ module Vfs
176
176
  else
177
177
  raise "can't copy to unknown Entry!"
178
178
  end
179
-
179
+
180
180
  # efficient_dir_copy(target, options) || unefficient_dir_copy(target, options)
181
- unefficient_dir_copy(target, options)
182
-
181
+ unefficient_dir_copy(target, options)
182
+
183
183
  target
184
184
  end
185
185
  def copy_to! to, options = {}
@@ -196,48 +196,48 @@ module Vfs
196
196
  options[:override] = true
197
197
  move_to to, options
198
198
  end
199
-
199
+
200
200
  # class << self
201
201
  # attr_accessor :dont_use_efficient_dir_copy
202
202
  # end
203
-
203
+
204
204
  protected
205
- def unefficient_dir_copy to, options
205
+ def unefficient_dir_copy to, options
206
206
  to.create options
207
- entries options do |e|
207
+ entries options do |e|
208
208
  if e.is_a? Dir
209
209
  e.copy_to to.dir(e.name), options
210
210
  elsif e.is_a? File
211
211
  e.copy_to to.file(e.name), options
212
212
  else
213
213
  raise 'internal error'
214
- end
214
+ end
215
215
  end
216
216
  end
217
-
218
-
217
+
218
+
219
219
  # def efficient_dir_copy to, options
220
220
  # return false if self.class.dont_use_efficient_dir_copy
221
- #
221
+ #
222
222
  # storage.open_fs do |fs|
223
223
  # try = 0
224
- # begin
225
- # try += 1
224
+ # begin
225
+ # try += 1
226
226
  # self.class.efficient_dir_copy(self, to, options[:override])
227
- # rescue StandardError => error
227
+ # rescue StandardError => error
228
228
  # unknown_errors = 0
229
- #
229
+ #
230
230
  # attrs = get
231
231
  # if attrs[:file]
232
232
  # raise Error, "can't copy File as a Dir ('#{self}')!"
233
233
  # elsif attrs[:dir]
234
234
  # # some unknown error (but it also maybe caused by to be fixed error in 'to')
235
- # unknown_errors += 1
235
+ # unknown_errors += 1
236
236
  # else
237
237
  # raise Error, "'#{self}' not exist!" if options[:bang]
238
238
  # return true
239
239
  # end
240
- #
240
+ #
241
241
  # attrs = to.get
242
242
  # if attrs[:file]
243
243
  # if options[:override]
@@ -250,7 +250,7 @@ module Vfs
250
250
  # # if options[:override]
251
251
  # # to.destroy
252
252
  # # else
253
- # # dir_already_exist = true
253
+ # # dir_already_exist = true
254
254
  # # # raise Vfs::Error, "entry #{to} already exist!"
255
255
  # # end
256
256
  # else # parent not exist
@@ -259,16 +259,16 @@ module Vfs
259
259
  # # some unknown error (but it also maybe caused by already fixed error in 'from')
260
260
  # unknown_errors += 1
261
261
  # else
262
- # parent.create(options)
263
- # end
262
+ # parent.create(options)
263
+ # end
264
264
  # end
265
- #
265
+ #
266
266
  # raise error if unknown_errors > 1
267
267
  # try < 2 ? retry : raise(error)
268
268
  # end
269
269
  # end
270
270
  # end
271
- #
271
+ #
272
272
  # def self.efficient_dir_copy from, to, override
273
273
  # from.storage.open_fs{|fs|
274
274
  # fs.respond_to?(:efficient_dir_copy) and fs.efficient_dir_copy(from, to, override)
@@ -1,11 +1,11 @@
1
1
  module Vfs
2
2
  class Entry
3
3
  attr_reader :storage, :path, :path_cache
4
-
4
+
5
5
  def initialize *args
6
6
  if args.size == 1 and args.first.is_a? Entry
7
7
  entry = args.first
8
- @path_cache = entry.path_cache
8
+ @path_cache = entry.path_cache
9
9
  @storage, @path = entry.storage, entry.path
10
10
  else
11
11
  storage, path = *args
@@ -13,20 +13,20 @@ module Vfs
13
13
  @storage, @path = storage, path_cache.to_s
14
14
  end
15
15
  raise "storage not defined!" unless self.storage
16
- end
17
-
18
-
19
- #
16
+ end
17
+
18
+
19
+ #
20
20
  # Navigation
21
- #
21
+ #
22
22
  def parent
23
23
  Dir.new(storage, path_cache + '..')
24
24
  end
25
-
26
-
27
- #
25
+
26
+
27
+ #
28
28
  # Transformations
29
- #
29
+ #
30
30
  def dir path = nil
31
31
  if path
32
32
  new_path = path_cache + path
@@ -36,7 +36,7 @@ module Vfs
36
36
  end
37
37
  end
38
38
  alias_method :to_dir, :dir
39
-
39
+
40
40
  def file path = nil
41
41
  if path
42
42
  new_path = path_cache + path
@@ -46,46 +46,46 @@ module Vfs
46
46
  end
47
47
  end
48
48
  alias_method :to_file, :file
49
-
49
+
50
50
  def entry path = nil
51
51
  entry = if path
52
-
52
+
53
53
  new_path = path_cache + path
54
54
  klass = new_path.probably_dir? ? Dir : UniversalEntry
55
- entry = klass.new storage, new_path
55
+ entry = klass.new storage, new_path
56
56
  else
57
57
  UniversalEntry.new self
58
58
  end
59
59
  EntryProxy.new entry
60
60
  end
61
61
  alias_method :to_entry, :entry
62
-
63
-
64
- #
62
+
63
+
64
+ #
65
65
  # Attributes
66
- #
66
+ #
67
67
  def get attr_name = nil
68
68
  attrs = storage.open_fs{|fs| fs.attributes(path)}
69
69
  attr_name ? attrs[attr_name] : attrs
70
70
  end
71
-
71
+
72
72
  def set options
73
73
  not_implemented
74
74
  end
75
-
75
+
76
76
  def dir?; !!get(:dir) end
77
- def file?; !!get(:file) end
78
-
77
+ def file?; !!get(:file) end
78
+
79
79
  include SpecialAttributes
80
-
81
-
82
- #
80
+
81
+
82
+ #
83
83
  # Miscellaneous
84
- #
84
+ #
85
85
  def name
86
86
  path_cache.name
87
87
  end
88
-
88
+
89
89
  def tmp &block
90
90
  storage.open_fs do |fs|
91
91
  if block
@@ -97,29 +97,29 @@ module Vfs
97
97
  end
98
98
  end
99
99
  end
100
-
100
+
101
101
  def local?
102
102
  storage.local?
103
103
  end
104
-
105
-
106
- #
104
+
105
+
106
+ #
107
107
  # Utils
108
- #
108
+ #
109
109
  def inspect
110
110
  "#{storage}#{':' unless storage.to_s.empty?}#{path}"
111
111
  end
112
- alias_method :to_s, :inspect
113
-
112
+ alias_method :to_s, :inspect
113
+
114
114
  def == other
115
115
  return false unless other.is_a? Entry
116
116
  storage == other.storage and path == other.path
117
117
  end
118
-
119
- def hash
118
+
119
+ def hash
120
120
  storage.hash + path.hash
121
121
  end
122
-
122
+
123
123
  def eql? other
124
124
  return false unless other.class == self.class
125
125
  storage.eql?(other.storage) and path.eql?(other.path)