Class: RightData::FileSystemItem

Inherits:
Object
  • Object
show all
Defined in:
lib/FileSystemItem.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, args) ⇒ FileSystemItem

Returns a new instance of FileSystemItem.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/FileSystemItem.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_childrenObject (readonly)

Returns the value of attribute duplicate_children.



9
10
11
# File 'lib/FileSystemItem.rb', line 9

def duplicate_children
  @duplicate_children
end

#duplicatesObject

Returns the value of attribute duplicates.



10
11
12
# File 'lib/FileSystemItem.rb', line 10

def duplicates
  @duplicates
end

#ignorableObject

Returns the value of attribute ignorable.



11
12
13
# File 'lib/FileSystemItem.rb', line 11

def ignorable
  @ignorable
end

#ignore_childrenObject (readonly)

Returns the value of attribute ignore_children.



8
9
10
# File 'lib/FileSystemItem.rb', line 8

def ignore_children
  @ignore_children
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/FileSystemItem.rb', line 6

def parent
  @parent
end

#relativePathObject (readonly)

Returns the value of attribute relativePath.



5
6
7
# File 'lib/FileSystemItem.rb', line 5

def relativePath
  @relativePath
end

Class Method Details

.rootItemObject



48
49
50
# File 'lib/FileSystemItem.rb', line 48

def self.rootItem
  @rootItem ||= self.new '/', :parent => nil
end

Instance Method Details

#basenameObject



46
# File 'lib/FileSystemItem.rb', line 46

def basename; @relativePath; end

#childAtIndex(n) ⇒ Object



72
73
74
# File 'lib/FileSystemItem.rb', line 72

def childAtIndex n
  children[n]
end

#childrenObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/FileSystemItem.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|
          FileSystemItem.new path, :parent => self
       }
     else
       @children = nil
     end
  end
  @children
end

#children?Boolean

Returns:

  • (Boolean)


80
# File 'lib/FileSystemItem.rb', line 80

def children?; !children.nil? && !children.empty?; end

#duplicate?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
# File 'lib/FileSystemItem.rb', line 82

def duplicate?
  if leaf?
    !duplicates.empty?
  else # Dup if all ignored / dup children
    ((@ignore_children + @duplicate_children) == numberOfChildren)
  end
end

#duplicate_filesObject



39
40
41
42
43
# File 'lib/FileSystemItem.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

#filesObject

TODO: Do this for LEAVES instead of files. Include empty dirs.



29
30
31
32
33
# File 'lib/FileSystemItem.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

#fullPathObject



68
69
70
# File 'lib/FileSystemItem.rb', line 68

def fullPath
  @parent ? File.join(@parent.fullPath, @relativePath) : @relativePath
end

#ignorable?Boolean

Returns:

  • (Boolean)


90
# File 'lib/FileSystemItem.rb', line 90

def ignorable?; ignorable; end

#ignore_filesObject



34
35
36
37
38
# File 'lib/FileSystemItem.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_childrenObject



105
106
107
108
# File 'lib/FileSystemItem.rb', line 105

def increment_duplicate_children
  @duplicate_children += 1
  update_duplicate_ignorable_status
end

#increment_ignorable_childrenObject



92
93
94
95
# File 'lib/FileSystemItem.rb', line 92

def increment_ignorable_children
  @ignore_children += 1
  update_duplicate_ignorable_status
end

#leaf?Boolean

Returns:

  • (Boolean)


110
# File 'lib/FileSystemItem.rb', line 110

def leaf?; !children?; end

#numberOfChildrenObject



76
77
78
# File 'lib/FileSystemItem.rb', line 76

def numberOfChildren
  children == nil ? -1 : children.size
end

#other_childrenObject



118
119
120
# File 'lib/FileSystemItem.rb', line 118

def other_children
  children.size - ignore_children - duplicate_children
end

#pathObject



67
# File 'lib/FileSystemItem.rb', line 67

def path; fullPath; end

#put_for_shell(pre, path, comment) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/FileSystemItem.rb', line 127

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:



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
170
171
172
173
# File 'lib/FileSystemItem.rb', line 136

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_paramObject



122
# File 'lib/FileSystemItem.rb', line 122

def to_param; to_s; end

#to_sObject



123
124
125
# File 'lib/FileSystemItem.rb', line 123

def to_s
  "<Tree :path => #{self.path}, :files => #{self.files}>" 
end

#traverse(&block) ⇒ Object

Allow proc to decide if we traverse



112
113
114
115
116
# File 'lib/FileSystemItem.rb', line 112

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_statusObject



97
98
99
100
101
102
103
# File 'lib/FileSystemItem.rb', line 97

def update_duplicate_ignorable_status
  if parent.nil?
    puts "# PARENT IS NIL TRYING TO update_duplicate_ignorable_status @ #{self.path}"
  else
    parent.increment_duplicate_children if((@ignore_children + @duplicate_children) == numberOfChildren)
  end
end