trash 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/lib/trash.rb +26 -17
  3. data/spec/trash_spec.rb +49 -6
  4. data/trash.gemspec +2 -2
  5. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/lib/trash.rb CHANGED
@@ -2,23 +2,23 @@ require 'fileutils'
2
2
 
3
3
  class Trash
4
4
  attr_reader :errors
5
- attr_accessor :trashcan
5
+ attr_accessor :trash_can
6
6
 
7
7
  def initialize(options = {})
8
- @trashcan = options[:trashcan].nil? ? "#{ENV['HOME']}/.Trash" : options[:trashcan]
9
- create_trashcan_if_absent
8
+ @trash_can = options[:trash_can].nil? ? "#{ENV['HOME']}/.Trash" : options[:trash_can]
9
+ create_trash_can_if_absent
10
10
  @errors = []
11
11
  end
12
12
 
13
- def has_trashcan?
14
- File.directory? @trashcan
13
+ def has_trash_can?
14
+ File.directory? @trash_can
15
15
  end
16
16
 
17
17
  def throw_out(*paths)
18
18
  paths.each do |path|
19
19
  path = File.expand_path(path)
20
20
  if File.exist? path
21
- FileUtils.mv(path, "#{ENV['HOME']}/.Trash/#{unique_file_name(path)}")
21
+ FileUtils.mv(path, "#{@trash_can}/#{unique_file_name(path)}")
22
22
  else
23
23
  add_error "#{path} does not exist. Please check the file path."
24
24
  return 1
@@ -34,20 +34,29 @@ class Trash
34
34
 
35
35
  private
36
36
 
37
- def create_trashcan_if_absent
38
- FileUtils.mkdir_p @trashcan unless has_trashcan?
37
+ def create_trash_can_if_absent
38
+ FileUtils.mkdir_p(@trash_can) unless has_trash_can?
39
39
  end
40
40
 
41
41
  def unique_file_name(path)
42
- path_name = File.basename(path)
43
- path_extension = File.extname(path)
44
- if (File.exists?("#{ENV['HOME']}/.Trash/#{path_name}"))
45
- count = 1
46
- while File.exists?("#{ENV['HOME']}/.Trash/#{path_name.gsub(path_extension, "0#{count}#{path_extension}")}")
47
- count += 1
48
- end
49
- new_path_name = path_name.gsub(path_extension, "0#{count}#{path_extension}")
42
+ file_name = File.split(path).last
43
+ file_extension = File.extname(path)
44
+
45
+ return file_name unless File.exists?("#{@trash_can}/#{file_name}")
46
+
47
+ if File.directory? path
48
+ unique_file_name_finder { |c| "#{file_name}#{"%02d" % c}" }
49
+ else
50
+ unique_file_name_finder { |c| "#{file_name.gsub(file_extension, "#{"%02d" % c}#{file_extension}")}" }
50
51
  end
51
- return new_path_name
52
52
  end
53
+
54
+ def unique_file_name_finder
55
+ count = 1
56
+ while File.exists?("#{@trash_can}/#{yield(count)}")
57
+ count += 1
58
+ end
59
+ return yield(count)
60
+ end
61
+
53
62
  end
data/spec/trash_spec.rb CHANGED
@@ -42,7 +42,7 @@ describe "Trash" do
42
42
  delete_from_trash "testing2.txt"
43
43
  end
44
44
 
45
- it "should handle spaces in the file/folder name" do
45
+ it "should handle spaces in the file/directory name" do
46
46
  create_file = `echo "Test with spaces..." > "/tmp/test with spaces.txt"`
47
47
  Trash.new.throw_out("/tmp/test with spaces.txt")
48
48
  tmp_should_not_contain "test with spaces.txt"
@@ -110,17 +110,60 @@ describe "Trash" do
110
110
  delete_from_trash "testdir01"
111
111
  delete_from_trash "testdir02"
112
112
  end
113
+
114
+ it "handles dots in a directory name" do
115
+ `mkdir -p /tmp/testdir.2010`
116
+ Trash.new.throw_out("/tmp/testdir.2010")
117
+ tmp_should_not_contain "testdir.2010"
118
+ trash_should_contain_directory "testdir.2010"
119
+
120
+ `mkdir -p /tmp/testdir.2010`
121
+ Trash.new.throw_out("/tmp/testdir.2010")
122
+ tmp_should_not_contain "testdir.2010"
123
+ trash_should_contain_directory "testdir.201001"
124
+
125
+ delete_from_trash "testdir.2010"
126
+ delete_from_trash "testdir.201001"
127
+ end
128
+
129
+
130
+ it "appends a number to the directory name if a directory with same name already exisits in trash" do
131
+ `mkdir -p /tmp/testing`
132
+ Trash.new.throw_out("/tmp/testing")
133
+ tmp_should_not_contain "testing"
134
+ trash_should_contain_directory "testing"
135
+
136
+ `mkdir -p /tmp/testing`
137
+ Trash.new.throw_out("/tmp/testing")
138
+ tmp_should_not_contain "testing"
139
+ trash_should_contain_directory "testing01"
140
+
141
+ `mkdir -p /tmp/testing`
142
+ Trash.new.throw_out("/tmp/testing")
143
+ tmp_should_not_contain "testing"
144
+ trash_should_contain_directory "testing02"
145
+
146
+ `mkdir -p /tmp/testing`
147
+ Trash.new.throw_out("/tmp/testing")
148
+ tmp_should_not_contain "testing"
149
+ trash_should_contain_directory "testing03"
150
+
151
+ delete_from_trash "testing"
152
+ delete_from_trash "testing01"
153
+ delete_from_trash "testing02"
154
+ delete_from_trash "testing03"
155
+ end
113
156
 
114
- describe "trashcan" do
115
- it "finds the trashcan" do
157
+ describe "trash_can" do
158
+ it "finds the trash can" do
116
159
  FileUtils.mkdir_p "#{ENV['HOME']}/.Trash"
117
- Trash.new.has_trashcan?.should == true
160
+ Trash.new.has_trash_can?.should == true
118
161
  end
119
162
 
120
- it "creates a trashcan if one does not exist" do
163
+ it "creates a trash can if one does not exist" do
121
164
  delete("/tmp/test_trash_can")
122
165
  File.exist?("/tmp/test_trash_can").should == false
123
- oscar = Trash.new({:trashcan => "/tmp/test_trash_can"})
166
+ oscar = Trash.new({:trash_can => "/tmp/test_trash_can"})
124
167
  File.exist?("/tmp/test_trash_can").should == true
125
168
  File.directory?("/tmp/test_trash_can").should == true
126
169
  delete("/tmp/test_trash_can")
data/trash.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{trash}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Lee Jones"]
12
- s.date = %q{2010-03-25}
12
+ s.date = %q{2010-03-26}
13
13
  s.default_executable = %q{trash}
14
14
  s.description = %q{when its hard to say goodbye, and rm is just too much... use trash instead.}
15
15
  s.email = %q{scribblethink@gmail.com}
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Lee Jones
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-25 00:00:00 -04:00
17
+ date: 2010-03-26 00:00:00 -04:00
18
18
  default_executable: trash
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency