lib/ld/file/file.rb in ld-0.3.5 vs lib/ld/file/file.rb in ld-0.3.6
- old
+ new
@@ -1,118 +1,198 @@
class Ld::File
- attr_accessor :path, :base_name, :name, :type
+ attr_accessor :path, :name, :exist, :size, :mode, :stat
+ @@current_path = Dir.pwd
+ @@exclude = ['.', '..']
+ #= 作用 创建一个Ld::File实例(不初始化参数)
def initialize path
- # raise "file is not found!\n#{path}" if !File.exist? path
- @path = path
+ @path = path[0] == '/' ? path : "#{Dir.pwd}/#{path}"
@name = File.basename @path
- @base_name = name.split('.')[0]
- @type = File.directory?(@path) ? 1 : 0
+ 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
- def self.open_dir path
- Ld::File.new path
+ #= 作用 创建一个Ld::File实例(始化参数)
+ def self.open path
+ self.new path
end
- def self.open path
- if File.exist? path
- self.new path
- else
- return nil
- end
+ # 作用 返回这个目录下的所有一级目录与一级文件,如果不是目录,会报错
+ 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
- def brothers
- father.children
+ def is_exclude? name
+ @@exclude.include? name
end
- def children(remove = nil)
- arr = []
- Dir.foreach(@path)do |p|
- removes = ['.','..','.DS_Store']
- removes << remove if remove
- if !removes.include?(p)
- arr << Ld::File.new("#{@path}/#{p}")
- end
- end
- arr.sort!{|a,b| b.type-a.type}
- arr
+ #= 作用 返回当前所在目录(Dir.pwd)
+ def self.current
+ Ld::File.new @@current_path
end
- def search_files regexp
- arr = []
- iter_search_files regexp, arr
- arr
+ def exist!
+ raise "不存在的 #{path}" if !@exist
end
- def search_dirs
- arr = []
- iter_search_dir arr
- arr
+ def dir?
+ type == 'directory'
end
- def iter_search_dir arr
- children.each do |f|
- if f.type == 1
- arr << f
- f.iter_search_dir arr
+ def file?
+ type == 'file'
+ end
+
+ def dir!
+ raise "这不是一个目录,而是一个#{type}:#{path}" if type != 'directory'
+ end
+
+ def file!
+ raise "这不是一个文件,而是一个#{type}:#{path}" if type != 'file'
+ end
+
+ #= 作用 返回一个一级目录或文件,如果不存在则返回nil
+ def find name
+ dir!
+ Ld::File.new "#{path}/#{name.to_s}" if File.exist? "#{path}/#{name.to_s}"
+ end
+
+ #= 作用 返回所有精确匹配的(查当前目录下的所有节点)目录和文件
+ 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
+
+ #= 作用 返回所有模糊匹配的(查当前目录下的所有节点)目录和文件
+ 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
+
+ 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
- self
end
- def iter_search_files regexp, arr
- children.each do |f|
- if f.type == 1
- f.iter_search_files regexp, arr
+ def iter_search_regexp regexp, results
+ children.each do |file|
+ if file.name.match(regexp)
+ results << file
end
- if f.name.match(regexp)
- arr << f
+ if file.dir?
+ file.iter_search_regexp regexp, results
end
end
- self
end
- def father
- arr = @path.split('/')
- arr.pop
- Ld::File.new(arr.join('/'))
+ #= 作用 返回所有lines
+ def lines
+ File.open(@path).readlines
end
- def find name
- name = name.to_s
- children.each do |f|
- if f.name == name
- return f
+ # def method_missing name
+ # find name
+ # end
+
+ #= 作用 改名称
+ def rename new_name
+ new_path = "#{dir.path}/#{new_name}"
+ if File.rename @path, new_path
+ @path = new_path
+ end
+ end
+
+ #= 作用 删除当前文件
+ def delete
+ puts "删除!:#{path}\n,确认请输入 delete_file,"
+ if gets.chomp == 'delete_file'
+ if File.delete path == 1
+ @exist = false
+ puts "删除成功 #{path}"
end
end
- return nil
end
- def read
- File.open(@path).read
+ #= 作用 返回所有文件(下一级)
+ def files
+ children.select{|f| f.type == 'file'}
end
- def readlines
- File.open(@path).readlines
+ #= 作用 返回父目录
+ def parent
+ Ld::File.new(File.dirname @path)
end
- def size
- File.size path
+ #= 作用 返回所有兄弟
+ def siblings
+ parent.children
end
- def lines
- arr = []
- File.new(path).each_line{|l| arr << l }
- arr
+ #= 作用 返回所有目录
+ def dirs
+ children.select{|f| f.type == 'directory'}
end
- def exist?
- File.exist? path
+ #= 作用 输出目录中所有条目
+ def ls
+ if type == 'directory'
+ Ld::Print.ls self
+ elsif type == 'file'
+ Ld::Print.ls self.parent
+ end
end
- def method_missing name
- find name
+ def save
+
+ end
+
+ def self.write path, arr
+ File.open(path)
+ end
+
+ # test:
+ def self.test
+ sdf{100.times{Ld::File.new('app').search_regexp //}}
+ end
+
+ def sdf &block
+ t1 = Time.new
+ block.call
+ t2 = Time.new
+ puts t2 - t1
end
end