filename 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -0,0 +1,132 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/filename")
4
+ require 'optparse'
5
+
6
+ basename = File.basename(__FILE__)
7
+ help_message =<<HELP
8
+ Usage: #{basename} <command> [arguments ...] [options ...]
9
+
10
+ <command> is 'new', 'cache', 'list_cache', 'delete_cache', 'config', 'list_config', or 'init'.
11
+ #{basename} new <basename> [options ...]
12
+ #{basename} cache <cache_key> [options ...]
13
+ #{basename} list_cache
14
+ #{basename} delete_cache <cache_key> [...]
15
+ #{basename} config <config_key> [options ...]
16
+ #{basename} list_config
17
+ #{basename} init
18
+
19
+ HELP
20
+
21
+ Version = '0.0.4'
22
+
23
+ options = {}
24
+ create_cache = false
25
+
26
+ begin
27
+ OptionParser.new(help_message) do |opt|
28
+ opt.on('-s NUM', '--start NUM', Integer, 'Set the starting number.') do |v|
29
+ options[:start] = v
30
+ end
31
+ opt.on('-i NUM', '--digit NUM', Integer, 'Set the digit of number.') do |v|
32
+ options[:digit] = v
33
+ end
34
+ opt.on('-d STR', '--delimiter STR', String, 'Set the delimiter string: number or time.') do |v|
35
+ options[:delimiter] = v
36
+ end
37
+ opt.on('-t TYPE', '--type TYPE', String, 'Set the type of additional part.') do |v|
38
+ options[:type] = v.intern
39
+ end
40
+ opt.on('-f STR', '--format STR', String, 'Set the format string.') do |v|
41
+ options[:format] = v
42
+ end
43
+ opt.on('-p POS', '--position POS', String, 'Set the position of addition: prefix, suffix, or middle.') do |v|
44
+ options[:position] = v.intern
45
+ end
46
+ opt.on('-P TYPE', '--path TYPE', String, 'Set the type of path: absolute or relative.') do |v|
47
+ options[:type] = v.intern
48
+ end
49
+ opt.on('-e STR', '--extension STR', String, 'Set the extension string.') do |v|
50
+ options[:extension] = v
51
+ end
52
+ opt.on('-a STR', '--add STR', String, 'Change the behavior of addition: always, auto, or prohibit.') do |v|
53
+ options[:add] = v.intern
54
+ end
55
+ opt.on('-D', '--directory', 'Create parant directory.') do |v|
56
+ options[:directory] = true
57
+ end
58
+ opt.on('-c KEY', '--cache KEY', String, 'Create cache for command "new".') do |v|
59
+ create_cache = v
60
+ end
61
+ opt.parse!(ARGV)
62
+ end
63
+
64
+ raise OptionParser::InvalidArgument if ARGV.size == 0
65
+
66
+ cmd = ARGV[0]
67
+ args = ARGV[1..-1]
68
+ manage = FileName.manage
69
+
70
+ case cmd
71
+ when 'new'
72
+ raise OptionParser::InvalidArgument if args.size == 0
73
+ fname = FileName.new(*args, options)
74
+ if create_cache
75
+ manage.save_cache(create_cache, fname)
76
+ else
77
+ $stdout.puts fname.create
78
+ end
79
+ exit(0)
80
+ when 'cache'
81
+ raise OptionParser::InvalidArgument if args.size != 1
82
+ key = args[0]
83
+ if fname = manage.load_cache(key)
84
+ $stdout.puts fname.create
85
+ manage.save_cache(key, fname)
86
+ exit(0)
87
+ end
88
+ exit(1)
89
+ when 'list_cache'
90
+ manage.list_cache.each do |name|
91
+ $stdout.puts name
92
+ end
93
+ exit(0)
94
+ when 'delete_cache'
95
+ raise OptionParser::InvalidArgument if args.size == 0
96
+ args.each do |key|
97
+ manage.delete_cache(key)
98
+ end
99
+ exit(0)
100
+ when 'config'
101
+ raise OptionParser::InvalidArgument if args.size <= 1
102
+ if fname = FileName.configuration(*args)
103
+ if create_cache
104
+ manage.save_cache(create_cache, fname)
105
+ else
106
+ $stdout.puts fname.create(options)
107
+ end
108
+ exit(0)
109
+ end
110
+ exit(1)
111
+ when 'list_config'
112
+ manage.list_configuration.each do |name|
113
+ $stdout.puts name
114
+ end
115
+ exit(0)
116
+ when 'init'
117
+ manage.save_configuration_example
118
+ exit(0)
119
+ end
120
+ rescue OptionParser::InvalidOption
121
+ $stderr.print <<MES
122
+ error: Invalid Option
123
+ #{help_message}
124
+ MES
125
+ exit(2)
126
+ rescue OptionParser::InvalidArgument
127
+ $stderr.print <<MES
128
+ error: Invalid Argument
129
+ #{help_message}
130
+ MES
131
+ exit(2)
132
+ end
@@ -5,13 +5,14 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{filename}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Takayuki YAMAGUCHI"]
12
- s.date = %q{2011-03-29}
12
+ s.date = %q{2011-04-02}
13
13
  s.description = %q{Create filename with sequential number or time string that is not duplicated.}
14
14
  s.email = %q{d@ytak.info}
15
+ s.executables = ["filename-create"]
15
16
  s.extra_rdoc_files = [
16
17
  "LICENSE.txt",
17
18
  "README.md"
@@ -25,17 +26,22 @@ Gem::Specification.new do |s|
25
26
  "README.md",
26
27
  "Rakefile",
27
28
  "VERSION",
29
+ "bin/filename-create",
28
30
  "filename.gemspec",
29
31
  "lib/filename.rb",
32
+ "spec/cache_spec.rb",
33
+ "spec/configuration_spec.rb",
30
34
  "spec/filename_spec.rb",
31
35
  "spec/spec_helper.rb"
32
36
  ]
33
37
  s.homepage = %q{http://github.com/ytaka/filename}
34
38
  s.licenses = ["GPLv3"]
35
39
  s.require_paths = ["lib"]
36
- s.rubygems_version = %q{1.5.2}
40
+ s.rubygems_version = %q{1.7.1}
37
41
  s.summary = %q{Filename generator}
38
42
  s.test_files = [
43
+ "spec/cache_spec.rb",
44
+ "spec/configuration_spec.rb",
39
45
  "spec/filename_spec.rb",
40
46
  "spec/spec_helper.rb"
41
47
  ]
@@ -47,11 +47,17 @@ class FileName
47
47
  #
48
48
  # [:directory]
49
49
  # Default value of the option of FileName#create.
50
- def initialize(basepath, opts = {})
50
+ def initialize(basepath, *rest)
51
+ if Hash === rest[-1]
52
+ opts = rest.delete_at(-1)
53
+ else
54
+ opts = {}
55
+ end
56
+ path = File.join(basepath, *rest)
51
57
  if opts[:path] == :relative
52
- @basepath = basepath
58
+ @basepath = path
53
59
  else
54
- @basepath = File.expand_path(basepath)
60
+ @basepath = File.expand_path(path)
55
61
  end
56
62
  @number = opts[:start] || 0
57
63
  @digit = opts[:digit] || 2
@@ -186,10 +192,122 @@ class FileName
186
192
  end
187
193
  end
188
194
 
195
+ # If @format is a Proc object, we can not dump a FileName object.
196
+ def dump
197
+ Marshal.dump(self)
198
+ end
199
+
200
+ def save_to(path)
201
+ open(path, 'w') do |f|
202
+ f.print dump
203
+ end
204
+ end
205
+
206
+ def self.load(str)
207
+ Marshal.load(str)
208
+ end
209
+
210
+ def self.load_from(path)
211
+ self.load(File.read(path))
212
+ end
213
+
189
214
  # Executing FileName.new and FileName.create, we get new filename.
190
215
  # The same options of FileName.new are available.
191
- def self.create(basepath, opts = {})
192
- self.new(basepath, opts).create
216
+ def self.create(basepath, *rest)
217
+ self.new(basepath, *rest).create
218
+ end
219
+
220
+ @@manage = nil
221
+
222
+ def self.manage
223
+ @@manage = FileName::Manage.new unless @@manage
224
+ @@manage
225
+ end
226
+
227
+ def self.configuration(*args)
228
+ self.manage.configuration(*args)
229
+ end
230
+
231
+ class Manage
232
+ @@filename_directory = File.join(ENV['HOME'], '.filename_gem')
233
+ @@configuration = nil
234
+
235
+ CONF_DIRECTORY = 'conf'
236
+ CACHE_DIRECTORY = 'cache'
237
+ SAMPLE_CONF_NAME = 'process.rb.example'
238
+
239
+ def configuration_directory
240
+ File.join(@@filename_directory, CONF_DIRECTORY)
241
+ end
242
+
243
+ def cache_directory(*file)
244
+ File.join(@@filename_directory, CACHE_DIRECTORY, *file)
245
+ end
246
+
247
+ def load_configuration
248
+ @@configuration = {}
249
+ # old_safe_level = $SAFE
250
+ # $SAFE = 2
251
+ Dir.glob(configuration_directory + '/*.rb') do |path|
252
+ if key = path.match(/\/([^\/]*)\.rb$/)[1]
253
+ @@configuration[key.intern] = eval(File.read(path))
254
+ end
255
+ end
256
+ # $SAFE = old_safe_level
257
+ end
258
+
259
+ def configuration(key, basepath, *rest)
260
+ load_configuration unless @@configuration
261
+ if opts = @@configuration[key.intern]
262
+ return FileName.new(basepath, *rest, opts)
263
+ end
264
+ return nil
265
+ end
266
+
267
+ def list_configuration
268
+ Dir.glob(configuration_directory + "/*.rb").map { |s| File.basename(s).sub(/\.rb$/, '') }.sort
269
+ end
270
+
271
+ def save_cache(key, filename)
272
+ dir = cache_directory
273
+ FileUtils.mkdir_p(dir)
274
+ filename.save_to(File.join(dir, key))
275
+ end
276
+
277
+ def load_cache(key)
278
+ path = cache_directory(key)
279
+ if File.exist?(path)
280
+ return FileName.load_from(path)
281
+ end
282
+ return nil
283
+ end
284
+
285
+ def delete_cache(key)
286
+ path = cache_directory(key)
287
+ if File.exist?(path)
288
+ FileUtils.rm(path)
289
+ end
290
+ end
291
+
292
+ def list_cache
293
+ Dir.glob(cache_directory + "/*").map { |s| File.basename(s) }.sort
294
+ end
295
+
296
+ def save_configuration_example
297
+ dir = configuration_directory
298
+ FileUtils.mkdir_p(dir)
299
+ open(File.join(dir, SAMPLE_CONF_NAME), 'w') do |f|
300
+ f.print <<SAMPLE
301
+ {
302
+ :type => :number,
303
+ :position => :middle,
304
+ :path => :relative,
305
+ :format => lambda { |n| sprintf("%05d_%02d", Process.pid, n) }
306
+ }
307
+ SAMPLE
308
+ end
309
+ end
310
+
193
311
  end
194
312
 
195
313
  end
@@ -0,0 +1,14 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe FileName do
4
+ before(:all) do
5
+ @filename = FileName.new('abc', 'def.txt')
6
+ end
7
+
8
+ it "should dump and load" do
9
+ s = @filename.dump
10
+ s.should be_an_instance_of String
11
+ FileName.load(s).should be_an_instance_of FileName
12
+ end
13
+
14
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe FileName do
4
+ context "when saving example of configuration" do
5
+ before(:all) do
6
+ @dir = File.join(File.dirname(__FILE__), 'conf.tmp')
7
+ FileName::Manage.class_variable_set(:@@filename_directory, @dir)
8
+ end
9
+
10
+ it "should create directory" do
11
+ FileName::Manage.new.save_configuration_example
12
+ File.exist?(@dir).should be_true
13
+ conf_dir = File.join(@dir, FileName::Manage::CONF_DIRECTORY)
14
+ File.exist?(conf_dir).should be_true
15
+ Dir.entries(conf_dir).should include(FileName::Manage::SAMPLE_CONF_NAME)
16
+ end
17
+
18
+ after(:all) do
19
+ FileUtils.rm_r(@dir) if File.exist?(@dir)
20
+ end
21
+ end
22
+
23
+ context "when loading configuration" do
24
+ before(:all) do
25
+ @dir = File.join(File.dirname(__FILE__), 'conf.tmp')
26
+ FileName::Manage.class_variable_set(:@@filename_directory, @dir)
27
+ FileName::Manage.new.save_configuration_example
28
+ path = File.join(@dir, FileName::Manage::CONF_DIRECTORY, FileName::Manage::SAMPLE_CONF_NAME)
29
+ FileUtils.mv(path, path.sub(/\.example$/, ''))
30
+ @conf = File.basename(path.sub(/\.rb\.example$/, ''))
31
+ end
32
+
33
+ it "should return FileName" do
34
+ FileName.configuration(@conf, 'base.path').should be_an_instance_of(FileName)
35
+ end
36
+
37
+ it "should return nil" do
38
+ FileName.configuration(:not_exist, 'base.path').should be_nil
39
+ end
40
+
41
+ after(:all) do
42
+ FileUtils.rm_r(@dir) if File.exist?(@dir)
43
+ end
44
+ end
45
+ end
@@ -129,6 +129,24 @@ describe FileName do
129
129
  check_create_directory(filename, basename, path)
130
130
  end
131
131
 
132
+ it "should conbine a few arguments" do
133
+ filename = FileName.new('abc', 'def', 'ghi')
134
+ filename.create.should == File.expand_path('abc/def/ghi')
135
+ end
136
+
137
+ it "should conbine a few arguments with hash option" do
138
+ filename = FileName.new('abc', 'def', 'ghi', :path => :relative, :add => :prohibit)
139
+ filename.create.should == 'abc/def/ghi'
140
+ end
141
+
142
+ it "should conbine a few arguments on FileName#create" do
143
+ FileName.create('abc', 'def', 'ghi').should == File.expand_path('abc/def/ghi')
144
+ end
145
+
146
+ it "should conbine a few arguments on FileName#create with hash option" do
147
+ FileName.create('abc', 'def', 'ghi', :path => :relative, :add => :prohibit).should == 'abc/def/ghi'
148
+ end
149
+
132
150
  context "when we set the default options of FileName#create" do
133
151
  NUMBER_TEST_REPEAT = 3
134
152
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: filename
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Takayuki YAMAGUCHI
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-29 00:00:00 +09:00
14
- default_executable:
13
+ date: 2011-04-02 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: rspec
@@ -59,8 +58,8 @@ dependencies:
59
58
  version_requirements: *id004
60
59
  description: Create filename with sequential number or time string that is not duplicated.
61
60
  email: d@ytak.info
62
- executables: []
63
-
61
+ executables:
62
+ - filename-create
64
63
  extensions: []
65
64
 
66
65
  extra_rdoc_files:
@@ -75,11 +74,13 @@ files:
75
74
  - README.md
76
75
  - Rakefile
77
76
  - VERSION
77
+ - bin/filename-create
78
78
  - filename.gemspec
79
79
  - lib/filename.rb
80
+ - spec/cache_spec.rb
81
+ - spec/configuration_spec.rb
80
82
  - spec/filename_spec.rb
81
83
  - spec/spec_helper.rb
82
- has_rdoc: true
83
84
  homepage: http://github.com/ytaka/filename
84
85
  licenses:
85
86
  - GPLv3
@@ -93,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
94
  requirements:
94
95
  - - ">="
95
96
  - !ruby/object:Gem::Version
96
- hash: 1626148105231843710
97
+ hash: -4477579115920714388
97
98
  segments:
98
99
  - 0
99
100
  version: "0"
@@ -106,10 +107,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
107
  requirements: []
107
108
 
108
109
  rubyforge_project:
109
- rubygems_version: 1.5.2
110
+ rubygems_version: 1.7.1
110
111
  signing_key:
111
112
  specification_version: 3
112
113
  summary: Filename generator
113
114
  test_files:
115
+ - spec/cache_spec.rb
116
+ - spec/configuration_spec.rb
114
117
  - spec/filename_spec.rb
115
118
  - spec/spec_helper.rb