filename 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,21 +1,31 @@
1
1
  # filename
2
2
 
3
- This gem includs the class FileName to create filename that is not duplicated
4
- and command 'filename-create' for shell.
5
- We select type of additional part of filename: number or time.
3
+ The gem 'filename' is used to create filenames that have sequential numbers,
4
+ time strings, or strings of other formats not to be duplicated.
5
+ This includes the class FileName and command 'filename-create' for shell.
6
+
7
+ * [http://rubygems.org/gems/filename](http://rubygems.org/gems/filename)
8
+ * [http://rubydoc.info/gems/filename/](http://rubydoc.info/gems/filename/)
6
9
 
7
10
  ## Installation
8
11
 
12
+ We can install gem command.
13
+
9
14
  gem install filename
10
15
 
16
+ ## Requirements
17
+
18
+ filename is tested on ruby 1.9.2.
19
+ For arguments with variable length errors occur on ruby 1.8.7.
20
+
11
21
  ## How to use as library
12
22
 
13
23
  We need to require 'filename'.
14
24
 
15
25
  require 'filename'
16
26
 
17
- And then we create method for an instance of FileName or
18
- class method FileName.create.
27
+ And then we call 'create' method for an instance of FileName or
28
+ class method 'FileName.create'.
19
29
 
20
30
  ### Filenames with sequential numbers
21
31
 
@@ -28,12 +38,25 @@ Default options add suffixes and use additional parts of sequential numbers.
28
38
  p filename.create(:add => :always) # => "/path/to/base.txt.01"
29
39
  p filename.create(:add => :always) # => "/path/to/base.txt.02"
30
40
 
41
+ We give a base filename and get an instance of FileName.
42
+ Then, we get filenames with sequential numbers
43
+ by FileName#create.
44
+ The default value of option :add of FileName#create is :auto
45
+ and add some string to filename if the targeted file exists.
46
+ If we set :auto to the option :add as the above example,
47
+ we add always add some string to filename.
48
+
31
49
  ### Filenames with time strings
32
50
 
33
51
  require 'filename'
34
52
  filename = FileName.new('base.txt', :type => :time)
35
53
  p filename.create(:add => :always) # For example, returns "/path/to/base.txt.20110326_073247_078247"
36
54
 
55
+ If we set :time to the option :type of FileName.new,
56
+ we add time string to filename.
57
+ The default value of the option :type is :number and
58
+ we add a number to filename.
59
+
37
60
  ### Control adding additional parts
38
61
 
39
62
  require 'filename'
@@ -42,7 +65,11 @@ Default options add suffixes and use additional parts of sequential numbers.
42
65
  p filename.create(:add => :prohibit) # Always returns "/path/to/base.txt"
43
66
  p filename.create(:add => :auto) # If the file exist, returns "/path/to/base.txt.00". Otherwise, "/path/to/base.txt"
44
67
 
45
- ### Add additinal parts to prefix or middle position
68
+ ### Add additional parts to prefix or middle position
69
+
70
+ An option :position controls positions of additional strings.
71
+ The value :suffix, :prefix, and :middle are accepted.
72
+ The default is :suffix.
46
73
 
47
74
  require 'filename'
48
75
  filename = FileName.new('base.txt', :position => :prefix)
@@ -53,6 +80,11 @@ Default options add suffixes and use additional parts of sequential numbers.
53
80
 
54
81
  ### Change delimiters
55
82
 
83
+ The default delimiter is "." if the option :position is :suffix.
84
+ Otherwise, the string is "_".
85
+ If we want to change delimiter string, we set desired string to
86
+ an option :delimiter.
87
+
56
88
  require 'filename'
57
89
  filename = FileName.new('base.txt', :position => :prefix, :delimiter => '')
58
90
  p filename.create(:add => :always) # => "/path/to/00base.txt"
@@ -62,24 +94,48 @@ Default options add suffixes and use additional parts of sequential numbers.
62
94
 
63
95
  ### Change extensions
64
96
 
97
+ If we want to change extension of base filename,
98
+ we set an option :extension.
99
+
65
100
  require 'filename'
66
101
  filename = FileName.new('base.txt')
67
102
  p filename.create(:add => :always, :extension => 'log') # => "/path/to/base.log.00"
68
103
 
69
104
  ### Create parent directory
70
105
 
106
+ If we set :parent to an option :directory,
107
+ we make the parent directories on creating filenames.
108
+
71
109
  require 'filename'
72
110
  filename = FileName.new('dir/base.txt')
73
111
  p filename.create(:add => :always, :directory => :parent) # Create 'dir' directory.
74
112
 
75
113
  ### Create directory of created filename
76
114
 
115
+ If we set :self to an option :directory,
116
+ we make the directories of created filenames.
117
+
77
118
  require 'filename'
78
119
  filename = FileName.new('dir1/dir2')
79
120
  p filename.create(:add => :always, :directory => :self) # Create 'dir1/dir2' directory.
80
121
 
122
+ ### Create empty file
123
+
124
+ If we set an option :file, we can create an empty file.
125
+ If the value is :write and the targeted file does not exist,
126
+ we create an empty file.
127
+ If the value is :overwrite, we always create an empty file.
128
+
129
+ require 'filename'
130
+ filename = FileName.new('path/to/file')
131
+ p filename.create(:file => :write) # Create empty file 'path/to/file' if 'path/to/file' does not exist.
132
+ p filename.create(:file => :overwrite) # Create empty file 'path/to/file' even if 'path/to/file' exists.
133
+
81
134
  ### Change starting numbers and digits of sequential numbers
82
135
 
136
+ We can set starting number by an option :start and
137
+ digit number by an option :digit.
138
+
83
139
  require 'filename'
84
140
  filename = FileName.new('base.txt', :digit => 4, :start => 1000)
85
141
  p filename.create(:add => :always) # => "/path/to/base.txt.1000"
@@ -87,6 +143,11 @@ Default options add suffixes and use additional parts of sequential numbers.
87
143
 
88
144
  ### Change formats of sequential numbers
89
145
 
146
+ If we set string object to an option :format,
147
+ the value is used by method sprintf.
148
+ If we set proc object that take Fixnum,
149
+ we use the evaluation of the proc object as an additional string.
150
+
90
151
  require 'filename'
91
152
  filename = FileName.new('base.txt', :format => "@%03d@")
92
153
  p filename.create(:add => :always) # => "/path/to/base.txt.@000@"
@@ -97,6 +158,13 @@ Default options add suffixes and use additional parts of sequential numbers.
97
158
 
98
159
  ### Change formats of time strings
99
160
 
161
+ We can set a string or proc object to an option :format
162
+ similar to the case of number type.
163
+ If the value is a string, we use the string as first argument of
164
+ method sprintf.
165
+ If the value is a proc object, we evaluate it and
166
+ use returned value as an additional part.
167
+
100
168
  require 'filename'
101
169
  filename = FileName.new('base.txt', :format => "%H%M%S")
102
170
  p filename.create(:add => :always) # For example, returns "/path/to/base.txt.075130"
@@ -106,6 +174,10 @@ Default options add suffixes and use additional parts of sequential numbers.
106
174
 
107
175
  ### Use of variable for proc set by :format option
108
176
 
177
+ If we give a hash to an option :data,
178
+ the values of the hash are saved as instance variables.
179
+ We can use the variables in proc object for an option :format.
180
+
109
181
  require 'filename'
110
182
  filename = FileName.new('base.txt', :data => { :a => 3 },
111
183
  :format => lambda { |n| s = sprintf("%03d", @a * n); @a += 2; s })
@@ -113,18 +185,38 @@ Default options add suffixes and use additional parts of sequential numbers.
113
185
  p filename.create(:add => :always) # => "/path/to/base.txt.005"
114
186
  p filename.create(:add => :always) # => "/path/to/base.txt.014"
115
187
 
188
+ In the above example we prepare the instance variable "@a = 3"
189
+ and use @a in proc object specified by an option :format.
190
+
191
+ ### Options of FileName#new, FileName#create, and FileName.create
192
+
193
+ If we set options of FileName#create to FileName#new,
194
+ the values is the default values of FileName#create.
195
+ If we want to change on creating filenames,
196
+ we set options for FileName#create.
197
+ Options of FileName.create are the same as these of FileName#new.
198
+
116
199
  ## How to use command 'filename-create'
117
200
 
201
+ We can use command 'filename-create' on shell.
202
+
203
+ ### Basic usage and options
204
+
118
205
  filename-create with 'new' and basename puts path of file.
119
206
 
120
207
  filename-create new basename
121
208
 
122
- We can use the corresponding options to optional arguments of
123
- FileName#create.
209
+ Then the created filename is displayed.
210
+
211
+ We can use the options corresponding to
212
+ the optional arguments of FileName#create
213
+ except for "--cache" option.
214
+ We also can not set proc object to :format by command filename-create.
124
215
 
125
216
  -s, --start NUM Set the starting number.
126
217
  -i, --digit NUM Set the digit of number.
127
218
  -d, --delimiter STR Set the delimiter string: number or time.
219
+ -n, --no-delimiter Do not use delimiter.
128
220
  -t, --type TYPE Set the type of additional part.
129
221
  -f, --format STR Set the format string.
130
222
  -p, --position POS Set the position of addition: prefix, suffix, or middle.
@@ -132,15 +224,47 @@ FileName#create.
132
224
  -e, --extension STR Set the extension string.
133
225
  -a, --add STR Change the behavior of addition: always, auto, or prohibit.
134
226
  -D, --directory STR Create directory: self or parent.
227
+ -F, --file STR Create an empty file: write or overwrite.
228
+ -c, --cache KEY Create cache for command "new" and "config".
229
+
230
+ ### cache
135
231
 
136
232
  To create sequential filenames, we call 'filename-create new' with '--cache' option
137
- and then 'filename-create cache'.
233
+ to create cache. At this point, we set options that we want to use.
234
+
235
+ filename-create new basename --add always --cache cache_name
236
+
237
+ We refer created cache by 'cache_name'.
238
+ To generate filenames from cache we type the following.
239
+
240
+ filename-create cache cache_name
241
+
242
+ Sequential commands create filenames with sequential numbers.
138
243
 
139
- filename-create new basename --add always --cache base
140
- filename-create cache base
141
- /path/to/basename.00
142
- filename-create cache base
143
- /pat/to/basename.01
244
+ When we finish to use the cache,
245
+ we type
246
+
247
+ filename-create delete_cache cache_name
248
+
249
+ and then delete the cache.
250
+
251
+ To list the saved cache, we type
252
+
253
+ filename-create list_cache
254
+
255
+ ### Configuration files
256
+
257
+ If we save files in directory ~/.filename\_gem/conf,
258
+ which define a ruby hash of options of FileName#create,
259
+ we can use the configurations to create filenames.
260
+ If there is ~/.filename\_gem/conf/sample.rb that preserves an option hash,
261
+ we type
262
+
263
+ filename-create config sample
264
+
265
+ and get filename from the configuration.
266
+ We can also create cache to type it with "--cache" option
267
+ same as "filename-create new".
144
268
 
145
269
  ## Contributing to filename
146
270
 
@@ -152,7 +276,11 @@ and then 'filename-create cache'.
152
276
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
153
277
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
154
278
 
279
+ ## License
280
+
281
+ The license is GPLv3.
282
+ See LICENSE.txt for further details.
283
+
155
284
  ## Copyright
156
285
 
157
- Copyright (c) 2011 Takayuki YAMAGUCHI. See LICENSE.txt for
158
- further details.
286
+ Copyright (c) 2011 Takayuki YAMAGUCHI.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.1.0
@@ -18,7 +18,7 @@ Usage: #{basename} <command> [arguments ...] [options ...]
18
18
 
19
19
  HELP
20
20
 
21
- Version = '0.0.4'
21
+ Version = '0.1.0'
22
22
 
23
23
  options = {}
24
24
  create_cache = false
@@ -34,6 +34,9 @@ begin
34
34
  opt.on('-d STR', '--delimiter STR', String, 'Set the delimiter string: number or time.') do |v|
35
35
  options[:delimiter] = v
36
36
  end
37
+ opt.on('-n', '--no-delimiter', "Do not use delimiter.") do |v|
38
+ options[:delimiter] = ''
39
+ end
37
40
  opt.on('-t TYPE', '--type TYPE', String, 'Set the type of additional part.') do |v|
38
41
  options[:type] = v.intern
39
42
  end
@@ -55,7 +58,10 @@ begin
55
58
  opt.on('-D STR', '--directory STR', String, 'Create directory: self or parent.') do |v|
56
59
  options[:directory] = v.intern
57
60
  end
58
- opt.on('-c KEY', '--cache KEY', String, 'Create cache for command "new".') do |v|
61
+ opt.on('-F STR', '--file STR', String, 'Create an empty file: write or overwrite.') do |v|
62
+ options[:file] = v.intern
63
+ end
64
+ opt.on('-c KEY', '--cache KEY', String, 'Create cache for command "new" and "config".') do |v|
59
65
  create_cache = v
60
66
  end
61
67
  opt.parse!(ARGV)
@@ -116,6 +122,9 @@ begin
116
122
  when 'init'
117
123
  manage.save_configuration_example
118
124
  exit(0)
125
+ else
126
+ raise OptionParser::InvalidArgument
127
+ exit(1)
119
128
  end
120
129
  rescue OptionParser::InvalidOption
121
130
  $stderr.print <<MES
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{filename}
8
- s.version = "0.0.6"
8
+ s.version = "0.1.0"
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-04-09}
12
+ s.date = %q{2011-04-15}
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
15
  s.executables = ["filename-create"]
@@ -8,7 +8,7 @@ class FileName
8
8
 
9
9
  attr_accessor :configuration_key, :format
10
10
 
11
- OPTIONS_CREATE = [:extension, :add, :directory]
11
+ OPTIONS_CREATE = [:extension, :add, :directory, :file]
12
12
 
13
13
  # The options are following:
14
14
  #
@@ -55,6 +55,9 @@ class FileName
55
55
  #
56
56
  # [:directory]
57
57
  # Default value of the option of FileName#create.
58
+ #
59
+ # [:file]
60
+ # Default value of the option of FileName#create.
58
61
  def initialize(basepath, *rest)
59
62
  if Hash === rest[-1]
60
63
  opts = rest.delete_at(-1)
@@ -69,6 +72,9 @@ class FileName
69
72
  end
70
73
  @number = opts[:start] || 0
71
74
  @digit = opts[:digit] || 2
75
+ if @digit < 1
76
+ raise ArgumentError, "Number of digit must be positive."
77
+ end
72
78
  @type = opts[:type] || :number
73
79
  @position = opts[:position] || :suffix
74
80
  @delimiter = opts[:delimiter] || (@position == :suffix ? '.' : '_')
@@ -190,6 +196,28 @@ class FileName
190
196
  end
191
197
  private :create_directory
192
198
 
199
+ def touch_file(path)
200
+ FileUtils.mkdir_p(File.dirname(path))
201
+ open(path, 'w') {}
202
+ end
203
+ private :touch_file
204
+
205
+ def write_file(path, file_opt)
206
+ if file_opt
207
+ case file_opt
208
+ when :write
209
+ unless File.exist?(path)
210
+ touch_file(path)
211
+ end
212
+ when :overwrite
213
+ touch_file(path)
214
+ else
215
+ raise ArgumentError, "Invalid file option."
216
+ end
217
+ end
218
+ end
219
+ private :write_file
220
+
193
221
  # The options are following:
194
222
  # [:extension (String of extension)]
195
223
  # If we want to change extension, we set the value of the option.
@@ -203,6 +231,12 @@ class FileName
203
231
  # [:directory (:self, :parent, or nil)]
204
232
  # If the value is :self, we make directory of created filename.
205
233
  # If the value is :parent, we make parent directory of created filename.
234
+ # If the value is nil, we do nothing.
235
+ #
236
+ # [:file]
237
+ # If the value is :overwrite, we create a new empty file.
238
+ # If the value is :write and the file does not exist, we create an empty file.
239
+ # If the value is nil, we do nothing.
206
240
  def create(opts = {})
207
241
  base = get_basepath(get_option_create(opts, :extension))
208
242
  opt_add = get_option_create(opts, :add)
@@ -220,6 +254,7 @@ class FileName
220
254
  path = base
221
255
  end
222
256
  create_directory(path, get_option_create(opts, :directory))
257
+ write_file(path, get_option_create(opts, :file))
223
258
  path
224
259
  end
225
260
 
@@ -143,6 +143,25 @@ describe FileName do
143
143
  FileUtils.rm_r(File.dirname(path))
144
144
  end
145
145
 
146
+ it "should create empty file" do
147
+ basename = File.join(File.dirname(__FILE__), 'abc/def')
148
+ filename = FileName.new(basename)
149
+ path = filename.create(:file => :write)
150
+ File.exist?(path).should be_true
151
+ File.read(path).should == ''
152
+ FileUtils.rm_r(File.dirname(path))
153
+ end
154
+
155
+ it "should overwrite empty file" do
156
+ basename = File.join(File.dirname(__FILE__), 'abc/def')
157
+ filename = FileName.new(basename, :add => :prohibit)
158
+ open(filename.create(:directory => :parent), 'w') { |f| f.puts "hello world" }
159
+ path = filename.create(:file => :overwrite)
160
+ File.exist?(path).should be_true
161
+ File.read(path).should == ''
162
+ FileUtils.rm_r(File.dirname(path))
163
+ end
164
+
146
165
  it "should conbine a few arguments" do
147
166
  filename = FileName.new('abc', 'def', 'ghi')
148
167
  filename.create.should == File.expand_path('abc/def/ghi')
@@ -212,6 +231,30 @@ describe FileName do
212
231
  end
213
232
  end
214
233
 
234
+ it "should create empty file" do
235
+ basename = File.join(File.dirname(__FILE__), 'abc/def')
236
+ filename = FileName.new(basename, :file => :write)
237
+ NUMBER_TEST_REPEAT.times do |i|
238
+ path = filename.create
239
+ File.exist?(path).should be_true
240
+ File.read(path).should == ''
241
+ FileUtils.rm_r(File.dirname(path))
242
+ end
243
+ end
244
+
245
+ it "should overwrite empty file" do
246
+ basename = File.join(File.dirname(__FILE__), 'abc/def')
247
+ filename0 = FileName.new(basename, :add => :prohibit, :directory => :parent)
248
+ filename = FileName.new(basename, :add => :prohibit, :file => :overwrite)
249
+ NUMBER_TEST_REPEAT.times do |i|
250
+ open(filename0.create, 'w') { |f| f.puts "hello world" }
251
+ path = filename.create
252
+ File.exist?(path).should be_true
253
+ File.read(path).should == ''
254
+ FileUtils.rm_r(File.dirname(path))
255
+ end
256
+ end
257
+
215
258
  it "should change extension" do
216
259
  filename = FileName.new(__FILE__, :extension => 'txt', :add => :prohibit)
217
260
  NUMBER_TEST_REPEAT.times do |i|
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: filename
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.6
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Takayuki YAMAGUCHI
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-09 00:00:00 Z
13
+ date: 2011-04-15 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -94,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
- hash: -1246538817859403112
97
+ hash: -1890861991035807209
98
98
  segments:
99
99
  - 0
100
100
  version: "0"