Class: Vfs::Dir
Instance Attribute Summary
Attributes inherited from Entry
Instance Method Summary (collapse)
-
- (Object) [](path)
(also: #/)
Container.
-
- (Object) copy_to(to, options = {})
Transfers.
-
- (Object) create(options = {})
CRUD.
- - (Object) destroy(options = {})
- - (Object) dirs(*args, &block)
- - (Boolean) empty?
-
- (Object) entries(*args, &block)
(also: #each)
Content.
- - (Object) files(*args, &block)
- - (Boolean) include?(name) (also: #has?)
- - (Object) move_to(to, options = {})
Methods inherited from Entry
#==, #created_at, #delete, #dir, #dir?, #entry, #eql?, #file, #file?, #get, #hash, #initialize, #inspect, #local?, #name, #parent, #set, #tmp, #updated_at
Constructor Details
This class inherits a constructor from Vfs::Entry
Instance Method Details
- (Object) [](path) Also known as: /
Container
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/vfs/entries/dir.rb', line 6 def [] path path = path.to_s if path =~ /.+[\/]$/ path = path.sub /\/$/, '' dir path elsif path =~ /\*/ entries path else entry path end end |
- (Object) copy_to(to, options = {})
Transfers
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/vfs/entries/dir.rb', line 145 def copy_to to, = {} [:bang] = true unless .include? :bang raise Error, "invalid argument, destination should be a Entry (#{to})!" unless to.is_a? Entry raise Error, "you can't copy to itself" if self == to target = if to.is_a? File to.dir elsif to.is_a? Dir to.dir #(name) elsif to.is_a? UniversalEntry # raise "can't copy Dir to File ('#{self}')!" if to.file? and !options[:override] to.dir #.create else raise "can't copy to unknown Entry!" end # efficient_dir_copy(target, options) || unefficient_dir_copy(target, options) unefficient_dir_copy(target, ) target end |
- (Object) create(options = {})
CRUD
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/vfs/entries/dir.rb', line 29 def create = {} driver.open do try = 0 begin try += 1 driver.create_dir path rescue StandardError => error entry = self.entry attrs = entry.get if attrs and attrs[:file] #entry.exist? entry.destroy elsif attrs and attrs[:dir] # dir already exist, no need to recreate it return self else parent = self.parent if parent.exist? # some unknown error raise error else parent.create() end end try < 2 ? retry : raise(error) end end self end |
- (Object) destroy(options = {})
59 60 61 |
# File 'lib/vfs/entries/dir.rb', line 59 def destroy = {} destroy_entry :dir, :file end |
- (Object) dirs(*args, &block)
121 122 123 124 125 126 127 |
# File 'lib/vfs/entries/dir.rb', line 121 def dirs *args, &block = args.last.is_a?(Hash) ? args.pop : {} [:filter] = :dir args << entries *args, &block end |
- (Boolean) empty?
134 135 136 137 138 139 |
# File 'lib/vfs/entries/dir.rb', line 134 def empty? catch :break do entries{|e| throw :break, false} true end end |
- (Object) entries(*args, &block) Also known as: each
Content
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/vfs/entries/dir.rb', line 67 def entries *args, &block raise "invalid arguments #{args.inspect}!" if args.size > 2 = args.last.is_a?(Hash) ? args.pop : {} query = args.first [:bang] = true unless .include? :bang filter = [:filter] type_required = [:type] driver.open do begin list = [] # query option is optional and supported only for some drivers (local driver for example) driver.each_entry path, query do |name, type| # for performance reasons some drivers may return the type of entry as # optionally evaluated callback. type = type.call if (filter or type_required) and type.is_a?(Proc) next if filter and (filter != type) entry = if type == :dir dir(name) elsif type == :file file(name) else entry(name) end block ? block.call(entry) : (list << entry) end block ? nil : list rescue StandardError => error attrs = get if attrs and attrs[:file] raise Error, "can't query entries on File ('#{self}')!" elsif attrs and attrs[:dir] # some unknown error raise error else # TODO2 remove :bang raise Error, "'#{self}' not exist!" if [:bang] [] end end end end |
- (Object) files(*args, &block)
113 114 115 116 117 118 119 |
# File 'lib/vfs/entries/dir.rb', line 113 def files *args, &block = args.last.is_a?(Hash) ? args.pop : {} [:filter] = :file args << entries *args, &block end |
- (Boolean) include?(name) Also known as: has?
129 130 131 |
# File 'lib/vfs/entries/dir.rb', line 129 def include? name entry[name].exist? end |
- (Object) move_to(to, options = {})
168 169 170 171 172 |
# File 'lib/vfs/entries/dir.rb', line 168 def move_to to, = {} copy_to to, destroy to end |