Module: Pa::ClassMethods::Dir

Included in:
Pa
Defined in:
lib/tagen/core/pa/dir.rb

Instance Method Summary collapse

Instance Method Details

#each(path = ".", o = {}) ⇒ Enumerator<Pa> #each(path = ".", o = {}) {|path| ... } ⇒ 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<Pa>

    Parameters:

    Options Hash (o):

    • :nodot (Boolean) — default: false

      include dot file

    • :nobackup (Boolean) — default: false

      include backup file

    • :error (Boolean) — default: false

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

    Returns:

  • #each(path = ".", o = {}) {|path| ... } ⇒ nil

    Yield Parameters:

    • path (Pa)

    Returns:

    • (nil)

Raises:

  • (Errno::ENOENT)


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
109
110
# File 'lib/tagen/core/pa/dir.rb', line 83

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

	(path,), o = args.extract_options
	path = path ? get(path) : "."
	raise Errno::ENOENT, "`#{path}' doesn't exists."  unless File.exists?(path)
	raise Errno::ENOTDIR, "`#{path}' not a directoyr."  unless File.directory?(path)

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

	while (entry=dir.read)
		next if %w(. ..).include? entry
		next if o[:nodot] and entry=~/^\./
		next if o[:nobackup] and entry=~/~$/

		# => "foo" not "./foo"
		pa = path=="." ? Pa(entry) : Pa(File.join(path, entry))
		if o[:error]
			blk.call pa, err  
		else
			blk.call pa
		end
	end
end

#each_r(path = ".", o = {}) ⇒ Enumerator<Pa> #each_r(path = ".", o = {}) {|pa, relative, err| ... } ⇒ nil

each with recursive

  • each_r() skip Exception

  • each_r()err

Overloads:

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

    Returns:

  • #each_r(path = ".", o = {}) {|pa, relative, err| ... } ⇒ nil

    Yield Parameters:

    • pa (Pa)
    • relative (String)

      relative path

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

    Returns:

    • (nil)

See Also:



125
126
127
128
129
130
131
132
# File 'lib/tagen/core/pa/dir.rb', line 125

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

	(path,), o = args.extract_options
	path ||= "."

	_each_r(path, "", o, &blk)
end

#empty?(path) ⇒ Boolean

is directory empty?

Parameters:

Returns:

  • (Boolean)


58
# File 'lib/tagen/core/pa/dir.rb', line 58

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

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

Note:

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

path globbing, exclude ‘.’ ‘..’ for :dotmatch

Overloads:

  • #glob(*paths, o = {}) ⇒ Array<Pa>

    Parameters:

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

      option

    Options Hash (o):

    • :dotmatch (Boolean)

      glob not match dot file by default.

    • :pathname (Boolean)

      wildcard doesn’t match /

    • :noescape (Boolean)

      makes ‘\’ ordinary

    Returns:

  • #glob(*paths, o = {}) {|path| ... } ⇒ nil

    Yield Parameters:

    • path (Pa)

    Returns:

    • (nil)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tagen/core/pa/dir.rb', line 30

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

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

	ret = Dir.glob(paths, flag)

	# delete . .. for '.*'
	ret.tap{|v|v.delete(*%w(. ..))}
	ret.map!{|v|Pa(v)}

	if blk
		ret.each {|pa|
			blk.call pa
		}
	else
		ret
	end
end

#ls(*args) ⇒ Array<String>

list directory contents

Returns:

See Also:



153
154
155
# File 'lib/tagen/core/pa/dir.rb', line 153

def ls(*args)
	each(*args).with_object([]){|pa,m| m<<pa.b}
end

#ls_r(*args) ⇒ Array<String>

ls with recursive

Returns:

See Also:



161
162
163
# File 'lib/tagen/core/pa/dir.rb', line 161

def ls_r(*args)
	each_r(*args).with_object([]){|pa,m| m<<pa.b}
end