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.



4
5
6
7
# File 'lib/fast/dir.rb', line 4

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

Instance Method Details

#[](name) ⇒ Object



264
265
266
267
268
269
270
271
# File 'lib/fast/dir.rb', line 264

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



273
274
275
276
277
278
279
# File 'lib/fast/dir.rb', line 273

def []= name, content
  if name.is_a? Integer # I do not wish to disable Array behaviour
    super 
  else
    return File.new.write "#{@path}/#{name}", content       
  end
end

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



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

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fast/dir.rb', line 49

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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fast/dir.rb', line 66

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: destroy, del, unlink, remove

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



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fast/dir.rb', line 81

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!

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fast/dir.rb', line 99

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



36
37
38
39
40
41
42
43
44
45
# File 'lib/fast/dir.rb', line 36

def dirs path = nil, &block
  @path = normalize path if path
  ::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)


122
123
124
125
# File 'lib/fast/dir.rb', line 122

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)


130
131
132
133
134
135
136
137
138
139
# File 'lib/fast/dir.rb', line 130

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)


142
143
144
145
146
147
148
149
150
151
# File 'lib/fast/dir.rb', line 142

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



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/fast/dir.rb', line 155

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



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

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)



23
24
25
26
27
28
29
30
31
32
# File 'lib/fast/dir.rb', line 23

def files path = nil, &block
  @path = normalize path if path
  ::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 DirFilter filter



174
175
176
# File 'lib/fast/dir.rb', line 174

def filter
  DirFilter.new self
end

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

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



10
11
12
13
14
15
16
17
18
19
# File 'lib/fast/dir.rb', line 10

def list path = nil, &block
  @path = normalize path unless path.nil?
  ::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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/fast/dir.rb', line 223

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



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

def path
  @path if @path
end

#rename(*args) ⇒ Object

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

Raises:

  • (ArgumentError)


195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/fast/dir.rb', line 195

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

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



210
211
212
213
214
215
216
217
218
219
220
# 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
  Dir.new.delete! target if Dir.new.exist? target
  do_rename_to target
end

#to_sObject

Returns a String brief of the dir



169
170
171
# File 'lib/fast/dir.rb', line 169

def to_s
  return "#{@path}"
end