Class: FolderContentsHistory

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries = {}) ⇒ FolderContentsHistory

Returns a new instance of FolderContentsHistory.



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

def initialize(entries = {})
  @entries = entries
end

Class Method Details

.new_from_history(history_file) ⇒ Object



94
95
96
97
98
99
# File 'lib/folder_contents_history.rb', line 94

def self.new_from_history(history_file)
  entries = JSON.parse(File.read(history_file)).inject({}) do |hash, item|
    hash.update(item.first => FileNameHistory.new_from_history(item.last))
  end
  FolderContentsHistory.new(entries)
end

Instance Method Details

#changedObject



78
79
80
# File 'lib/folder_contents_history.rb', line 78

def changed
  @entries.select { |_key, value| value.changed? && value.present? }
end

#conflicts?Boolean

Returns:

  • (Boolean)


89
90
91
92
# File 'lib/folder_contents_history.rb', line 89

def conflicts?
  #changed.map { |entry| entry.last }.compact.uniq.length < changed.length

  @entries.map { |entry| entry.last.last_name }.compact.uniq.length < @entries.length
end

#executeObject



44
45
46
47
48
49
# File 'lib/folder_contents_history.rb', line 44

def execute
  changed.each_pair do |key, value|
    value.rename
    @entries[value.current] = @entries.delete(key)
  end
end

#load_entries(path = '.') ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/folder_contents_history.rb', line 9

def load_entries(path = '.')
  entries = Dir["#{path}/*"].inject({}) 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

#logObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/folder_contents_history.rb', line 51

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



64
65
66
67
68
69
70
# File 'lib/folder_contents_history.rb', line 64

def log_backup(where)
  with_history = @entries.select { |name, history| history.history.length > 1 }
  backup = with_history.inject({}) do |hash, item|
    hash.update(item.last.to_hash)
  end
  File.write(where, JSON.pretty_generate(backup))
end

#plan_reapplication(path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/folder_contents_history.rb', line 31

def plan_reapplication(path)
  entries = Dir["#{path}/*"].map { |file| file.gsub(/#{path}\//,'') }
  histories = @entries.values
  @entries = entries.inject({}) do |hash, item|
    hash.tap do |obj|
      new_history = histories.select { |history| history.was_named? item }.first
      obj.update(item => new_history.clone) unless new_history.nil?
    end
  end
  @entries.select! { |name, history| history }
  @entries.each { |name, history| history.plan_reapplication(name) }
end

#plan_renames(regexp, replacement) ⇒ Object



19
20
21
22
23
# File 'lib/folder_contents_history.rb', line 19

def plan_renames(regexp, replacement)
  @entries.select { |key, value| key[regexp] }.each_pair do |key, value|
    value.plan_rename(regexp, replacement)
  end
end

#plan_rollbacksObject



25
26
27
28
29
# File 'lib/folder_contents_history.rb', line 25

def plan_rollbacks
  @entries.each_value do |file_history|
    file_history.plan_rollback
  end
end

#show_history(files) ⇒ Object



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

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



72
73
74
75
76
# File 'lib/folder_contents_history.rb', line 72

def to_hash(input_hash = @entries)
  input_hash.inject({}) do |hash, entry|
    hash.update(entry[0] => entry[1].to_hash)
  end
end