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.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
-
.current ⇒ Object
作用 返回当前所在目录(Dir.pwd).
-
.open(path) ⇒ Object
作用 打开一个文件.
-
.test ⇒ Object
test:.
- .write(path, arr) ⇒ Object
Instance Method Summary collapse
-
#children ⇒ Object
作用 返回这个目录下的所有一级目录与一级文件,如果不是目录,会报错.
-
#delete ⇒ Object
作用 删除当前文件(有gets确认).
- #dir! ⇒ Object
-
#dir? ⇒ Boolean
作用 判断这是目录吗.
-
#dirs ⇒ Object
作用 返回所有目录.
- #exist! ⇒ Object
- #file! ⇒ Object
-
#file? ⇒ Boolean
作用 判断这是文件吗.
-
#files ⇒ Object
作用 返回所有文件.
-
#find(name) ⇒ Object
作用 查找文件或目录,返回一个一级目录或文件,如果不存在则返回nil.
-
#initialize(path) ⇒ File
constructor
A new instance of File.
- #is_exclude?(name) ⇒ Boolean
- #iter_search(name, results) ⇒ Object
- #iter_search_regexp(regexp, results) ⇒ Object
-
#lines ⇒ Object
作用 如果是一个文本文件,返回所有行.
-
#ls ⇒ Object
作用 输出目录中所有条目.
-
#parent ⇒ Object
作用 返回父目录.
-
#rename(new_name) ⇒ Object
作用 修改名称(目录或文件均可).
- #sdf(&block) ⇒ Object
-
#search(name, type = :all) ⇒ Object
作用 精确查找,返回所有匹配的目录和文件.
-
#search_regexp(regexp, type = :all) ⇒ Object
作用 模糊查找,返回所有匹配的目录和文件.
-
#siblings ⇒ Object
作用 返回所有兄弟.
Constructor Details
#initialize(path) ⇒ File
Returns a new instance of File.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ld/file/file.rb', line 7 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 |
#type ⇒ Object
Returns the value of attribute type.
3 4 5 |
# File 'lib/ld/file/file.rb', line 3 def type @type end |
Class Method Details
.current ⇒ Object
作用 返回当前所在目录(Dir.pwd)
38 39 40 |
# File 'lib/ld/file/file.rb', line 38 def self.current Ld::File.new @@current_path end |
.open(path) ⇒ Object
作用 打开一个文件
23 24 25 |
# File 'lib/ld/file/file.rb', line 23 def self.open path self.new path end |
.test ⇒ Object
test:
184 185 186 |
# File 'lib/ld/file/file.rb', line 184 def self.test sdf{100.times{Ld::File.new('app').search_regexp //}} end |
.write(path, arr) ⇒ Object
179 180 181 |
# File 'lib/ld/file/file.rb', line 179 def self.write path, arr File.open(path) end |
Instance Method Details
#children ⇒ Object
作用 返回这个目录下的所有一级目录与一级文件,如果不是目录,会报错
28 29 30 31 |
# File 'lib/ld/file/file.rb', line 28 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
作用 删除当前文件(有gets确认)
140 141 142 143 144 145 146 147 148 |
# File 'lib/ld/file/file.rb', line 140 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
56 57 58 |
# File 'lib/ld/file/file.rb', line 56 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
作用 返回所有目录
166 167 168 |
# File 'lib/ld/file/file.rb', line 166 def dirs children.select{|f| f.type == 'directory'} end |
#exist! ⇒ Object
42 43 44 |
# File 'lib/ld/file/file.rb', line 42 def exist! raise "不存在的 #{path}" if !@exist end |
#file! ⇒ Object
60 61 62 |
# File 'lib/ld/file/file.rb', line 60 def file! raise "这不是一个文件,而是一个#{type}:#{path}" if type != 'file' end |
#file? ⇒ Boolean
作用 判断这是文件吗
52 53 54 |
# File 'lib/ld/file/file.rb', line 52 def file? type == 'file' end |
#files ⇒ Object
作用 返回所有文件
151 152 153 |
# File 'lib/ld/file/file.rb', line 151 def files children.select{|f| f.type == 'file'} end |
#find(name) ⇒ Object
作用 查找文件或目录,返回一个一级目录或文件,如果不存在则返回nil
65 66 67 68 |
# File 'lib/ld/file/file.rb', line 65 def find name dir! Ld::File.new "#{path}/#{name.to_s}" if File.exist? "#{path}/#{name.to_s}" end |
#is_exclude?(name) ⇒ Boolean
33 34 35 |
# File 'lib/ld/file/file.rb', line 33 def is_exclude? name @@exclude.include? name end |
#iter_search(name, results) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ld/file/file.rb', line 100 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
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ld/file/file.rb', line 111 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
作用 如果是一个文本文件,返回所有行
123 124 125 |
# File 'lib/ld/file/file.rb', line 123 def lines File.open(@path).readlines end |
#ls ⇒ Object
作用 输出目录中所有条目
171 172 173 174 175 176 177 |
# File 'lib/ld/file/file.rb', line 171 def ls if type == 'directory' Ld::Print.ls self elsif type == 'file' Ld::Print.ls self.parent end end |
#parent ⇒ Object
作用 返回父目录
156 157 158 |
# File 'lib/ld/file/file.rb', line 156 def parent Ld::File.new(File.dirname @path) end |
#rename(new_name) ⇒ Object
作用 修改名称(目录或文件均可)
132 133 134 135 136 137 |
# File 'lib/ld/file/file.rb', line 132 def rename new_name new_path = "#{dir.path}/#{new_name}" if File.rename @path, new_path @path = new_path end end |
#sdf(&block) ⇒ Object
188 189 190 191 192 193 |
# File 'lib/ld/file/file.rb', line 188 def sdf &block t1 = Time.new block.call t2 = Time.new puts t2 - t1 end |
#search(name, type = :all) ⇒ Object
作用 精确查找,返回所有匹配的目录和文件
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ld/file/file.rb', line 71 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
作用 模糊查找,返回所有匹配的目录和文件
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ld/file/file.rb', line 86 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
作用 返回所有兄弟
161 162 163 |
# File 'lib/ld/file/file.rb', line 161 def siblings parent.children end |