Class: FlEd::FileListing
- Inherits:
-
Object
- Object
- FlEd::FileListing
- Defined in:
- lib/fled/file_listing.rb,
lib/fled/file_listing.rb,
lib/fled/file_listing.rb
Overview
Shell operation list builder
Constant Summary collapse
- RELATION_KEYS =
[:parent]
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
Class Method Summary collapse
Instance Method Summary collapse
- #[](uid) ⇒ Object
- #add(uid, object = {}) ⇒ Object
- #breadth_first(parent = nil, path = [], &block) ⇒ Object
- #children_of(parent, &blk) ⇒ Object
- #count ⇒ Object
- #depth_first(parent = nil, path = [], &block) ⇒ Object
- #dup ⇒ Object
- #each(&block) ⇒ Object
- #has_child?(parent, child) ⇒ Boolean
-
#initialize ⇒ FileListing
constructor
A new instance of FileListing.
-
#operations_from!(source_listing) ⇒ Object
Generate a list of operations that would transform the ‘source_listing` into the receiver.
- #path_name_of(object) ⇒ Object
- #path_of(object) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ FileListing
Returns a new instance of FileListing.
5 6 7 8 9 |
# File 'lib/fled/file_listing.rb', line 5 def initialize @objects_by_id = {} @objects = [] @errors = [] end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
10 11 12 |
# File 'lib/fled/file_listing.rb', line 10 def errors @errors end |
Class Method Details
.parse(listing) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/fled/file_listing.rb', line 56 def self.parse listing objects = self.new previous_indent = nil previous = nil stack = [] listing.split("\n").each_with_index do |line, line_number| next if line.strip.empty? raise RuntimeError, "Unparsable line #{line.inspect}" unless line =~ /^((?: )*)(.*?)(?::(\d+))?\r?$/ indent = $1 name = $2.strip if (dir = name[-1..-1] == "/") name = name[0..-2] end current = nil begin current = objects.add($3, :name => name, :line => "#{$1}#{$2}") rescue Exception => e (objects.errors ||= []) << [:fail, :duplicate_uid, {:name => name, :line_number => line_number + 1} ] end next unless current current[:dir] = true if dir next if name.strip == "" # Ignore indent when there is no name - element will be deleted, parent isnt used if previous_indent && previous_indent != indent if previous_indent.length < indent.length stack.push(previous) else stack.pop end end current[:line_number] = line_number + 1 current[:parent] = stack.last if stack.count > 0 previous = current previous_indent = indent end objects end |
Instance Method Details
#[](uid) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/fled/file_listing.rb', line 42 def [](uid) if uid.is_a?(String) @objects_by_id[uid] else @objects[uid] end end |
#add(uid, object = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/fled/file_listing.rb', line 32 def add uid, object = {} if uid uid = uid.to_s raise RuntimeError, "UID #{uid.inspect} already declared" if @objects_by_id[uid] @objects_by_id[uid] = object object[:uid] = uid end @objects << object object end |
#breadth_first(parent = nil, path = [], &block) ⇒ Object
229 230 231 232 233 234 235 236 |
# File 'lib/fled/file_listing.rb', line 229 def breadth_first parent = nil, path = [], &block to_browse = [] children_of parent do |child| yield child, path to_browse += [child] end to_browse.each { |child| breadth_first child, path + [child], &block } end |
#children_of(parent, &blk) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/fled/file_listing.rb', line 212 def children_of parent, &blk unless !parent || parent.is_a?(Hash) parent_object = self[parent] raise RuntimeError, "No parent #{parent.inspect} found" unless parent_object parent = parent_object end @objects. select { |e| (e[:parent].nil? && parent.nil?) || (e[:parent] == parent) }. sort { |a, b| a[:name] <=> b[:name] }. each(&blk) end |
#count ⇒ Object
31 |
# File 'lib/fled/file_listing.rb', line 31 def count ; @objects.count ; end |
#depth_first(parent = nil, path = [], &block) ⇒ Object
223 224 225 226 227 228 |
# File 'lib/fled/file_listing.rb', line 223 def depth_first parent = nil, path = [], &block children_of parent do |child| depth_first(child, path + [child], &block) yield child, path end end |
#dup ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/fled/file_listing.rb', line 12 def dup result = self.class.new each do |object| result.add object[:uid], object.dup end result.each do |object| RELATION_KEYS.each do |relation| next unless foreign = object[relation] unless foreign[:uid] raise RuntimeError, "Cannot duplicate anonymous object and maintain #{relation.inspect} referential" end unless (object[relation] = result[foreign[:uid]]) raise RuntimeError, "Cannot duplicate object in #{relation.inspect} referential, no duplicate found in target (?!)" end end end result end |
#each(&block) ⇒ Object
30 |
# File 'lib/fled/file_listing.rb', line 30 def each &block ; @objects.each(&block) ; end |
#has_child?(parent, child) ⇒ Boolean
195 196 197 198 199 200 |
# File 'lib/fled/file_listing.rb', line 195 def has_child? parent, child while child = child[:parent] return true if child[:uid] == parent[:uid] end false end |
#operations_from!(source_listing) ⇒ Object
Generate a list of operations that would transform the ‘source_listing` into the receiver.
Result is an array with each entry of the format:
-
‘[:fail, reason_symbol, target]`
-
‘[:warn, reason_symbol, target]`
-
‘[:mk, [path components]]`
-
‘[:moved, [source path components], [dest path components]]`
-
‘[:renamed, [source path components], new name]`
-
‘[:rm, [path components], source_object]`
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/fled/file_listing.rb', line 109 def operations_from! source_listing op_errors = [] operations = [] pending_renames = [] running_source = source_listing.dup self.breadth_first do |target, path| next if path.any? { |o| o[:error] } if target[:name] != "" && !target[:uid] operations << [:mk, self.path_of(target).map { |o| o[:name] }] fake_source = {:name => target[:name], :dir => true} fake_source[:parent] = running_source[path.last[:source][:uid]] unless path.empty? target_uid = "new_#{running_source.count}" target_uid += "_" while @objects_by_id[target_uid] || running_source[target_uid] target[:uid] = target_uid @objects_by_id[target_uid] = target target[:source] = running_source.add(target_uid, fake_source) else source = running_source[target[:uid]] if !(target[:source] = source) target[:error] = true op_errors += [[:fail, :no_such_uid, target]] next end next if target[:name] == "" if (target[:parent] || {})[:uid] != (source[:parent] ||{})[:uid] existing_names = running_source.children_of((target[:parent] || {})[:uid]).map { |o| o[:name] } new_name = target[:name] new_name += ".tmp" while existing_names.any? { |n| n.casecmp(new_name) == 0 } if new_name != target[:name] pending_renames << [:renamed, target, target[:name]] end if (target_parent = target[:parent]) until !target_parent || !target_parent[:source] || target_parent[:source][:dir] target_parent = target_parent[:parent] end end source_path = running_source.path_of(source).map { |o| o[:name] } target[:name] = source[:name] = new_name operations << [:moved, source_path, self.path_of(target_parent).map { |o| o[:name] } + [target[:name]] ] if target[:parent] source[:parent] = running_source[target[:parent][:uid]] else source.delete(:parent) end elsif target[:name] != source[:name] source_path = running_source.path_of(source).map { |o| o[:name] } existing_names = running_source.children_of((target[:parent] || {})[:uid]).map { |o| o[:name] } new_name = target[:name] new_name += ".tmp" while existing_names.any? { |n| n.casecmp(new_name) == 0 } if new_name != target[:name] pending_renames << [:renamed, target, target[:name]] end target[:name] = source[:name] = new_name operations << [:renamed, source_path, target[:name] ] end end end pending_renames.each do |op| target = op[1] new_name = op[2] existing_names = running_source.children_of((target[:parent] || {})[:uid]).map { |o| o[:name] } if existing_names.any? { |n| n.casecmp(new_name) == 0 } op_errors += [[:warn, :would_overwrite, target, running_source.path_of(target[:parent]).map { |o| o[:name] } + [new_name]]] else operations << [:renamed, running_source.path_of(target).map { |o| o[:name] }, new_name ] target[:name] = target[:source][:name] = new_name end end self.depth_first do |target, path| if target[:name] == "" operation = target[:source][:dir] ? :rmdir : :rm operations << [operation, running_source.path_of(target[:source]).map { |o| o[:name] }, target[:source]] end end errors + op_errors + operations end |
#path_name_of(object) ⇒ Object
201 202 203 |
# File 'lib/fled/file_listing.rb', line 201 def path_name_of object File.join(path_of(object).map { |o| o[:name] }) end |
#path_of(object) ⇒ Object
204 205 206 207 208 209 210 211 |
# File 'lib/fled/file_listing.rb', line 204 def path_of object res = [] while object res.unshift object object = object[:parent] end res end |
#to_s ⇒ Object
51 52 53 54 55 |
# File 'lib/fled/file_listing.rb', line 51 def to_s return "" if @objects.empty? max_width = @objects.map { |o| o[:line] }.map(&:length).max + 10 @objects.map { |e| "#{e[:line].ljust(max_width)}:#{e[:uid]}" }.join("\n") end |