file_discard 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/file_discard.rb +139 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3b38d7bdbec2c063fabda64c3d04131b3c63e5d8
4
+ data.tar.gz: bae85f1bdf78a07e9c2cb7ec56662a3677507b20
5
+ SHA512:
6
+ metadata.gz: 5fb8dfe12a6a995b00ab7cbac31ef85b1345c7d05bd6e13fc96f01a3defbeaf4c67a93ed7d5ddc2d60561416ade98f115c5a3ea9b27d91cb9ae25445c74e7d13
7
+ data.tar.gz: 4d6d3acb2213a9b2b1d7bdbca853da477cef1569178e57c53427a4683a492df4e0eda90d8e6b60d10e86e22306b6530314b5761bd47b22d38cbfa5ceab1540cf
@@ -0,0 +1,139 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014 Brad Robel-Forrest
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require 'pathname'
24
+ require 'fileutils'
25
+
26
+ module FileDiscard
27
+
28
+ VERSION = '0.0.1'
29
+
30
+ ######################################################################
31
+ # Module Methods
32
+
33
+ def self.mix_it_in!
34
+ [File, Pathname].each do |c|
35
+ c.class_eval do
36
+ def self.discard(*args)
37
+ FileDiscard.discarder.discard(*args)
38
+ end
39
+ def discard(options = {})
40
+ FileDiscard.discarder.discard(self, options)
41
+ end
42
+ end
43
+ end
44
+ self
45
+ end
46
+
47
+ def self.discard(*args)
48
+ discarder.discard(*args)
49
+ end
50
+
51
+ def self.discarder=(discarder)
52
+ @@discarder = discarder
53
+ end
54
+
55
+ def self.discarder
56
+ @@discarder ||= case RUBY_PLATFORM
57
+ when /darwin/i then OsxDiscarder.new
58
+ when /linux/i then LinuxDiscarder.new
59
+ else
60
+ raise NotImplementedError
61
+ .new("Unsupported platform: #{RUBY_PLATFORM}")
62
+ end
63
+ end
64
+
65
+ ######################################################################
66
+ # Discarders
67
+
68
+ class Discarder
69
+ def initialize(home_trash, mountpoint_trash_fmt)
70
+ home = Pathname.new('~').expand_path
71
+ @home_trash = home.join(home_trash)
72
+ @home_mountpoint = mountpoint_of home
73
+ @mountpoint_trash_fmt = mountpoint_trash_fmt
74
+ end
75
+
76
+ def discard(obj, move_options = {})
77
+ pn = pathname_for obj
78
+ trash = find_trash_for pn
79
+ raise Errno::ENOENT.new(trash.to_s) unless trash.exist?
80
+ dst = uniquify(trash.join(pn.basename))
81
+ FileUtils.mv pn.expand_path, dst, move_options
82
+ end
83
+
84
+ private
85
+ def mountpoint_of(pn)
86
+ pn = pn.parent until pn.mountpoint?
87
+ pn
88
+ end
89
+
90
+ def find_trash_for(pn)
91
+ pd = pn.expand_path.realpath.dirname
92
+ mp = mountpoint_of pd
93
+ return @home_trash if mp == @home_mountpoint
94
+ mp.join(@mountpoint_trash_fmt % Process.uid)
95
+ end
96
+
97
+ def pathname_for(obj)
98
+ if obj.is_a? Pathname
99
+ obj
100
+ elsif obj.respond_to? :to_path
101
+ Pathname.new(obj.to_path)
102
+ else
103
+ Pathname.new(obj)
104
+ end
105
+ end
106
+
107
+ def uniquify(pn)
108
+ return pn unless pn.exist?
109
+
110
+ ext = pn.extname
111
+ base = pn.basename(ext).to_s
112
+ dn = pn.dirname
113
+
114
+ count = 0
115
+ fmt = bfmt = '%H.%M.%S'
116
+
117
+ loop do
118
+ ts = Time.now.strftime(fmt)
119
+ pn = dn.join("#{base} #{ts}#{ext}")
120
+ return pn unless pn.exist?
121
+ fmt = bfmt + ".%#{count}N" # use fractional seconds, with increasing precision
122
+ count += 1
123
+ end
124
+ end
125
+ end # class Discarder
126
+
127
+ class OsxDiscarder < Discarder
128
+ def initialize
129
+ super '.Trash', '.Trashes/%s'
130
+ end
131
+ end
132
+
133
+ class LinuxDiscarder < Discarder
134
+ def initialize
135
+ super '.local/share/Trash', '.Trash-%s'
136
+ end
137
+ end
138
+
139
+ end # module FileDiscard
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_discard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brad Robel-Forrest
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple helper to move files to the trash on OS X or Linux platforms
14
+ email: brad+filediscard@gigglewax.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/file_discard.rb
20
+ homepage: https://github.com/bradrf/file_discard#readme
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Move files to the trash
44
+ test_files: []