Class: Vfs::Dir

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

Instance Attribute Summary

Attributes inherited from Entry

#driver, #path, #path_cache

Instance Method Summary collapse

Methods inherited from Entry

#==, #created_at, #destroy, #dir, #dir?, #entry, #eql?, #file, #file?, #get, #hash, #initialize, #inspect, #local?, #name, #parent, #remove, #set, #tmp, #updated_at

Constructor Details

This class inherits a constructor from Vfs::Entry

Instance Method Details

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

Container.



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

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

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

Transfers.

Raises:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/vfs/entries/dir.rb', line 137

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
    to.dir
  elsif to.is_a? Dir
    to.dir
  elsif to.is_a? UniversalEntry
    to.dir
  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

#create(options = {}) ⇒ Object

CRUD.



24
25
26
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
# File 'lib/vfs/entries/dir.rb', line 24

def create options = {}
  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.delete
      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

#delete(options = {}) ⇒ Object



54
55
56
# File 'lib/vfs/entries/dir.rb', line 54

def delete options = {}
  delete_entry :dir, :file
end

#dirs(*args, &block) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/vfs/entries/dir.rb', line 115

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

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

#empty?Boolean



128
129
130
131
132
133
# File 'lib/vfs/entries/dir.rb', line 128

def empty?
  catch :break do
    entries{|e| throw :break, false}
    true
  end
end

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

Content.



60
61
62
63
64
65
66
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
# File 'lib/vfs/entries/dir.rb', line 60

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
  filter = options[:filter]
  type_required = options[: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 name == '' # Fix for https://github.com/alexeypetrushin/vfs/issues/5
        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 options[:bang]
        []
      end
    end
  end
end

#files(*args, &block) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/vfs/entries/dir.rb', line 107

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?



123
124
125
# File 'lib/vfs/entries/dir.rb', line 123

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

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



159
160
161
162
163
# File 'lib/vfs/entries/dir.rb', line 159

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