Module: Dahistory::Base

Included in:
Dahistory
Defined in:
lib/Dahistory.rb

Instance Method Summary collapse

Instance Method Details

#backup_file(str = :RETURN) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/Dahistory.rb', line 80

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(*args) ⇒ Object



36
37
38
# File 'lib/Dahistory.rb', line 36

def dirs *args
  @dirs = args.flatten.map { |dir| File.expand_path dir }
end

#file(path) ⇒ Object



32
33
34
# File 'lib/Dahistory.rb', line 32

def file path
  @file = File.expand_path(path)
end

#git_add_commitObject



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

def git_add_commit
  @git = :commit
end

#git_add_commit_push(args = "") ⇒ Object



93
94
95
# File 'lib/Dahistory.rb', line 93

def git_add_commit_push args = ""
  @git = args
end

#initialize(file_path = nil) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
24
25
26
27
28
# File 'lib/Dahistory.rb', line 20

def initialize file_path = nil
  @git = false
  @file = nil
  file!(file_path) if file_path
  dirs         './history'
  pending_dir  './pending'
  @on_raise_pending = nil
  yield(self) if block_given?
end

#on_raise_pending(&blok) ⇒ Object



44
45
46
# File 'lib/Dahistory.rb', line 44

def on_raise_pending &blok
  @on_raise_pending = blok
end

#pending_dir(dir) ⇒ Object



40
41
42
# File 'lib/Dahistory.rb', line 40

def pending_dir dir 
  @pending_dir = File.expand_path(dir)
end

#saveObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/Dahistory.rb', line 97

def save 

  content  = File.read(file)
  standard = content.gsub("\r", '')

  old = self.class.find_file_copy file, dirs
  in_pending = self.class.find_file_copy file, pending_dir

  if !old 
    
    if !in_pending
      
      File.write(backup_file, content) 

      if @git
        Exit_Zero "git add #{backup_file}"
        Exit_Zero %! git commit -m "Backup: #{backup_file}"!
      end

      if @git.is_a?(String)
        Exit_Zero %! git push #{@git} !
      end
      
    end # === if !in_pending
    
    on_raise_pending.call if on_raise_pending
    raise Pending, backup_file
  end

end