Class: Fast::Dir

Inherits:
Array
  • Object
show all
Defined in:
lib/fast/dir.rb

Overview

Directory handling class

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Dir

Returns a new instance of Dir.



6
7
8
9
# File 'lib/fast/dir.rb', line 6

def initialize path = nil
  super()
  @path = normalize path if path
end

Instance Method Details

#[](name) ⇒ Object



276
277
278
279
280
281
282
283
# File 'lib/fast/dir.rb', line 276

def [] name
  if name.is_a? Integer # I do not wish to disable Array behaviour
    super
  else
    return Dir.new "#{@path}/#{name}" if dirs.include? normalize name
    return File.new "#{@path}/#{name}" if files.include? normalize name
  end
end

#[]=(name, content) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/fast/dir.rb', line 285

def []= name, content
  if name.is_a? Integer # I do not wish to disable Array behaviour
    super 
  else
    if content.is_a? Hash
      subdir = Dir.new.create! "#{@path}/#{name}"
      content.each do |item_name, item_content|
        subdir[item_name] = item_content
      end
      return subdir
    else
      return File.new.write "#{@path}/#{name}", content       
    end
  end
end

#copy(*args) ⇒ Object Also known as: copy!



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/fast/dir.rb', line 255

def copy *args
  if args.length > 1
    current, target = *args
    @path = normalize current
    target = Dir.new target
  else
    target = Dir.new args.first
  end
  
  target.create
  list do |entry|
    if File.new.exist? "#{@path}/#{entry}" # This is a "is file?" check and should be more obvious
      File.new.copy "#{@path}/#{entry}", "#{target.path}/#{entry}"
    else # is a Dir then
      Dir.new.copy "#{@path}/#{entry}", "#{target.path}/#{entry}"
    end
  end
end

#create(*args) ⇒ Object

Creates the dir, if it doesn’t exist. Otherwise raises an ArgumentException Returns the last dir path passed as argument



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fast/dir.rb', line 54

def create *args
  if args.length > 0
    return_me = nil
    args.each do |path|
      raise ArgumentError, "Dir '#{path}' already exists" if Dir.new.exist? path
      return_me = do_create path
    end
    return return_me
  else
    raise ArgumentError, "No arguments passed, at least one is required" unless @path
    raise ArgumentError, "Dir '#{@path}' already exists" if Dir.new.exist? @path
    do_create @path
  end
end

#create!(*args) ⇒ Object

Creates the dir, if it doesn’t exist. Otherwise remains silent Returns the last dir path passed as argument



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fast/dir.rb', line 71

def create! *args
  if args.length > 0
    return_me = nil
    args.each do |path|
      return_me = do_create path
    end
    return return_me
  else
    raise ArgumentError, "No arguments passed, at least one is required" unless @path
    do_create @path
  end
end

#delete(*args) ⇒ Object Also known as: remove, destroy, del, unlink

Deletes the directory along with all its content. Powerful, simple, risky! Many arguments can be passed



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fast/dir.rb', line 86

def delete *args
  if args.length > 0
    return_me = nil
    args.each do |path|
      return_me = do_delete path
    end
    return return_me
  else
    do_delete
  end
end

#delete!(*args) ⇒ Object Also known as: remove!, destroy!, del!, unlink!

Like #delete, but raises no error if some directory is missing



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fast/dir.rb', line 104

def delete! *args
  if args.length > 0
    return_me = nil
    args.each do |path|
      begin
        return_me = do_delete path
      rescue
        return_me = nil
      end
    end
    return return_me
  else
    begin
      do_delete
    rescue
      nil
    end
  end
end

#dirs(path = nil, &block) ⇒ Object

Returns a Fast::Dir list of all directories in the directory, non-recursive and excluding points



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fast/dir.rb', line 40

def dirs path = nil, &block
  @path = normalize path if path
  self.clear unless self.empty?
  ::Dir.foreach @path do |entry|
    if ::File.directory? "#{@path}/#{entry}" and entry != "." and entry != ".."
      self << entry 
      block.call entry if block
    end
  end
  self
end

#exist?(path = nil) ⇒ Boolean Also known as: exists?

Checks for existence. True if the directory exists, false otherwise

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/fast/dir.rb', line 130

def exist? path = nil
  @path = normalize path if path
  ::File.directory? @path
end

#exist_all?(*args) ⇒ Boolean

Returns true if all passed dirs exist, false otherwise

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
147
# File 'lib/fast/dir.rb', line 138

def exist_all? *args
  unless args.empty?
    args.each do |path|
      return false if not Dir.new.exist? path
    end
    return true
  else
    exist?
  end
end

#exist_any?(*args) ⇒ Boolean

Returns true if any of passed dirs exists, false otherwise

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
159
# File 'lib/fast/dir.rb', line 150

def exist_any? *args
  unless args.empty?
    args.each do |path|
      return true if Dir.new.exist? path
    end
    return false
  else
    exist?
  end
end

#exist_which(*args) ⇒ Object

Return a list with the existing dirs path Note: This should be delegated to the SubSetter::Fast::Dir



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/fast/dir.rb', line 163

def exist_which *args
  if args.empty?
    raise ArgumentError, "Wrong number of arguments, at least one dir should be passed"
  end
  
  existing_ones = []
  args.each do |path|
    existing_ones << path if Dir.new.exist? path
  end

  return existing_ones
end

#expand(path = nil) ⇒ Object Also known as: absolute

Expands the path to absolute is it is relative



189
190
191
192
# File 'lib/fast/dir.rb', line 189

def expand path = nil
  @path = normalize path if path
  ::File.expand_path @path
end

#files(path = nil, &block) ⇒ Object

Returns a Fast::Dir list of all files in the directory. Non recursive (at least yet)



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fast/dir.rb', line 26

def files path = nil, &block
  @path = normalize path if path
  self.clear unless self.empty?
  ::Dir.foreach @path do |entry|
    unless ::File.directory? "#{@path}/#{entry}"
      self << entry 
      block.call entry if block
    end
  end
  self
end

#filterObject Also known as: by

Sends self to a SubSetter for Fast::Dir



182
183
184
# File 'lib/fast/dir.rb', line 182

def filter
  SubSetter::Fast::Dir.new self
end

#list(path = nil, &block) ⇒ Object

Returns a Fast::Dir list with all items in the directory, except “..” and “.”



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fast/dir.rb', line 12

def list path = nil, &block
  @path = normalize path unless path.nil?
  self.clear unless self.empty?
  ::Dir.foreach @path do |entry|
    unless entry == "." or entry == ".."
      self << entry 
      block.call entry if block
    end
  end
  self
end

#merge(*args) ⇒ Object

Merges the target dir into this



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/fast/dir.rb', line 235

def merge *args
  if args.length > 1
    current, target = *args
    @path = normalize current
    target = normalize target
  else
    target = normalize args.first
  end
  
  Dir.new.list target do |entry|
    unless Dir.new.exist? "#{target}/#{entry}"
      File.new.rename "#{target}/#{entry}", "#{@path}/#{entry}"
    else
      Dir.new.rename "#{target}/#{entry}", "#{@path}/#{entry}"
    end
  end
  
  Dir.new.delete target
end

#pathObject

Returns the path to the dir, if defined



197
198
199
# File 'lib/fast/dir.rb', line 197

def path
  @path if @path
end

#rename(*args) ⇒ Object Also known as: move

Renames this dir into the target path, unless the target path points to an existing dir.

Raises:

  • (ArgumentError)


203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/fast/dir.rb', line 203

def rename *args
  if args.length > 1
    current, target = *args
    @path = normalize current
    target = normalize target
  else
    target = normalize args.first
  end

  raise ArgumentError, "The target directory '#{target}' already exists" if Dir.new.exist? target
  do_rename_to target
end

#rename!(*args) ⇒ Object Also known as: move!

Renames this dir into the target path: overwrites the target dir if it exists



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/fast/dir.rb', line 220

def rename! *args
  if args.length > 1
    current, target = *args
    @path = normalize current
    target = normalize target
  else
    target = normalize args.first
  end
  Dir.new.delete! target if Dir.new.exist? target
  do_rename_to target
end

#to_sObject

Returns a String brief of the dir



177
178
179
# File 'lib/fast/dir.rb', line 177

def to_s
  return "#{@path}"
end