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.
-
#suffix ⇒ Object
Returns the value of attribute suffix.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
-
.base64(path) ⇒ Object
编码base64.
-
.current ⇒ Object
作用 返回当前所在目录(Dir.pwd).
-
.open(path) ⇒ Object
作用 打开一个文件.
-
.test ⇒ Object
test:.
- .write(path, arr) ⇒ Object
-
.write_image_by_base64_and_path(base64, path) ⇒ Object
解码base64,并写入图片.
Instance Method Summary collapse
-
#children ⇒ Object
作用 返回这个目录下的所有一级目录与一级文件,如果不是目录,会报错.
-
#delete ⇒ Object
作用 删除当前文件(有gets确认).
- #details ⇒ Object
- #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
作用 如果是一个文本文件,返回所有行.
- #look ⇒ Object
-
#ls ⇒ Object
作用 输出目录中所有条目.
-
#parent ⇒ Object
作用 返回父目录.
- #read_attrs ⇒ Object
-
#rename(new_name) ⇒ Object
作用 修改名称(目录或文件均可).
- #sdf(&block) ⇒ Object
-
#search(name, type = :all) ⇒ Object
作用 精确查找,返回所有匹配的目录和文件.
-
#search_regexp(regexp, type = :all) ⇒ Object
作用 模糊查找,返回所有匹配的目录和文件.
-
#siblings ⇒ Object
作用 返回所有兄弟.
Constructor Details
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 |
#suffix ⇒ Object
Returns the value of attribute suffix.
3 4 5 |
# File 'lib/ld/file/file.rb', line 3 def suffix @suffix 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
.base64(path) ⇒ Object
编码base64
21 22 23 |
# File 'lib/ld/file/file.rb', line 21 def self.base64 path Base64.encode64(File.open(path, 'rb').read) end |
.current ⇒ Object
作用 返回当前所在目录(Dir.pwd)
60 61 62 |
# File 'lib/ld/file/file.rb', line 60 def self.current Ld::File.new @@current_path end |
.open(path) ⇒ Object
作用 打开一个文件
14 15 16 17 18 |
# File 'lib/ld/file/file.rb', line 14 def self.open path f = self.new path f.read_attrs f end |
.test ⇒ Object
test:
206 207 208 |
# File 'lib/ld/file/file.rb', line 206 def self.test sdf{100.times{Ld::File.new('app').search_regexp //}} end |
Instance Method Details
#children ⇒ Object
作用 返回这个目录下的所有一级目录与一级文件,如果不是目录,会报错
50 51 52 53 |
# File 'lib/ld/file/file.rb', line 50 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确认)
162 163 164 165 166 167 168 169 170 |
# File 'lib/ld/file/file.rb', line 162 def delete puts "删除!:#{path}\n,确认请输入 delete_file," if gets.chomp == 'delete_file' if File.delete path == 1 @exist = false puts "删除成功 #{path}" end end end |
#details ⇒ Object
217 218 219 |
# File 'lib/ld/file/file.rb', line 217 def details Ld::Dir.new(@path).details end |
#dir! ⇒ Object
78 79 80 |
# File 'lib/ld/file/file.rb', line 78 def dir! raise "这不是一个目录,而是一个#{type}:#{path}" if type != 'directory' end |
#dir? ⇒ Boolean
作用 判断这是目录吗
69 70 71 |
# File 'lib/ld/file/file.rb', line 69 def dir? type == 'directory' end |
#dirs ⇒ Object
作用 返回所有目录
188 189 190 |
# File 'lib/ld/file/file.rb', line 188 def dirs children.select{|f| f.type == 'directory'} end |
#exist! ⇒ Object
64 65 66 |
# File 'lib/ld/file/file.rb', line 64 def exist! raise "不存在的 #{path}" if !@exist end |
#file! ⇒ Object
82 83 84 |
# File 'lib/ld/file/file.rb', line 82 def file! raise "这不是一个文件,而是一个#{type}:#{path}" if type != 'file' end |
#file? ⇒ Boolean
作用 判断这是文件吗
74 75 76 |
# File 'lib/ld/file/file.rb', line 74 def file? type == 'file' end |
#files ⇒ Object
作用 返回所有文件
173 174 175 |
# File 'lib/ld/file/file.rb', line 173 def files children.select{|f| f.type == 'file'} end |
#find(name) ⇒ Object
作用 查找文件或目录,返回一个一级目录或文件,如果不存在则返回nil
87 88 89 90 |
# File 'lib/ld/file/file.rb', line 87 def find name dir! Ld::File.new "#{path}/#{name.to_s}" if File.exist? "#{path}/#{name.to_s}" end |
#is_exclude?(name) ⇒ Boolean
55 56 57 |
# File 'lib/ld/file/file.rb', line 55 def is_exclude? name @@exclude.include? name end |
#iter_search(name, results) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ld/file/file.rb', line 122 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
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ld/file/file.rb', line 133 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
作用 如果是一个文本文件,返回所有行
145 146 147 |
# File 'lib/ld/file/file.rb', line 145 def lines File.open(@path).readlines end |
#look ⇒ Object
45 46 47 |
# File 'lib/ld/file/file.rb', line 45 def look puts lines end |
#ls ⇒ Object
作用 输出目录中所有条目
193 194 195 196 197 198 199 |
# File 'lib/ld/file/file.rb', line 193 def ls if type == 'directory' Ld::Print.ls self elsif type == 'file' Ld::Print.ls self.parent end end |
#parent ⇒ Object
作用 返回父目录
178 179 180 |
# File 'lib/ld/file/file.rb', line 178 def parent Ld::File.new(File.dirname @path) end |
#read_attrs ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ld/file/file.rb', line 30 def read_attrs @suffix = @name.split('.').last @suffix = @suffix == @name ? nil : @suffix 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 |
#rename(new_name) ⇒ Object
作用 修改名称(目录或文件均可)
154 155 156 157 158 159 |
# File 'lib/ld/file/file.rb', line 154 def rename new_name new_path = "#{dir.path}/#{new_name}" if File.rename @path, new_path @path = new_path end end |
#sdf(&block) ⇒ Object
210 211 212 213 214 215 |
# File 'lib/ld/file/file.rb', line 210 def sdf &block t1 = Time.new block.call t2 = Time.new puts t2 - t1 end |
#search(name, type = :all) ⇒ Object
作用 精确查找,返回所有匹配的目录和文件
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ld/file/file.rb', line 93 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
作用 模糊查找,返回所有匹配的目录和文件
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ld/file/file.rb', line 108 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
作用 返回所有兄弟
183 184 185 |
# File 'lib/ld/file/file.rb', line 183 def siblings parent.children end |