Dahistory 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,30 +10,36 @@ It is mainly to help you keep backups of files you overwrite on servers you admi
10
10
  **Example:** You use Chef or Puppet to write a config file, but you want to be sure you
11
11
  are overwriting a file you have encountered before.
12
12
 
13
+ Installation
14
+ ------------
15
+
16
+ gem 'Dahistory'
17
+
13
18
  Useage
14
19
  ------
15
20
 
16
21
  require "Dahistory"
17
22
 
18
23
  path = "some/file.txt"
19
- Dahistory_File( path )
20
- # Checks your ./history directory.
24
+
25
+ Dahistory path
26
+
27
+ # Checks your directory (default "./history").
21
28
  # If not found there, saves copy of file in ./pending dir and
22
29
  # raises Dahistory::Pending_File, "pending/HOSTNAME,path,some,file.txt.TIMESTAMP"
23
30
 
24
31
  # You review the file,
25
- # move the file from the pending directory, and
32
+ # move the file from the pending directory to your source/history dir, and
26
33
  # re-do your last command (Capistrano, Chef, Puppet, etc.)
27
34
 
28
35
  Override the default settings:
29
36
 
30
- Dahistory.check { |o|
37
+ Dahistory { |o|
31
38
 
32
- o.file "file/path.txt"
33
- o.dirs [ "dir1", "dir2" ]
34
- o.backup_dir "./history"
35
- o.pending_dir "pending_dir_path"
36
- o.backup_file "#{`hostname`}.backup.path.txt"
39
+ o.file "file/name.txt"
40
+ o.dirs "dir1", "dir2" # defaults to "./history"
41
+ o.pending_dir "./pending"
42
+ o.backup_file "#{`hostname`}.name.txt.#{Time.now.to_i}"
37
43
 
38
44
  }
39
45
 
@@ -1,3 +1,3 @@
1
1
  class Dahistory
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/Dahistory.rb CHANGED
@@ -1,108 +1,102 @@
1
1
  require 'Dahistory/version'
2
2
 
3
+ def Dahistory file = nil
4
+ da = Dahistory.new { |o|
5
+ o.file(file) if file
6
+ yield(o) if block_given?
7
+ }
8
+
9
+ da.save
10
+ end # === def
3
11
 
4
12
  class Dahistory
5
13
 
6
14
  Pending = Class.new(RuntimeError)
7
15
 
8
- module Class_Methods
9
-
10
- def check file = nil
11
- da = Dahistory.new { |o|
12
- o.file(file) if file
13
- yield(o) if block_given?
14
- }
15
-
16
- da.save
16
+ module Base
17
+
18
+ def initialize file_path = nil
19
+ @file = nil
20
+ file!(file_path) if file_path
21
+ dirs './history'
22
+ pending_dir './pending'
23
+ yield(self) if block_given?
17
24
  end
18
25
 
19
- end # === module
26
+ old_meths = public_instance_methods
20
27
 
21
- extend Class_Methods
22
-
23
- def initialize file_path = nil
24
- @file = nil
25
- file!(file_path) if file_path
26
- dirs ['.']
27
- pending_dir "./pending"
28
- @backup_dir = nil
29
- yield(self) if block_given?
30
- end
31
-
32
- old_meths = public_instance_methods
33
-
34
- def file path
35
- @file = File.expand_path(path)
36
- end
28
+ def file path
29
+ @file = File.expand_path(path)
30
+ end
37
31
 
38
- def dirs *args
39
- @dirs = args.flatten.map { |dir| File.expand_path dir }
40
- end
32
+ def dirs *args
33
+ @dirs = args.flatten.map { |dir| File.expand_path dir }
34
+ end
41
35
 
42
- def backup_dir dir
43
- @backup_dir = File.expand_path(dir)
44
- end
36
+ def pending_dir dir = :RETURN
37
+ @pending_dir = File.expand_path(dir)
38
+ end
45
39
 
46
- def pending_dir dir = :RETURN
47
- @pending_dir = File.expand_path(dir)
48
- end
40
+ (public_instance_methods - old_meths ).each { |name|
49
41
 
50
- (public_instance_methods - old_meths ).each { |name|
51
-
52
- alias_method :"#{name}_set", name
42
+ alias_method :"#{name}_set", name
53
43
 
54
- eval %~
44
+ eval %~
55
45
  def #{name} *args, &blok
56
-
46
+
57
47
  if args.empty?
58
-
48
+
59
49
  unless instance_variable_defined?(:@#{name})
60
50
  raise ArgumentError, "Instance variable not set: @#{name}"
61
51
  end
62
52
  @#{name}
63
-
53
+
64
54
  else
65
-
55
+
66
56
  #{name}_set(*args, &blok)
67
- #
57
+
68
58
  end
69
-
70
- end # === def
71
- ~
72
-
73
- }
74
-
75
- def backup_file str = :RETURN
76
- if str == :RETURN
77
- @backup_file ||= "#{`hostname`.strip}-#{file.gsub('/',',')}.#{Time.now.utc.strftime "%Y.%m.%d.%H.%M.%S"}"
78
- return @backup_file
79
- end
80
-
81
- @backup_file = str
82
- end
83
-
84
- def source_files
85
- dirs.map { |path|
86
- full = File.join( File.expand_path(path), "/*")
87
- files = Dir.glob( full, File::FNM_DOTMATCH ).select { |unk| File.file?(unk) }
88
- }.flatten
89
- end
90
-
91
- def save
92
59
 
93
- content = File.read(file)
94
- standard = content.gsub("\r", '')
60
+ end # === def
61
+ ~
95
62
 
96
- old = source_files.detect { |path|
97
- raw = File.read(path)
98
- raw.gsub("\r","") == standard
99
63
  }
100
64
 
101
- if !old
102
- File.write(backup_file, content)
103
- raise Pending, backup_file
65
+ def backup_file str = :RETURN
66
+ if str == :RETURN
67
+ @backup_file ||= "#{`hostname`.strip}-#{file.gsub('/',',')}.#{Time.now.utc.strftime "%Y.%m.%d.%H.%M.%S"}"
68
+ return @backup_file
69
+ end
70
+
71
+ @backup_file = str
104
72
  end
73
+
74
+ def source_files
75
+ dirs.map { |path|
76
+ full = File.join( File.expand_path(path), "/*")
77
+ files = Dir.glob( full, File::FNM_DOTMATCH ).select { |unk| File.file?(unk) }
78
+ }.flatten
79
+ end
80
+
81
+ def save
82
+
83
+ content = File.read(file)
84
+ standard = content.gsub("\r", '')
85
+
86
+ old = source_files.detect { |path|
87
+ raw = File.read(path)
88
+ raw.gsub("\r","") == standard
89
+ }
90
+
91
+ if !old
92
+ File.write(backup_file, content)
93
+ raise Pending, backup_file
94
+ end
95
+
96
+ end # === def
105
97
 
106
- end # === def
98
+ end # === Base
107
99
 
100
+ include Base
101
+
108
102
  end # === class Dahistory
@@ -10,7 +10,7 @@ describe "Dahistory: new pending file" do
10
10
  chdir {
11
11
  File.write(file, target)
12
12
  pending = begin
13
- Dahistory.check("files/a.txt")
13
+ Dahistory "files/a.txt"
14
14
  rescue Dahistory::Pending => e
15
15
  e.message
16
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Dahistory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: