Module: Dahistory::Base
- Included in:
- Dahistory
- Defined in:
- lib/Dahistory.rb
Instance Method Summary collapse
- #backup_basename ⇒ Object
- #backup_file(str = :RETURN) ⇒ Object
- #dirs(*raw) ⇒ Object
- #file(path) ⇒ Object
- #git_add_commit ⇒ Object
- #git_add_commit_push(args = "") ⇒ Object
- #git_it(path) ⇒ Object
- #history(raw) ⇒ Object
- #initialize(file_path = nil) {|_self| ... } ⇒ Object
- #on_raise_pending(&blok) ⇒ Object
- #pending_dir(dir) ⇒ Object
- #save ⇒ Object
Instance Method Details
#backup_basename ⇒ Object
85 86 87 |
# File 'lib/Dahistory.rb', line 85 def backup_basename File.basename( backup_file ) end |
#backup_file(str = :RETURN) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/Dahistory.rb', line 89 def backup_file str = :RETURN if str == :RETURN @backup_file ||= "#{`hostname`.strip}-#{file.gsub('/',',')}.#{Time.now.utc.strftime "%Y.%m.%d.%H.%M.%S"}" return File.join( pending_dir, @backup_file ) end @backup_file = str end |
#dirs(*raw) ⇒ Object
37 38 39 |
# File 'lib/Dahistory.rb', line 37 def dirs *raw @dirs = raw.flatten.map { |dir| File. dir } end |
#file(path) ⇒ Object
33 34 35 |
# File 'lib/Dahistory.rb', line 33 def file path @file = File.(path) end |
#git_add_commit ⇒ Object
98 99 100 |
# File 'lib/Dahistory.rb', line 98 def git_add_commit @git = :commit end |
#git_add_commit_push(args = "") ⇒ Object
102 103 104 |
# File 'lib/Dahistory.rb', line 102 def git_add_commit_push args = "" @git = args end |
#git_it(path) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/Dahistory.rb', line 106 def git_it path return false unless @git Exit_0 "git add #{path}" Exit_0 %! git commit -m "Backup: #{backup_file}"! if @git.is_a?(String) Exit_0 %! git push #{@git} ! end end |
#history(raw) ⇒ Object
41 42 43 |
# File 'lib/Dahistory.rb', line 41 def history raw @history = File. raw end |
#initialize(file_path = nil) {|_self| ... } ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/Dahistory.rb', line 20 def initialize file_path = nil @git = false @file = nil file!(file_path) if file_path dirs [] history './history' pending_dir './pending' @on_raise_pending = nil yield(self) if block_given? end |
#on_raise_pending(&blok) ⇒ Object
49 50 51 |
# File 'lib/Dahistory.rb', line 49 def on_raise_pending &blok @on_raise_pending = blok end |
#pending_dir(dir) ⇒ Object
45 46 47 |
# File 'lib/Dahistory.rb', line 45 def pending_dir dir @pending_dir = File.(dir) end |
#save ⇒ Object
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 |
# File 'lib/Dahistory.rb', line 116 def save content = File.read(file) standard = content.gsub("\r", '') in_dirs = self.class.find_file_copy file, dirs in_history = self.class.find_file_copy file, history in_pending = self.class.find_file_copy file, pending_dir return true if in_history if in_dirs path = File.join(history, backup_basename) File.write(path, content) git_it path return true end if !in_pending File.write(backup_file, content) git_it backup_file end # === if !in_pending on_raise_pending.call if on_raise_pending raise Pending, backup_file end |