Dahistory 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -2
- data/lib/Dahistory/version.rb +1 -1
- data/lib/Dahistory.rb +48 -26
- data/spec/tests/Dashistory.rb +36 -5
- metadata +1 -1
data/README.md
CHANGED
@@ -41,6 +41,8 @@ Override the default settings:
|
|
41
41
|
|
42
42
|
}
|
43
43
|
|
44
|
-
**Note:**
|
45
|
-
|
44
|
+
**Note:**
|
45
|
+
Both **def Dahistory** and **class Dahistory** are defined.
|
46
|
+
All the code is in one file and less than 150 lines:
|
47
|
+
[lib/Dahistory.rb](https://github.com/da99/Dahistory/blob/master/lib/Dahistory.rb)
|
46
48
|
|
data/lib/Dahistory/version.rb
CHANGED
data/lib/Dahistory.rb
CHANGED
@@ -13,7 +13,7 @@ end # === def
|
|
13
13
|
class Dahistory
|
14
14
|
|
15
15
|
Pending = Class.new(RuntimeError)
|
16
|
-
|
16
|
+
|
17
17
|
module Base
|
18
18
|
|
19
19
|
def initialize file_path = nil
|
@@ -34,63 +34,60 @@ class Dahistory
|
|
34
34
|
@dirs = args.flatten.map { |dir| File.expand_path dir }
|
35
35
|
end
|
36
36
|
|
37
|
-
def pending_dir dir
|
37
|
+
def pending_dir dir
|
38
38
|
@pending_dir = File.expand_path(dir)
|
39
39
|
end
|
40
40
|
|
41
|
+
#
|
42
|
+
# My alternative to :attr_accessors:
|
43
|
+
#
|
44
|
+
# Previous methods were setters.
|
45
|
+
# Alias the methods as #{name}_set
|
46
|
+
# and create reader/setter methods.
|
47
|
+
#
|
41
48
|
(public_instance_methods - old_meths ).each { |name|
|
42
49
|
|
43
50
|
alias_method :"#{name}_set", name
|
44
51
|
|
45
52
|
eval %~
|
46
|
-
|
53
|
+
def #{name} *args, &blok
|
47
54
|
|
48
|
-
|
55
|
+
if args.empty?
|
49
56
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
57
|
+
unless instance_variable_defined?(:@#{name})
|
58
|
+
raise ArgumentError, "Instance variable not set: @#{name}"
|
59
|
+
end
|
60
|
+
@#{name}
|
54
61
|
|
55
|
-
|
62
|
+
else
|
56
63
|
|
57
|
-
|
64
|
+
#{name}_set(*args, &blok)
|
58
65
|
|
59
|
-
|
66
|
+
end
|
60
67
|
|
61
|
-
|
62
|
-
|
68
|
+
end # === def
|
69
|
+
~
|
63
70
|
|
64
71
|
}
|
65
72
|
|
66
73
|
def backup_file str = :RETURN
|
67
74
|
if str == :RETURN
|
68
75
|
@backup_file ||= "#{`hostname`.strip}-#{file.gsub('/',',')}.#{Time.now.utc.strftime "%Y.%m.%d.%H.%M.%S"}"
|
69
|
-
return @backup_file
|
76
|
+
return File.join( pending_dir, @backup_file )
|
70
77
|
end
|
71
78
|
|
72
79
|
@backup_file = str
|
73
80
|
end
|
74
81
|
|
75
|
-
def source_files
|
76
|
-
dirs.map { |path|
|
77
|
-
full = File.join( File.expand_path(path), "/*")
|
78
|
-
files = Dir.glob( full, File::FNM_DOTMATCH ).select { |unk| File.file?(unk) }
|
79
|
-
}.flatten
|
80
|
-
end
|
81
|
-
|
82
82
|
def save
|
83
83
|
|
84
84
|
content = File.read(file)
|
85
85
|
standard = content.gsub("\r", '')
|
86
86
|
|
87
|
-
old =
|
88
|
-
raw = File.read(path)
|
89
|
-
raw.gsub("\r","") == standard
|
90
|
-
}
|
87
|
+
old = self.class.find_file_copy file, dirs
|
91
88
|
|
92
89
|
if !old
|
93
|
-
File.write(backup_file, content)
|
90
|
+
File.write(backup_file, content) unless self.class.find_file_copy(file, pending_dir)
|
94
91
|
raise Pending, backup_file
|
95
92
|
end
|
96
93
|
|
@@ -99,5 +96,30 @@ class Dahistory
|
|
99
96
|
end # === Base
|
100
97
|
|
101
98
|
include Base
|
99
|
+
|
100
|
+
class << self
|
101
|
+
|
102
|
+
def find_file_copy file, *raw_dirs
|
103
|
+
standard = File.read(file).gsub("\r", "")
|
104
|
+
found = nil
|
105
|
+
dirs = raw_dirs.flatten
|
106
|
+
|
107
|
+
dirs.each { |path|
|
108
|
+
full = File.join( File.expand_path(path), "/*")
|
109
|
+
files = Dir.glob( full, File::FNM_DOTMATCH ).select { |unk| File.file?(unk) }
|
110
|
+
|
111
|
+
found = files.detect { |f|
|
112
|
+
raw = File.read(f)
|
113
|
+
raw.gsub("\r", "") == standard
|
114
|
+
}
|
115
|
+
|
116
|
+
break if found
|
117
|
+
}
|
118
|
+
|
119
|
+
found
|
120
|
+
end # === def
|
121
|
+
|
122
|
+
end # === class
|
102
123
|
|
103
124
|
end # === class Dahistory
|
125
|
+
|
data/spec/tests/Dashistory.rb
CHANGED
@@ -14,24 +14,55 @@ describe "Dahistory" do
|
|
14
14
|
|
15
15
|
end # === Dahistory
|
16
16
|
|
17
|
-
describe "Dahistory:
|
17
|
+
describe "Dahistory: pending file" do
|
18
18
|
|
19
19
|
before { reset_dirs }
|
20
20
|
|
21
21
|
it "copies file to pending dir" do
|
22
|
-
|
23
22
|
file = "files/a.txt"
|
24
23
|
target = rand(1000).to_s
|
25
24
|
chdir {
|
26
25
|
File.write(file, target)
|
27
26
|
pending = begin
|
28
|
-
Dahistory
|
27
|
+
Dahistory file
|
29
28
|
rescue Dahistory::Pending => e
|
30
29
|
e.message
|
31
30
|
end
|
32
|
-
File.
|
31
|
+
name = File.basename(pending)
|
32
|
+
File.read(File.join "pending", name).should == target
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
it "does not copy file if file is already in ./pending" do
|
37
|
+
file = "files/#{rand 1000}.txt"
|
38
|
+
target = rand(10000).to_s
|
39
|
+
chdir {
|
40
|
+
File.write(file, target)
|
41
|
+
File.write(file.sub('files', 'pending'), target)
|
42
|
+
begin
|
43
|
+
Dahistory file
|
44
|
+
rescue Dahistory::Pending => e
|
45
|
+
end
|
46
|
+
Dir.glob("pending/*").size.should == 1
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
end # === Dahistory: pending file
|
51
|
+
|
52
|
+
describe "Dahistory: existing file in ./history" do
|
53
|
+
|
54
|
+
before { reset_dirs }
|
55
|
+
|
56
|
+
it "does not raise any error" do
|
57
|
+
file = "files/#{rand 10000}.txt"
|
58
|
+
target = rand(10000).to_s
|
59
|
+
chdir {
|
60
|
+
File.write file, target
|
61
|
+
File.write file.sub("files", "history"), target
|
62
|
+
should.not.raise { Dahistory file }
|
33
63
|
}
|
64
|
+
|
34
65
|
end
|
35
66
|
|
36
|
-
end # === Dahistory:
|
67
|
+
end # === Dahistory: existing file in ./history
|
37
68
|
|