Class: RightData::FileSystemTree
- Inherits:
-
Object
- Object
- RightData::FileSystemTree
- Defined in:
- lib/FileSystemTree.rb
Instance Attribute Summary collapse
-
#duplicate_children ⇒ Object
readonly
Returns the value of attribute duplicate_children.
-
#duplicates ⇒ Object
Returns the value of attribute duplicates.
-
#ignorable ⇒ Object
Returns the value of attribute ignorable.
-
#ignore_children ⇒ Object
readonly
Returns the value of attribute ignore_children.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#relativePath ⇒ Object
readonly
Returns the value of attribute relativePath.
Class Method Summary collapse
Instance Method Summary collapse
- #basename ⇒ Object
- #childAtIndex(n) ⇒ Object
- #children ⇒ Object
- #children? ⇒ Boolean
- #duplicate? ⇒ Boolean
- #duplicate_files ⇒ Object
-
#files ⇒ Object
TODO: Do this for LEAVES instead of files.
- #fullPath ⇒ Object
- #ignorable? ⇒ Boolean
- #ignore_files ⇒ Object
- #increment_duplicate_children ⇒ Object
- #increment_ignorable_children ⇒ Object
-
#initialize(path, args) ⇒ FileSystemTree
constructor
A new instance of FileSystemTree.
- #leaf? ⇒ Boolean
- #numberOfChildren ⇒ Object
- #other_children ⇒ Object
- #path ⇒ Object
- #put_for_shell(pre, path, comment) ⇒ Object
-
#report(pre = "") ⇒ Object
Inspect the nodes:.
- #to_param ⇒ Object
- #to_s ⇒ Object
-
#traverse(&block) ⇒ Object
Allow proc to decide if we traverse.
- #update_duplicate_ignorable_status ⇒ Object
Constructor Details
#initialize(path, args) ⇒ FileSystemTree
Returns a new instance of FileSystemTree.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/FileSystemTree.rb', line 13 def initialize path, args if args[:parent] @relativePath = File.basename(path) @parent = args[:parent] else @relativePath = path @parent = nil end @ignorable = false @duplicates = [] # for this node @duplicate_children = 0 # counts for children @ignore_children = 0 self end |
Instance Attribute Details
#duplicate_children ⇒ Object (readonly)
Returns the value of attribute duplicate_children.
9 10 11 |
# File 'lib/FileSystemTree.rb', line 9 def duplicate_children @duplicate_children end |
#duplicates ⇒ Object
Returns the value of attribute duplicates.
10 11 12 |
# File 'lib/FileSystemTree.rb', line 10 def duplicates @duplicates end |
#ignorable ⇒ Object
Returns the value of attribute ignorable.
11 12 13 |
# File 'lib/FileSystemTree.rb', line 11 def ignorable @ignorable end |
#ignore_children ⇒ Object (readonly)
Returns the value of attribute ignore_children.
8 9 10 |
# File 'lib/FileSystemTree.rb', line 8 def ignore_children @ignore_children end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/FileSystemTree.rb', line 6 def parent @parent end |
#relativePath ⇒ Object (readonly)
Returns the value of attribute relativePath.
5 6 7 |
# File 'lib/FileSystemTree.rb', line 5 def relativePath @relativePath end |
Class Method Details
.rootItem ⇒ Object
48 49 50 |
# File 'lib/FileSystemTree.rb', line 48 def self.rootItem @rootItem ||= self.new '/', :parent => nil end |
Instance Method Details
#basename ⇒ Object
46 |
# File 'lib/FileSystemTree.rb', line 46 def basename; @relativePath; end |
#childAtIndex(n) ⇒ Object
72 73 74 |
# File 'lib/FileSystemTree.rb', line 72 def childAtIndex n children[n] end |
#children ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/FileSystemTree.rb', line 52 def children unless @children if File.directory?(fullPath) and File.readable?(fullPath) @children = Dir.entries(fullPath).select { |path| path != '.' and path != '..' }.map { |path| FileSystemTree.new path, :parent => self } else @children = nil end end @children end |
#children? ⇒ Boolean
80 |
# File 'lib/FileSystemTree.rb', line 80 def children?; !children.nil? && !children.empty?; end |
#duplicate? ⇒ Boolean
82 83 84 85 86 87 88 |
# File 'lib/FileSystemTree.rb', line 82 def duplicate? if leaf? !duplicates.empty? else # Dup if all ignored / dup children ((@ignore_children + @duplicate_children) == numberOfChildren) end end |
#duplicate_files ⇒ Object
39 40 41 42 43 |
# File 'lib/FileSystemTree.rb', line 39 def duplicate_files return 0 if leaf? && File.directory?(fullPath) return duplicate? ? 1 : 0 if leaf? return children.map {|n| n.duplicate_files}.inject {|sum, n| sum + n } end |
#files ⇒ Object
TODO: Do this for LEAVES instead of files. Include empty dirs.
29 30 31 32 33 |
# File 'lib/FileSystemTree.rb', line 29 def files return 0 if leaf? && File.directory?(fullPath) return 1 if leaf? return children.map {|n| n.files}.inject {|sum, n| sum + n } end |
#fullPath ⇒ Object
68 69 70 |
# File 'lib/FileSystemTree.rb', line 68 def fullPath @parent ? File.join(@parent.fullPath, @relativePath) : @relativePath end |
#ignorable? ⇒ Boolean
90 |
# File 'lib/FileSystemTree.rb', line 90 def ignorable?; ignorable; end |
#ignore_files ⇒ Object
34 35 36 37 38 |
# File 'lib/FileSystemTree.rb', line 34 def ignore_files return 0 if leaf? && File.directory?(fullPath) return ignorable? ? 1 : 0 if leaf? return children.map {|n| n.ignore_files}.inject {|sum, n| sum + n } end |
#increment_duplicate_children ⇒ Object
101 102 103 104 |
# File 'lib/FileSystemTree.rb', line 101 def increment_duplicate_children @duplicate_children += 1 update_duplicate_ignorable_status end |
#increment_ignorable_children ⇒ Object
92 93 94 95 |
# File 'lib/FileSystemTree.rb', line 92 def increment_ignorable_children @ignore_children += 1 update_duplicate_ignorable_status end |
#leaf? ⇒ Boolean
106 |
# File 'lib/FileSystemTree.rb', line 106 def leaf?; !children?; end |
#numberOfChildren ⇒ Object
76 77 78 |
# File 'lib/FileSystemTree.rb', line 76 def numberOfChildren children == nil ? -1 : children.size end |
#other_children ⇒ Object
114 115 116 |
# File 'lib/FileSystemTree.rb', line 114 def other_children children.size - ignore_children - duplicate_children end |
#path ⇒ Object
67 |
# File 'lib/FileSystemTree.rb', line 67 def path; fullPath; end |
#put_for_shell(pre, path, comment) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/FileSystemTree.rb', line 123 def put_for_shell(pre,path,comment) if(pre.empty?) puts Escape.shell_command([path, "# #{comment}"]) else puts Escape.shell_command([pre.split(" "), path, "# #{comment}"].flatten) end end |
#report(pre = "") ⇒ Object
Inspect the nodes:
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/FileSystemTree.rb', line 132 def report(pre="") pre += " " if !pre.empty? self.traverse do |n| # Is this a leaf (e.g. a file)? if n.leaf? if(File.directory?(n.path)) # Prune empty dirs! put_for_shell(pre,n.path,"Empty dir") # Remove the dups/igns! #puts "#{pre}'#{n.path.gsub(/'/,"\\\\'")}' # Empty dir" else msg = nil msg = " dup(#{n.duplicates.count})" if n.duplicate? msg = " ign" if n.ignorable? if msg put_for_shell(pre,n.path,msg) # Remove the dups/igns! # puts "#{pre}'#{n.path.gsub(/'/,"\\\\'")}' #{msg}" # Remove the dups/igns! else puts "# #{n.path} unique" end end false # Don't traverse deeper! else if n.duplicate_children + n.ignore_children == n.children.size put_for_shell(pre,n.path,"#{n.duplicate_children} dups / #{n.ignore_children} ignores") # puts "#{pre}'#{n.path.gsub(/'/,"\\\\'")}' # #{n.duplicate_children} dups / #{n.ignore_children} ignores" false # Don't traverse deeper! elsif n.children.size == 0 put_for_shell(pre,n.path," Empty") # puts "#{pre}'#{n.path.gsub(/'/,"\\\\'")}' # Empty... " false else puts "# #{n.path} # Note #{n.duplicate_children} dup/ #{n.ignore_children} ign / #{n.other_children} other " true end end end puts "# #{self.ignore_files} ignores, #{self.duplicate_files} dups of #{self.files} files" end |
#to_param ⇒ Object
118 |
# File 'lib/FileSystemTree.rb', line 118 def to_param; to_s; end |
#to_s ⇒ Object
119 120 121 |
# File 'lib/FileSystemTree.rb', line 119 def to_s "<Tree :path => #{self.path}, :files => #{self.files}>" end |
#traverse(&block) ⇒ Object
Allow proc to decide if we traverse
108 109 110 111 112 |
# File 'lib/FileSystemTree.rb', line 108 def traverse(&block) # Allow proc to decide if we traverse if block.call(self) && children? children.each { |c| c.traverse(&block) } end end |
#update_duplicate_ignorable_status ⇒ Object
97 98 99 |
# File 'lib/FileSystemTree.rb', line 97 def update_duplicate_ignorable_status parent.increment_duplicate_children if((@ignore_children + @duplicate_children) == numberOfChildren) end |