class FolderContentsHistory
Public Class Methods
new(entries = {})
click to toggle source
# File lib/folder_contents_history.rb, line 4 def initialize(entries = {}) @entries = entries end
new_from_history(history_file)
click to toggle source
# File lib/folder_contents_history.rb, line 100 def self.new_from_history(history_file) entries = MultiJson.load(File.read(history_file)).reduce({}) do |hash, item| hash.update(item.first => FileNameHistory.new_from_history(item.last)) end FolderContentsHistory.new(entries) end
Public Instance Methods
changed()
click to toggle source
# File lib/folder_contents_history.rb, line 82 def changed @entries.select { |_key, value| value.changed? && value.present? } end
conflicts?()
click to toggle source
# File lib/folder_contents_history.rb, line 93 def conflicts? # changed.map { |entry| entry.last }.compact.uniq.length < changed.length @entries.map do |entry| entry.last.last_name end.compact.uniq.length < @entries.length end
execute()
click to toggle source
# File lib/folder_contents_history.rb, line 47 def execute changed.each_pair do |key, value| value.rename @entries[value.current] = @entries.delete(key) end end
load_entries(path = '.')
click to toggle source
# File lib/folder_contents_history.rb, line 8 def load_entries(path = '.') entries = Dir["#{path}/*"].reduce({}) do |hash, item| pathless = item.gsub(/#{path}\//, '') hash.update(pathless => FileNameHistory.new(pathless)) end @entries.merge!(entries) do |_key, old_hash, new_hash| old_hash.history.length > new_hash.history.length ? old_hash : new_hash end end
log(regexp = '', replacement = '')
click to toggle source
# File lib/folder_contents_history.rb, line 54 def log(regexp = '', replacement = '') if conflicts? puts <<-NOTE.gsub(/^.*\|/, '').colorize(:color => :red) |================================================== |!!! Some files would be lost by this operation !!! |================================================== NOTE end changed.each_value do |file_history| file_history.log end end
log_backup(where)
click to toggle source
# File lib/folder_contents_history.rb, line 67 def log_backup(where) with_history = @entries .select { |_name, history| history.history.length > 1 } backup = with_history.reduce({}) do |hash, item| hash.update(item.last.to_hash) end File.write(where, MultiJson.dump(backup, :pretty => true)) end
merge_histories(old, new)
click to toggle source
# File lib/folder_contents_history.rb, line 38 def merge_histories(old, new) new.reduce({}) do |hash, item| hash.tap do |obj| new_history = old.select { |history| history.was_named? item }.first obj.update(item => new_history.clone) unless new_history.nil? end end end
plan_reapplication(path)
click to toggle source
# File lib/folder_contents_history.rb, line 30 def plan_reapplication(path) entries = Dir["#{path}/*"].map { |file| file.gsub(/#{path}\//, '') } histories = @entries.values @entries = merge_histories(histories, entries) @entries.select! { |_name, history| history } @entries.each { |name, history| history.plan_reapplication(name) } end
plan_renames(regexp, replacement)
click to toggle source
# File lib/folder_contents_history.rb, line 18 def plan_renames(regexp, replacement) @entries.select { |key, _value| key[regexp] }.each_pair do |_key, value| value.plan_rename(regexp, replacement) end end
plan_rollbacks()
click to toggle source
# File lib/folder_contents_history.rb, line 24 def plan_rollbacks @entries.each_value do |file_history| file_history.plan_rollback end end
show_history(files)
click to toggle source
# File lib/folder_contents_history.rb, line 86 def show_history(files) files.each do |file| @entries[file] = FileNameHistory.new(file) unless @entries[file] @entries[file].print_history end end
to_hash(input_hash = @entries)
click to toggle source
# File lib/folder_contents_history.rb, line 76 def to_hash(input_hash = @entries) input_hash.reduce({}) do |hash, entry| hash.update(entry[0] => entry[1].to_hash) end end