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, #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:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/vfs/entries/dir.rb', line 143

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 #(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

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

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.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(options)
        end
      end

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

#destroy(options = {}) ⇒ Object



57
58
59
# File 'lib/vfs/entries/dir.rb', line 57

def destroy options = {}
  destroy_entry :dir, :file
end

#dirs(*args, &block) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/vfs/entries/dir.rb', line 119

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)


132
133
134
135
136
137
# File 'lib/vfs/entries/dir.rb', line 132

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

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

Content



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
105
106
107
108
# File 'lib/vfs/entries/dir.rb', line 65

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 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



111
112
113
114
115
116
117
# File 'lib/vfs/entries/dir.rb', line 111

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)


127
128
129
# File 'lib/vfs/entries/dir.rb', line 127

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

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



166
167
168
169
170
# File 'lib/vfs/entries/dir.rb', line 166

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