Class: Ld::File
- Inherits:
-
Object
- Object
- Ld::File
- Defined in:
- lib/ld/file/file.rb
Constant Summary collapse
- @@current_path =
Dir.pwd
- @@exclude =
['.', '..']
Instance Attribute Summary collapse
-
#exist ⇒ Object
Returns the value of attribute exist.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#size ⇒ Object
Returns the value of attribute size.
-
#stat ⇒ Object
Returns the value of attribute stat.
Class Method Summary collapse
-
.current ⇒ Object
作用 返回当前所在目录(Dir.pwd).
-
.open(path) ⇒ Object
作用 创建一个Ld::File实例(始化参数).
-
.test ⇒ Object
test:.
- .write(path, arr) ⇒ Object
Instance Method Summary collapse
-
#children ⇒ Object
作用 返回这个目录下的所有一级目录与一级文件,如果不是目录,会报错.
-
#delete ⇒ Object
作用 删除当前文件.
- #dir! ⇒ Object
- #dir? ⇒ Boolean
-
#dirs ⇒ Object
作用 返回所有目录.
- #exist! ⇒ Object
- #file! ⇒ Object
- #file? ⇒ Boolean
-
#files ⇒ Object
作用 返回所有文件(下一级).
-
#find(name) ⇒ Object
作用 返回一个一级目录或文件,如果不存在则返回nil.
-
#initialize(path) ⇒ File
constructor
作用 创建一个Ld::File实例(不初始化参数).
- #is_exclude?(name) ⇒ Boolean
- #iter_search(name, results) ⇒ Object
- #iter_search_regexp(regexp, results) ⇒ Object
-
#lines ⇒ Object
作用 返回所有lines.
-
#ls ⇒ Object
作用 输出目录中所有条目.
-
#parent ⇒ Object
作用 返回父目录.
-
#rename(new_name) ⇒ Object
作用 改名称.
- #save ⇒ Object
- #sdf(&block) ⇒ Object
-
#search(name, type = :all) ⇒ Object
作用 返回所有精确匹配的(查当前目录下的所有节点)目录和文件.
-
#search_regexp(regexp, type = :all) ⇒ Object
作用 返回所有模糊匹配的(查当前目录下的所有节点)目录和文件.
-
#siblings ⇒ Object
作用 返回所有兄弟.
Constructor Details
#initialize(path) ⇒ File
作用 创建一个Ld::File实例(不初始化参数)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ld/file/file.rb', line 8 def initialize path @path = path[0] == '/' ? path : "#{Dir.pwd}/#{path}" @name = File.basename @path if File.exist? @path @exist = true @type = File.ftype path @stat = File.stat path @size = @stat.size @mode = @stat.mode else @exist = false @type = 'not found' end end |
Instance Attribute Details
#exist ⇒ Object
Returns the value of attribute exist.
3 4 5 |
# File 'lib/ld/file/file.rb', line 3 def exist @exist end |
#mode ⇒ Object
Returns the value of attribute mode.
3 4 5 |
# File 'lib/ld/file/file.rb', line 3 def mode @mode end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/ld/file/file.rb', line 3 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/ld/file/file.rb', line 3 def path @path end |
#size ⇒ Object
Returns the value of attribute size.
3 4 5 |
# File 'lib/ld/file/file.rb', line 3 def size @size end |
#stat ⇒ Object
Returns the value of attribute stat.
3 4 5 |
# File 'lib/ld/file/file.rb', line 3 def stat @stat end |
Class Method Details
.current ⇒ Object
作用 返回当前所在目录(Dir.pwd)
39 40 41 |
# File 'lib/ld/file/file.rb', line 39 def self.current Ld::File.new @@current_path end |
.open(path) ⇒ Object
作用 创建一个Ld::File实例(始化参数)
24 25 26 |
# File 'lib/ld/file/file.rb', line 24 def self.open path self.new path end |
.test ⇒ Object
test:
187 188 189 |
# File 'lib/ld/file/file.rb', line 187 def self.test sdf{100.times{Ld::File.new('app').search_regexp //}} end |
.write(path, arr) ⇒ Object
182 183 184 |
# File 'lib/ld/file/file.rb', line 182 def self.write path, arr File.open(path) end |
Instance Method Details
#children ⇒ Object
作用 返回这个目录下的所有一级目录与一级文件,如果不是目录,会报错
29 30 31 32 |
# File 'lib/ld/file/file.rb', line 29 def children dir! Dir.foreach(@path).map{|n| Ld::File.new("#{@path}/#{n}") if !is_exclude? n}.compact.sort{|a,b| a.type <=> b.type} end |
#delete ⇒ Object
作用 删除当前文件
139 140 141 142 143 144 145 146 147 |
# File 'lib/ld/file/file.rb', line 139 def delete puts "删除!:#{path}\n,确认请输入 delete_file," if gets.chomp == 'delete_file' if File.delete path == 1 @exist = false puts "删除成功 #{path}" end end end |
#dir! ⇒ Object
55 56 57 |
# File 'lib/ld/file/file.rb', line 55 def dir! raise "这不是一个目录,而是一个#{type}:#{path}" if type != 'directory' end |
#dir? ⇒ Boolean
47 48 49 |
# File 'lib/ld/file/file.rb', line 47 def dir? type == 'directory' end |
#dirs ⇒ Object
作用 返回所有目录
165 166 167 |
# File 'lib/ld/file/file.rb', line 165 def dirs children.select{|f| f.type == 'directory'} end |
#exist! ⇒ Object
43 44 45 |
# File 'lib/ld/file/file.rb', line 43 def exist! raise "不存在的 #{path}" if !@exist end |
#file! ⇒ Object
59 60 61 |
# File 'lib/ld/file/file.rb', line 59 def file! raise "这不是一个文件,而是一个#{type}:#{path}" if type != 'file' end |
#file? ⇒ Boolean
51 52 53 |
# File 'lib/ld/file/file.rb', line 51 def file? type == 'file' end |
#files ⇒ Object
作用 返回所有文件(下一级)
150 151 152 |
# File 'lib/ld/file/file.rb', line 150 def files children.select{|f| f.type == 'file'} end |
#find(name) ⇒ Object
作用 返回一个一级目录或文件,如果不存在则返回nil
64 65 66 67 |
# File 'lib/ld/file/file.rb', line 64 def find name dir! Ld::File.new "#{path}/#{name.to_s}" if File.exist? "#{path}/#{name.to_s}" end |
#is_exclude?(name) ⇒ Boolean
34 35 36 |
# File 'lib/ld/file/file.rb', line 34 def is_exclude? name @@exclude.include? name end |
#iter_search(name, results) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ld/file/file.rb', line 99 def iter_search name, results children.each do |file| if file.name == name results << file end if file.dir? file.iter_search name, results end end end |
#iter_search_regexp(regexp, results) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/ld/file/file.rb', line 110 def iter_search_regexp regexp, results children.each do |file| if file.name.match(regexp) results << file end if file.dir? file.iter_search_regexp regexp, results end end end |
#lines ⇒ Object
作用 返回所有lines
122 123 124 |
# File 'lib/ld/file/file.rb', line 122 def lines File.open(@path).readlines end |
#ls ⇒ Object
作用 输出目录中所有条目
170 171 172 173 174 175 176 |
# File 'lib/ld/file/file.rb', line 170 def ls if type == 'directory' Ld::Print.ls self elsif type == 'file' Ld::Print.ls self.parent end end |
#parent ⇒ Object
作用 返回父目录
155 156 157 |
# File 'lib/ld/file/file.rb', line 155 def parent Ld::File.new(File.dirname @path) end |
#rename(new_name) ⇒ Object
作用 改名称
131 132 133 134 135 136 |
# File 'lib/ld/file/file.rb', line 131 def rename new_name new_path = "#{dir.path}/#{new_name}" if File.rename @path, new_path @path = new_path end end |
#save ⇒ Object
178 179 180 |
# File 'lib/ld/file/file.rb', line 178 def save end |
#sdf(&block) ⇒ Object
191 192 193 194 195 196 |
# File 'lib/ld/file/file.rb', line 191 def sdf &block t1 = Time.new block.call t2 = Time.new puts t2 - t1 end |
#search(name, type = :all) ⇒ Object
作用 返回所有精确匹配的(查当前目录下的所有节点)目录和文件
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ld/file/file.rb', line 70 def search name, type = :all dir! results = [] iter_search name, results case type.to_s when 'all' results when 'file' results.map{|f| f.type == 'file'} when 'dir' results.map{|f| f.type == 'directory'} end end |
#search_regexp(regexp, type = :all) ⇒ Object
作用 返回所有模糊匹配的(查当前目录下的所有节点)目录和文件
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ld/file/file.rb', line 85 def search_regexp regexp, type = :all dir! results = [] iter_search_regexp regexp, results case type.to_s when 'all' results when 'file' results.map{|f| f.type == 'file'} when 'dir' results.map{|f| f.type == 'directory'} end end |
#siblings ⇒ Object
作用 返回所有兄弟
160 161 162 |
# File 'lib/ld/file/file.rb', line 160 def siblings parent.children end |