Class: Fast::Dir

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

Overview

Directory handling class

Inherits from Array in order to be usable as a Array Includes the module Fast::FilesystemObject for common functionality with Fast::File

Instance Method Summary collapse

Methods included from FilesystemObject

#exist?, #expand, #normalize, #path

Constructor Details

#initialize(path = nil) ⇒ Dir

Returns a new instance of Dir.



12
13
14
15
# File 'lib/fast/dir.rb', line 12

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

Instance Method Details

#[](name) ⇒ Object



283
284
285
286
287
288
289
290
# File 'lib/fast/dir.rb', line 283

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



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/fast/dir.rb', line 292

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

#conflicts_with?(*args) ⇒ Boolean

Checks the given dirs should be merged if any conflict would arise.

Returns true if any file in the target directory has the same path (ie: the same name and subdirectory structure) as other file in the source directory.

Returns:

  • (Boolean)


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/fast/dir.rb', line 313

def conflicts_with? *args
  if args.length > 1
    current, target = *args
    @path = normalize current
    target = Dir.new target
  else
    target = Dir.new args.first
  end
  
  files do |file|
    target.files do |target_file|
      return true if file == target_file
    end
  end
  
  dirs do |dir|
    target.dirs do |target_dir|
      if dir == target_dir
        return true if Fast::Dir.new.conflicts_with? "#{@path}/#{dir}", "#{target.path}/#{target_dir}" 
      end
    end
  end
  
  false
end

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



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/fast/dir.rb', line 262

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



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
# File 'lib/fast/dir.rb', line 60

def create *args
  if args.length > 0
    return_me = nil
    args.each do |path|
      unless path.is_a? Hash
        raise ArgumentError, "Dir '#{path}' already exists" if Dir.new.exist? path
        return_me = do_create path
      else
        if @path
          subdir = Dir.new.create! "#{@path}"
        else
          subdir = Dir.new "."
        end
        path.each do |item_name, item_content|
          subdir[item_name] = item_content
        end
        return subdir          
      end
    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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fast/dir.rb', line 89

def create! *args
  if args.length > 0
    return_me = nil
    args.each do |path|
      unless path.is_a? Hash
        return_me = do_create path
      else
        if @path
          subdir = Dir.new.create! "#{@path}"
        else
          subdir = Dir.new "."
        end
        path.each do |item_name, item_content|
          subdir[item_name] = item_content
        end
        return subdir          
      end
    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



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fast/dir.rb', line 116

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/fast/dir.rb', line 134

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



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fast/dir.rb', line 46

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_all?(*args) ⇒ Boolean

Returns true if all passed dirs exist, false otherwise

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
168
169
# File 'lib/fast/dir.rb', line 160

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)


172
173
174
175
176
177
178
179
180
181
# File 'lib/fast/dir.rb', line 172

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



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/fast/dir.rb', line 185

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

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

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



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fast/dir.rb', line 32

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

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

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



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fast/dir.rb', line 18

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



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/fast/dir.rb', line 242

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

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


210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/fast/dir.rb', line 210

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



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/fast/dir.rb', line 227

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

#toObject

This will be ported to a pattern exactly like SubSetter



199
200
201
# File 'lib/fast/dir.rb', line 199

def to
  Patterns::Adapter::Fast::Dir.new self
end

#to_sObject

Returns a String brief of the dir



204
205
206
# File 'lib/fast/dir.rb', line 204

def to_s
  return "#{@path}"
end