Class: FolderContentsHistory
- Inherits:
-
Object
- Object
- FolderContentsHistory
- Defined in:
- lib/folder_contents_history.rb
Class Method Summary collapse
Instance Method Summary collapse
- #changed ⇒ Object
- #conflicts? ⇒ Boolean
- #execute ⇒ Object
-
#initialize(entries = {}) ⇒ FolderContentsHistory
constructor
A new instance of FolderContentsHistory.
- #load_entries(path = '.') ⇒ Object
- #log ⇒ Object
- #log_backup(where) ⇒ Object
- #merge_histories(old, new) ⇒ Object
- #plan_reapplication(path) ⇒ Object
- #plan_renames(regexp, replacement) ⇒ Object
- #plan_rollbacks ⇒ Object
- #show_history(files) ⇒ Object
- #to_hash(input_hash = @entries) ⇒ Object
Constructor Details
#initialize(entries = {}) ⇒ FolderContentsHistory
Returns a new instance of FolderContentsHistory.
4 5 6 |
# File 'lib/folder_contents_history.rb', line 4 def initialize(entries = {}) @entries = entries end |
Class Method Details
.new_from_history(history_file) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/folder_contents_history.rb', line 100 def self.new_from_history(history_file) entries = JSON.parse(File.read(history_file)).reduce({}) do |hash, item| hash.update(item.first => FileNameHistory.new_from_history(item.last)) end FolderContentsHistory.new(entries) end |
Instance Method Details
#changed ⇒ Object
82 83 84 |
# File 'lib/folder_contents_history.rb', line 82 def changed @entries.select { |_key, value| value.changed? && value.present? } end |
#conflicts? ⇒ Boolean
93 94 95 96 97 98 |
# 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 ⇒ Object
47 48 49 50 51 52 |
# 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 = '.') ⇒ Object
8 9 10 11 12 13 14 15 16 |
# 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 ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/folder_contents_history.rb', line 54 def log if conflicts? puts " |==================================================\n |!!! Some files would be lost by this operation !!!\n |==================================================\n NOTE\n end\n changed.each_value do |file_history|\n file_history.log\n end\nend\n".gsub(/^.*\|/, '') |
#log_backup(where) ⇒ Object
67 68 69 70 71 72 73 74 |
# 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, JSON.pretty_generate(backup)) end |
#merge_histories(old, new) ⇒ Object
38 39 40 41 42 43 44 45 |
# 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) ⇒ Object
30 31 32 33 34 35 36 |
# 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) ⇒ Object
18 19 20 21 22 |
# 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 ⇒ Object
24 25 26 27 28 |
# 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) ⇒ Object
86 87 88 89 90 91 |
# 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) ⇒ Object
76 77 78 79 80 |
# 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 |