Module: Pa::Directory::ClassMethods

Defined in:
lib/pa/directory.rb

Instance Method Summary collapse

Instance Method Details

#each(*args, &blk) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/pa/directory.rb', line 155

def each(*args, &blk)
  return Pa.to_enum(:each, *args) unless blk

  args, o = Util.extract_options(args)
  each2(*args, o) { |path, abs, fname, err, rea|
    blk.call Pa(path), abs, fname, err, rea # jruby need []
  }
end

#each(path = ".", o = {}) ⇒ Enumerator<String> #each(path = ".", o = {}) {|path, abs, fname, err, rea| ... } ⇒ nil

Note:

raise Errno::ENOTDIR, Errno::ENOENT

traverse directory

each('.', error: true).with_object([]) do |(pa,err),m|
  ...
end

Examples:

each '.' do |pa|
  p pa.path #=> "foo" not "./foo"
end
# => '/home' ..

Overloads:

  • #each(path = ".", o = {}) ⇒ Enumerator<String>

    Parameters:

    • path (String, Pa) (defaults to: ".")

    Options Hash (o):

    • :dot (Boolean) — default: true

      include dot file

    • :backup (Boolean) — default: true

      include backup file

    • :error (Boolean) — default: false

      yield(pa, err) instead of raise Errno::EPERM when Dir.open(dir)

    • :file (Boolean) — default: false

      return path and not raise Errno:ENOTDIR if path is a file.

    • :base_dir (String, Pa) — default: nil

      base directory.

    Returns:

    • (Enumerator<String>)
  • #each(path = ".", o = {}) {|path, abs, fname, err, rea| ... } ⇒ nil

    Yield Parameters:

    • path (String)
    • abs (String)

      absolute path

    • fname (String)

      a basename

    • err (String)

      error

    • rea (String)

      real relative path with o

    Returns:

    • (nil)

Raises:

  • (Errno::ENOENT)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/pa/directory.rb', line 118

def each2(*args, &blk) 
  return Pa.to_enum(:each2, *args) unless blk

  (dir,), o = Util.extract_options(args)
  dir = dir ? get(dir) : "."
  o = {dot: true, backup: true}.merge(o)

  rea_dir = o[:base_dir] ? File.join(get(o[:base_dir]), dir) : dir
  raise Errno::ENOENT, "`#{rea_dir}' doesn't exists."  unless File.exists?(rea_dir)

  if not File.directory?(rea_dir) 
    if o[:file]
      rea_path = rea_dir
      blk.call dir, File.absolute_path(rea_path, "."), File.basename(rea_path), nil, rea_path # jruby rbx
      return
    else
      raise Errno::ENOTDIR, "`#{rea_dir}' is not a directoy."
    end
  end

  begin
    d = Dir.open(rea_dir)
  rescue Errno::EPERM => err
  end
  raise err if err and !o[:error]

  while (entry=d.read)
    next if %w(. ..).include? entry
    next if not o[:dot] and entry=~/^\./
    next if not o[:backup] and entry=~/~$/

    path = Util.join_path(dir, entry)
    rea_path = Util.join_path(rea_dir, entry)
    blk.call path, File.absolute_path(rea_path, "."), File.basename(rea_path), err, rea_path # jruby rbx
  end
end

#each2_r(path = ".", o = {}) ⇒ Enumerator<String> #each2_r(path = ".", o = {}) {|path, abs, rel/rea, err| ... } ⇒ nil

each with recursive

  • each2_r() skip Exception

  • each2_r()relative, err

Overloads:

  • #each2_r(path = ".", o = {}) ⇒ Enumerator<String>

    Options Hash (o):

    • :base_dir (String, Pa) — default: nil

      base directory.

    Returns:

    • (Enumerator<String>)
  • #each2_r(path = ".", o = {}) {|path, abs, rel/rea, err| ... } ⇒ nil

    Yield Parameters:

    • path (String)
    • abs (String)
    • rel/rea (String)

      relative path

    • err (Errno::ENOENT, Errno::EPERM)

    Returns:

    • (nil)

See Also:



179
180
181
182
183
184
185
186
# File 'lib/pa/directory.rb', line 179

def each2_r(*args, &blk)
  return Pa.to_enum(:each2_r, *args) if not blk

  (dir,), o = Util.extract_options(args)
  dir ||= "."

  _each2_r(dir, "", o, &blk)
end

#each_r(*args, &blk) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/pa/directory.rb', line 188

def each_r(*args, &blk)
  return Pa.to_enum(:each_r, *args) if not blk

  args, o = Util.extract_options(args)
  each2_r *args, o do |path, abs, rel, err, rea|
    blk.call Pa(path, :rel => rel), abs, rel, err, rea # jruby need []
  end
end

#empty?(path) ⇒ Boolean

is directory empty?

Parameters:

  • path (String)

Returns:

  • (Boolean)


85
86
87
# File 'lib/pa/directory.rb', line 85

def empty?(path)
  Dir.entries(get(path)).empty? 
end

#glob(*args, &blk) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pa/directory.rb', line 61

def glob(*args, &blk)
  args, o = Util.extract_options(args)
  ret = []
  blk ||= proc { |path| path }

  glob2 *args, o do |path|
    ret << blk.call(Pa(path))
  end

  ret
end

#glob2(*paths, o = {}) ⇒ Array<String> #glob2(*paths, o = {}) {|path| ... } ⇒ nil

Note:

glob is * ** ? [set] a,b

path globbing, exclude ‘.’ ‘..’

Overloads:

  • #glob2(*paths, o = {}) ⇒ Array<String>

    Parameters:

    • path (String)
    • o (Hash) (defaults to: {})

      option

    Options Hash (o):

    • :dotmatch (Boolean) — default: false

      match dot file. FNM_DOTMATCH

    • :pathname (Boolean) — default: false

      wildcard doesn’t match /. FNM_PATHNAME

    • :noescape (Boolean) — default: false

      makes ‘\’ ordinary. FNM_NOESCAPE

    Returns:

    • (Array<String>)
  • #glob2(*paths, o = {}) {|path| ... } ⇒ nil

    Yield Parameters:

    • path (String)

    Returns:

    • (nil)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pa/directory.rb', line 40

def glob2(*args, &blk)
  paths, o = Util.extract_options(args)
  paths.map!{|v|get(v)}
  blk ||= proc { |path| path }

  flag = 0
  o.each do |option, value|
    flag |= File.const_get("FNM_#{option.upcase}") if value
  end

  # delete . .. 
  files = Dir.glob(paths, flag).delete_if{|v| v =~ %r~(^|/)\.\.?$~}

  ret = []
  files.each { |path|
    ret << blk.call(path)
  }

  ret
end

#ls(*paths, o = {}) ⇒ Object #ls(*paths, o = {}) {|pa, abs, fname, err, rea| ... } ⇒ Array<String>

@return [Array<Pa>]

Overloads:

  • #ls(*paths, o = {}) {|pa, abs, fname, err, rea| ... } ⇒ Array<String>

    Yield Parameters:

    • pa (Pa)
    • abs (String)
    • fname (String)
    • err (Exception)
    • rea (String)

    Returns:

    • (Array<String>)


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/pa/directory.rb', line 277

def ls(*args, &blk)
  dirs, o = Util.extract_options(args)
  blk ||= proc { true }
  ret = []

  ls2(*dirs, o) { |path, abs, fname, err, rea|
    view_path = if o[:absolute]
             abs
           elsif o[:include]
             path
           else
             fname
           end

    ret << Pa(view_path) if blk.call(Pa(path), abs, fname, err, rea)
  }

  ret
end

#ls2(*dirs, o = {}) ⇒ Object #ls2(*dirs, o = {}) {|path, abs, fname| ... } ⇒ Array<String>

list directory contents block form is a filter.

@return [Array<String>]

Overloads:

  • #ls2(*dirs, o = {}) ⇒ Object

    Options Hash (o):

    • :absolute (Boolean) — default: false

      return absolute path instead.

    • :include (Boolean) — default: false

      return “<path>/foo”

  • #ls2(*dirs, o = {}) {|path, abs, fname| ... } ⇒ Array<String>

    Yield Parameters:

    • path (String)
    • abs (String)
    • fname (String)

    Returns:

    • (Array<String>)

See Also:



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/pa/directory.rb', line 214

def ls2(*args, &blk)
  dirs, o = Util.extract_options(args)
  dirs << "." if dirs.empty?
  blk ||= proc { true }

  dirs.each.with_object([]) { |dir, m|
    each2(dir, o) { |path, abs, fname, err, rea|

      view_path = if o[:absolute]
               abs
             elsif o[:include]
               path
             else
               fname
             end

      m << view_path if blk.call(path, abs, fname, err, rea)
    }
  }
end

#ls2_r(*dirs, o = {}) ⇒ Array<String> #ls2_r(*dirs, o = {}) {|path, abs, rel, err, rea| ... } ⇒ Array<String>

ls2 with recursive

Overloads:

  • #ls2_r(*dirs, o = {}) ⇒ Array<String>

    Returns:

    • (Array<String>)
  • #ls2_r(*dirs, o = {}) {|path, abs, rel, err, rea| ... } ⇒ Array<String>

    Yield Parameters:

    • path (String)
    • abs (String)
    • rel (String)
    • err (Exception)
    • rea (String)

    Returns:

    • (Array<String>)

See Also:



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/pa/directory.rb', line 247

def ls2_r(*args, &blk)
  dirs, o = Util.extract_options(args)
  dirs << "." if dirs.empty?
  blk ||= proc { true }

  dirs.each.with_object([]) { |dir, m|
    each2_r(dir, o) { |path, abs, rel, err, rea|
      view_path = if o[:absolute]
               abs
             elsif o[:include]
               path
             else
               rel
             end

      m << view_path if blk.call(path, abs, rel, err, rea)
    }
  }
end

#ls_r(*args, &blk) ⇒ Object



297
298
299
300
# File 'lib/pa/directory.rb', line 297

def ls_r(*args, &blk)
  args, o = Util.extract_options(args)
  ls2_r(*args, o, &blk)
end

#tmpdirObject



77
78
79
# File 'lib/pa/directory.rb', line 77

def tmpdir
  Pa(Dir.tmpdir)
end

#tmpdir2Object



73
74
75
# File 'lib/pa/directory.rb', line 73

def tmpdir2
  Dir.tmpdir
end