Class: Vfs::Dir

Inherits:
Entry show all
Defined in:
lib/vfs/entries/dir.rb

Instance Attribute Summary

Attributes inherited from Entry

#path, #path_cache, #storage

Instance Method Summary collapse

Methods inherited from Entry

#==, #created_at, #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

#[](path) ⇒ Object Also known as: /

Container



6
7
8
9
10
11
12
13
14
# File 'lib/vfs/entries/dir.rb', line 6

def [] path
  path = path.to_s
  if path =~ /.+[\/]$/
    path = path.sub /\/$/, ''
    dir path
  else
    entry path
  end
end

#copy_to(to, options = {}) ⇒ Object

Transfers

Raises:



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/vfs/entries/dir.rb', line 163

def copy_to to, options = {}
  options[:bang] = true unless options.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
    raise Error, "can't copy Dir to File ('#{self}')!" unless options[:override]
    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, options)

  target
end

#copy_to!(to, options = {}) ⇒ Object



186
187
188
189
# File 'lib/vfs/entries/dir.rb', line 186

def copy_to! to, options = {}
  options[:override] = true
  copy_to to, options
end

#create(options = {}) ⇒ Object

CRUD



27
28
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
58
59
# File 'lib/vfs/entries/dir.rb', line 27

def create options = {}
  storage.open do |fs|
    try = 0
    begin
      try += 1
      fs.create_dir path
    rescue StandardError => error
      entry = self.entry
      attrs = entry.get
      if attrs and attrs[:file] #entry.exist?
        if options[:override]
          entry.destroy
        else
          raise Error, "entry #{self} already exist!"
        end
      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(options)
        end
      end

      try < 2 ? retry : raise(error)
    end
  end
  self
end

#create!(options = {}) ⇒ Object



60
61
62
63
# File 'lib/vfs/entries/dir.rb', line 60

def create! options = {}
  options[:override] = true
  create options
end

#destroy(options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vfs/entries/dir.rb', line 65

def destroy options = {}
  storage.open do |fs|
    begin
      fs.delete_dir path
    rescue StandardError => e
      attrs = get
      if attrs and attrs[:file]
        if options[:force]
          file.destroy
        else
          raise Error, "can't destroy File #{dir} (You are trying to destroy it as if it's a Dir)"
        end
      elsif attrs and attrs[:dir]
        # unknown internal error
        raise e
      else
        # do nothing, file already not exist
      end
    end
  end
  self
end

#destroy!(options = {}) ⇒ Object



87
88
89
90
# File 'lib/vfs/entries/dir.rb', line 87

def destroy! options = {}
  options[:force] = true
  destroy options
end

#dirs(*args, &block) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/vfs/entries/dir.rb', line 142

def dirs *args, &block
  options = args.last.is_a?(Hash) ? args.pop : {}

  options[:filter] = :dir
  args << options
  entries *args, &block
end

#empty?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/vfs/entries/dir.rb', line 155

def empty?
  entries.empty?
end

#entries(*args, &block) ⇒ Object Also known as: each

Content



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/vfs/entries/dir.rb', line 96

def entries *args, &block
  raise "invalid arguments #{args.inspect}!" if args.size > 2
  options = args.last.is_a?(Hash) ? args.pop : {}
  query = args.first
  options[:bang] = true unless options.include? :bang

  storage.open do |fs|
    begin
      list = []
      # query option is optional and supported only for some storages (local fs for example)
      fs.each_entry path, query do |name, type|
        next if options[:filter] and options[:filter] != type
        entry = if type == :dir
          dir(name)
        elsif type == :file
          file(name)
        else
          raise 'invalid entry type!'
        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]
        # unknown error
        raise error
      else
        raise Error, "'#{self}' not exist!" if options[:bang]
        []
      end
    end
  end
end

#files(*args, &block) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/vfs/entries/dir.rb', line 134

def files *args, &block
  options = args.last.is_a?(Hash) ? args.pop : {}

  options[:filter] = :file
  args << options
  entries *args, &block
end

#include?(name) ⇒ Boolean Also known as: has?

Returns:

  • (Boolean)


150
151
152
# File 'lib/vfs/entries/dir.rb', line 150

def include? name
  entry[name].exist?
end

#move_to(to, options = {}) ⇒ Object



191
192
193
194
195
# File 'lib/vfs/entries/dir.rb', line 191

def move_to to, options = {}
  copy_to to, options
  destroy options
  to
end

#move_to!(to, options = {}) ⇒ Object



196
197
198
199
# File 'lib/vfs/entries/dir.rb', line 196

def move_to! to, options = {}
  options[:override] = true
  move_to to, options
end