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 CHANGED
@@ -41,6 +41,8 @@ Override the default settings:
41
41
 
42
42
  }
43
43
 
44
- **Note:** Both **def Dahistory** and **class Dahistory** are defined.
45
- All the code is in one file and less than 150 lines: [lib/Dahistory.rb](https://github.com/da99/Dahistory/blob/master/lib/Dahistory.rb)
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
 
@@ -1,3 +1,3 @@
1
1
  class Dahistory
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
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 = :RETURN
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
- def #{name} *args, &blok
53
+ def #{name} *args, &blok
47
54
 
48
- if args.empty?
55
+ if args.empty?
49
56
 
50
- unless instance_variable_defined?(:@#{name})
51
- raise ArgumentError, "Instance variable not set: @#{name}"
52
- end
53
- @#{name}
57
+ unless instance_variable_defined?(:@#{name})
58
+ raise ArgumentError, "Instance variable not set: @#{name}"
59
+ end
60
+ @#{name}
54
61
 
55
- else
62
+ else
56
63
 
57
- #{name}_set(*args, &blok)
64
+ #{name}_set(*args, &blok)
58
65
 
59
- end
66
+ end
60
67
 
61
- end # === def
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 = source_files.detect { |path|
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
+
@@ -14,24 +14,55 @@ describe "Dahistory" do
14
14
 
15
15
  end # === Dahistory
16
16
 
17
- describe "Dahistory: new pending file" do
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 "files/a.txt"
27
+ Dahistory file
29
28
  rescue Dahistory::Pending => e
30
29
  e.message
31
30
  end
32
- File.read(pending).should == target
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: new pending file
67
+ end # === Dahistory: existing file in ./history
37
68
 
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.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: