mediatype_directory 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +19 -11
- data/lib/mediatype_directory/mediatype_directory.rb +33 -3
- data/lib/mediatype_directory/version.rb +1 -1
- data/spec/mediatype_directory_spec.rb +89 -20
- metadata +1 -1
data/README.markdown
CHANGED
@@ -23,26 +23,34 @@ Create a new MediatypeDirectory object, passing in all your configuration option
|
|
23
23
|
require 'mediatype_directory'
|
24
24
|
|
25
25
|
config = {}
|
26
|
-
config[:
|
27
|
-
config[:
|
28
|
-
config[:
|
29
|
-
config[:linktype]
|
30
|
-
config[:test_mode]
|
26
|
+
config[:what] = '.pdf' # OR an array like ['.htm','.html','.shtml'] OR or a special string, 'audio' or 'video'
|
27
|
+
config[:from] = '~/path/to/top-level-dir/where/your/files/are'
|
28
|
+
config[:to] = '~/path/to/dir/where/you/want/to/create/links'
|
29
|
+
config[:linktype] = 'hard' # default: 'soft'
|
30
|
+
config[:test_mode] = true # (or 'true') default: false In test_mode, no directories or files are actually created
|
31
31
|
|
32
32
|
MediatypeDirectory.new(config).create_directory
|
33
33
|
|
34
|
-
|
34
|
+
config[:what] has an alias, config[:extensions]. You can also call "md.extensions = " or "md.what = "
|
35
|
+
|
36
|
+
config[:from] has aliases config[:source] & config[:directory_tree]. You can also call "md.source =", "md.from = " or "md.directory_tree ="
|
37
|
+
|
38
|
+
config[:to] has aliases config[:target] & config[:mediatype_dirname]. You can also call "md.target =", "md.to = " or "md.mediatype_dirname ="
|
39
|
+
|
40
|
+
It's safe to re-run this program as many times as you want. If a link already exists, it will be skipped. But if a new file is found that matches the criteria, a new link will be added.
|
41
|
+
|
42
|
+
If the original content of a hard link changes, the hard link will continue referring to the original version. You can delete the hard link (which should remove the old version from your system) and regenerate a new hardlink, which should point to the new version.
|
35
43
|
|
36
44
|
## EXAMPLE
|
37
45
|
|
38
46
|
require 'mediatype_directory'
|
39
47
|
|
40
48
|
md = MediatypeDirectory.new({
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
linktype: 'hard',
|
45
|
-
test_mode: true
|
49
|
+
what: [".flv",".mov",".mpg",'.mp4'], # List of file extensions for which you wish to generate links
|
50
|
+
from: '/home/jimmy/Tech/Ruby', # Where to look for existing files
|
51
|
+
to: '~/Tech/Docs2/Videos', # Where to store links to existing files
|
52
|
+
linktype: 'hard', # Create hard links, not soft links (a.k.a. symbolic links)
|
53
|
+
test_mode: true # Show what would happen without actually creating directories or files
|
46
54
|
})
|
47
55
|
|
48
56
|
md.create_directory
|
@@ -25,14 +25,28 @@ require 'pathname'
|
|
25
25
|
#
|
26
26
|
class MediatypeDirectory
|
27
27
|
|
28
|
-
|
29
|
-
|
28
|
+
VIDEO_FILE_EXTENSIONS = ['.3g2','.3gp','.asf','.asx','.avi','.bsf','.dat',
|
29
|
+
'.divx','.dvdmedia','.f4v','.fbr','.flv','.hdmov',
|
30
|
+
'.imovieproj','.m2p','.m4v','.mod','.moi','.mov',
|
31
|
+
'.mp4','.mpeg','.mpg','.mts','.ogm','.ogv','.ogx',
|
32
|
+
'.rm','.rmvb','.scm','.srt','.swf','.vob','.vro',
|
33
|
+
'.wmv','.xvid','.yuv']
|
34
|
+
|
35
|
+
AUDIO_FILE_EXTENSIONS = ['.aa','.aa3','.acd','.acm','.aif','.amr','.ape',
|
36
|
+
'.at3','.caf','.dcf','.dss','.emp','.emx','.flac',
|
37
|
+
'.gpx','.iff','.kpl','.m3u','.m3u8','.m4a','.m4b',
|
38
|
+
'.m4p','.mid','.midi','.mod','.mp3','.mpa','.mpga',
|
39
|
+
'.oga','.ogg','.omf','.pcast','.ra','.ram','.snd',
|
40
|
+
'.wav','.wma']
|
41
|
+
|
42
|
+
attr_accessor :extensions, :linktype
|
43
|
+
attr_reader :mediatype_dirname, :directory_tree, :test_mode
|
30
44
|
|
31
45
|
class InvalidDirname < StandardError
|
32
46
|
end
|
33
47
|
|
34
48
|
def initialize(config)
|
35
|
-
self.extensions = config[:extensions]
|
49
|
+
self.extensions = config[:extensions] || config[:what]
|
36
50
|
self.mediatype_dirname = config[:mediatype_dirname] || config[:target] || config[:to]
|
37
51
|
self.directory_tree = config[:directory_tree] || config[:source] || config[:from]
|
38
52
|
self.linktype = config[:linktype] || 'soft'
|
@@ -52,6 +66,22 @@ class MediatypeDirectory
|
|
52
66
|
@directory_tree = nil_or_convert_dirname(dirname)
|
53
67
|
end
|
54
68
|
|
69
|
+
def extensions=(exts)
|
70
|
+
@extensions = if exts =~ /video/i
|
71
|
+
VIDEO_FILE_EXTENSIONS
|
72
|
+
elsif exts =~ /audio/i
|
73
|
+
AUDIO_FILE_EXTENSIONS
|
74
|
+
else
|
75
|
+
Array(exts)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_mode=(val)
|
80
|
+
@test_mode = (val == true || val == 'true')
|
81
|
+
end
|
82
|
+
|
83
|
+
alias_method :what, :extensions
|
84
|
+
alias_method :what=, :extensions=
|
55
85
|
alias_method :source, :directory_tree
|
56
86
|
alias_method :from, :directory_tree
|
57
87
|
alias_method :source=, :directory_tree=
|
@@ -36,7 +36,10 @@ describe MediatypeDirectory do
|
|
36
36
|
FileUtils.mkdir_p('/home/xavier/Tech2/JQuery')
|
37
37
|
FileUtils.mkdir_p('/home/xavier/Tech2/XML')
|
38
38
|
FileUtils.touch('/home/xavier/Tech2/Ruby/ruby.pdf')
|
39
|
+
FileUtils.touch('/home/xavier/Tech2/Ruby/ruby.flv')
|
39
40
|
FileUtils.touch('/home/xavier/Tech2/Ruby/TESTING/ruby_testing.pdf')
|
41
|
+
FileUtils.touch('/home/xavier/Tech2/Ruby/TESTING/testing.mp3')
|
42
|
+
FileUtils.touch('/home/xavier/Tech2/Ruby/TESTING/testing.mp4')
|
40
43
|
FileUtils.touch('/home/xavier/Tech2/JQuery/jquery.pdf')
|
41
44
|
FileUtils.touch('/home/xavier/Tech2/XML/xml.xml')
|
42
45
|
Dir.chdir('/home/xavier/Tech2')
|
@@ -96,11 +99,54 @@ describe MediatypeDirectory do
|
|
96
99
|
let(:config) { { mediatype_dirname: tilde_pdf_dir } }
|
97
100
|
|
98
101
|
it { should be_true }
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
102
|
+
its(:mediatype_dirname) { should == File.expand_path(tilde_pdf_dir) }
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
context "when config sets test_mode as string 'true'" do
|
107
|
+
|
108
|
+
let(:config) { { to: tilde_pdf_dir, from: tilde_dir_tree, test_mode: 'true' } }
|
109
|
+
|
110
|
+
it { should be_true }
|
111
|
+
its(:test_mode) { should be_true }
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
context "when config sets :source and :target" do
|
116
|
+
|
117
|
+
let(:config) { { target: tilde_pdf_dir, source: tilde_dir_tree } }
|
118
|
+
|
119
|
+
it { should be_true }
|
120
|
+
its(:mediatype_dirname) { should == File.expand_path(tilde_pdf_dir) }
|
121
|
+
its(:directory_tree) { should == File.expand_path(tilde_dir_tree) }
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
context "when config sets :from and :to" do
|
126
|
+
|
127
|
+
let(:config) { { to: tilde_pdf_dir, from: tilde_dir_tree } }
|
128
|
+
|
129
|
+
it { should be_true }
|
130
|
+
its(:mediatype_dirname) { should == File.expand_path(tilde_pdf_dir) }
|
131
|
+
its(:directory_tree) { should == File.expand_path(tilde_dir_tree) }
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
context "when config sets :what to 'ViDeO'" do
|
136
|
+
|
137
|
+
let(:config) { { to: tilde_pdf_dir, from: tilde_dir_tree, what: 'ViDeO' } }
|
138
|
+
|
139
|
+
it { should be_true }
|
140
|
+
its(:extensions) { should include('.flv','.mov','.mp4','.ogv','.rm') }
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
context "when config sets :what to 'AuDiO'" do
|
145
|
+
|
146
|
+
let(:config) { { to: tilde_pdf_dir, from: tilde_dir_tree, what: 'AuDiO' } }
|
147
|
+
|
148
|
+
it { should be_true }
|
149
|
+
its(:extensions) { should include('.dss','.flac','.mp3','.ogg','.ra') }
|
104
150
|
|
105
151
|
end
|
106
152
|
|
@@ -132,7 +178,6 @@ describe MediatypeDirectory do
|
|
132
178
|
|
133
179
|
it "should call :check directories and :create_links" do
|
134
180
|
subject.should_receive(:check_directories)
|
135
|
-
Dir.should_receive(:chdir)
|
136
181
|
subject.should_receive(:create_links)
|
137
182
|
subject.create_directory
|
138
183
|
end
|
@@ -148,22 +193,46 @@ describe MediatypeDirectory do
|
|
148
193
|
extensions: ['.pdf'],
|
149
194
|
linktype: 'hard' } }
|
150
195
|
|
151
|
-
|
152
|
-
|
153
|
-
|
196
|
+
context "with test_mode = false" do
|
197
|
+
|
198
|
+
it "should create the correct directory" do
|
199
|
+
subject.create_directory
|
200
|
+
Dir.exists?(xavier_docs_ruby).should be_true
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should create the correct files" do
|
204
|
+
pending "This test fails because FakeFS is broken"
|
205
|
+
subject.create_directory
|
206
|
+
Dir.exists?('/home/xavier/Tech3/Docs/Ruby').should be_true
|
207
|
+
Dir.chdir('/home/xavier/Tech3/Docs/Ruby')
|
208
|
+
Dir.getwd.should == '/home/xavier/Tech3/Docs/Ruby'
|
209
|
+
Dir.glob(File.join("**","*.pdf")).should_not == []
|
210
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf').should be_true
|
211
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby.pdf').should be_true
|
212
|
+
File.exists?('/home/xavier/Tech3/Docs/jquery.pdf').should be_false
|
213
|
+
File.exists?('/home/xavier/Tech3/Docs/xml.xml').should be_false
|
214
|
+
end
|
215
|
+
|
154
216
|
end
|
155
217
|
|
156
|
-
|
157
|
-
|
158
|
-
subject.
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
218
|
+
context "with test_mode = true" do
|
219
|
+
|
220
|
+
before { subject.test_mode = true }
|
221
|
+
|
222
|
+
it "should not create the correct directory" do
|
223
|
+
subject.create_directory
|
224
|
+
Dir.exists?(xavier_docs_ruby).should be_false
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should create the correct files" do
|
228
|
+
pending "This test fails because FakeFS is broken"
|
229
|
+
subject.create_directory
|
230
|
+
Dir.exists?('/home/xavier/Tech3/Docs/Ruby').should be_false
|
231
|
+
Dir.glob(File.join("**","*.pdf")).should_not == []
|
232
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby_testing.pdf').should be_false
|
233
|
+
File.exists?('/home/xavier/Tech3/Docs/Ruby/ruby.pdf').should be_false
|
234
|
+
end
|
235
|
+
|
167
236
|
end
|
168
237
|
|
169
238
|
end
|