Class: Ld::Dir

Inherits:
Object
  • Object
show all
Defined in:
lib/ld/file/dir.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Dir

Returns a new instance of Dir.



6
7
8
9
10
11
# File 'lib/ld/file/dir.rb', line 6

def initialize path
  @path = path
  @name = @path.split('/').last
  @my = Ld::File.new @path
  get_all
end

Instance Attribute Details

#all_dirsObject

Returns the value of attribute all_dirs.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def all_dirs
  @all_dirs
end

#all_filesObject

Returns the value of attribute all_files.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def all_files
  @all_files
end

#all_files_sumObject

Returns the value of attribute all_files_sum.



4
5
6
# File 'lib/ld/file/dir.rb', line 4

def all_files_sum
  @all_files_sum
end

#all_othersObject

Returns the value of attribute all_others.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def all_others
  @all_others
end

#all_others_sumObject

Returns the value of attribute all_others_sum.



4
5
6
# File 'lib/ld/file/dir.rb', line 4

def all_others_sum
  @all_others_sum
end

#dirsObject

Returns the value of attribute dirs.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def dirs
  @dirs
end

#filesObject

Returns the value of attribute files.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def files
  @files
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def name
  @name
end

#othersObject

Returns the value of attribute others.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def others
  @others
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/ld/file/dir.rb', line 3

def path
  @path
end

Instance Method Details

#detailsObject



49
50
51
52
53
54
55
56
57
# File 'lib/ld/file/dir.rb', line 49

def details
  title = "目录名称:#{path.split('/').last}
  目录:#{@all_dirs.size}(个)
  文件:#{@all_files_sum}(Mb)/#{@all_files.size}(个)
  其它:#{@all_others_sum}(Kb)/#{@all_others.size}(个)"
  headings = ['文件类型(后缀)', '数量(个)', '大小(Kb)']
  rows = @all_suffix.map{|k,v| [k, v[0], v[1]]}.sort{|b,a| a[1] <=> b[1]}
  Ld::Print.print headings:headings,rows:rows,title:title
end

#get_allObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ld/file/dir.rb', line 13

def get_all
  @all = @my.search_regexp //, :all
  @all_files = []
  @all_dirs = []
  @all_others = []
  @all.each do |a|
    case a.type
      when 'directory'
        @all_dirs << a
      when 'file'
        @all_files << a
      else
        @all_others << a
    end
  end
  @all_files_sum  = (@all_files.map(&:size).sum.to_f / 1024 / 1024).round(1)
  @all_others_sum = (@all_others.map(&:size).sum.to_i / 1024).round(1)
  @all_suffix = {}
  @all_files.map(&:suffix).uniq.each do |suffix|
    @all_suffix[suffix] = get_suffix_count(suffix)
  end
  nil
end

#get_suffix_count(suffix) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ld/file/dir.rb', line 37

def get_suffix_count suffix
  count = 0
  size = 0
  @all_files.each do |f|
    if f.suffix == suffix
      count += 1
      size += f.size
    end
  end
  return [count, (size.to_f / 1024).round(2)]
end

#search_all_suffix(regexp) ⇒ Object



59
60
61
# File 'lib/ld/file/dir.rb', line 59

def search_all_suffix regexp
  @all_files.select{|f| f.suffix == regexp}
end