filename 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,22 @@
1
1
  # filename
2
2
 
3
- The class to create filename that is not duplicated.
3
+ This gem includs the class FileName to create filename that is not duplicated
4
+ and command 'filename-create' for shell.
4
5
  We select type of additional part of filename: number or time.
5
6
 
7
+ ## Installation
8
+
9
+ gem install filename
10
+
11
+ ## How to use as library
12
+
13
+ We need to require 'filename'.
14
+
15
+ require 'filename'
16
+
17
+ And then we create method for an instance of FileName or
18
+ class method FileName.create.
19
+
6
20
  ### Filenames with sequential numbers
7
21
 
8
22
  Default options add suffixes and use additional parts of sequential numbers.
@@ -56,7 +70,13 @@ Default options add suffixes and use additional parts of sequential numbers.
56
70
 
57
71
  require 'filename'
58
72
  filename = FileName.new('dir/base.txt')
59
- p filename.create(:add => :always, :directory => true) # Create 'dir' directory.
73
+ p filename.create(:add => :always, :directory => :parent) # Create 'dir' directory.
74
+
75
+ ### Create directory of created filename
76
+
77
+ require 'filename'
78
+ filename = FileName.new('dir1/dir2')
79
+ p filename.create(:add => :always, :directory => :self) # Create 'dir1/dir2' directory.
60
80
 
61
81
  ### Change starting numbers and digits of sequential numbers
62
82
 
@@ -84,6 +104,35 @@ Default options add suffixes and use additional parts of sequential numbers.
84
104
  filename = FileName.new('base.txt', :start => 10, :format => lambda { |t| t.usec.to_s })
85
105
  p filename.create(:add => :always) # For example, returns "/path/to/base.txt.849963"
86
106
 
107
+ ## How to use command 'filename-create'
108
+
109
+ filename-create with 'new' and basename puts path of file.
110
+
111
+ filename-create new basename
112
+
113
+ We can use the corresponding options to optional arguments of
114
+ FileName#create.
115
+
116
+ -s, --start NUM Set the starting number.
117
+ -i, --digit NUM Set the digit of number.
118
+ -d, --delimiter STR Set the delimiter string: number or time.
119
+ -t, --type TYPE Set the type of additional part.
120
+ -f, --format STR Set the format string.
121
+ -p, --position POS Set the position of addition: prefix, suffix, or middle.
122
+ -P, --path TYPE Set the type of path: absolute or relative.
123
+ -e, --extension STR Set the extension string.
124
+ -a, --add STR Change the behavior of addition: always, auto, or prohibit.
125
+ -D, --directory STR Create directory: self or parent.
126
+
127
+ To create sequential filenames, we call 'filename-create new' with '--cache' option
128
+ and then 'filename-create cache'.
129
+
130
+ filename-create new basename --add always --cache base
131
+ filename-create cache base
132
+ /path/to/basename.00
133
+ filename-create cache base
134
+ /pat/to/basename.01
135
+
87
136
  ## Contributing to filename
88
137
 
89
138
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -44,7 +44,7 @@ begin
44
44
  options[:position] = v.intern
45
45
  end
46
46
  opt.on('-P TYPE', '--path TYPE', String, 'Set the type of path: absolute or relative.') do |v|
47
- options[:type] = v.intern
47
+ options[:path] = v.intern
48
48
  end
49
49
  opt.on('-e STR', '--extension STR', String, 'Set the extension string.') do |v|
50
50
  options[:extension] = v
@@ -52,8 +52,8 @@ begin
52
52
  opt.on('-a STR', '--add STR', String, 'Change the behavior of addition: always, auto, or prohibit.') do |v|
53
53
  options[:add] = v.intern
54
54
  end
55
- opt.on('-D', '--directory', 'Create parant directory.') do |v|
56
- options[:directory] = true
55
+ opt.on('-D STR', '--directory STR', String, 'Create directory: self or parent.') do |v|
56
+ options[:directory] = v.intern
57
57
  end
58
58
  opt.on('-c KEY', '--cache KEY', String, 'Create cache for command "new".') do |v|
59
59
  create_cache = v
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{filename}
8
- s.version = "0.0.4"
8
+ s.version = "0.0.5"
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-02}
12
+ s.date = %q{2011-04-09}
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"]
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
37
37
  s.homepage = %q{http://github.com/ytaka/filename}
38
38
  s.licenses = ["GPLv3"]
39
39
  s.require_paths = ["lib"]
40
- s.rubygems_version = %q{1.7.1}
40
+ s.rubygems_version = %q{1.7.2}
41
41
  s.summary = %q{Filename generator}
42
42
  s.test_files = [
43
43
  "spec/cache_spec.rb",
@@ -160,6 +160,21 @@ class FileName
160
160
  end
161
161
  private :get_option_create
162
162
 
163
+ def create_directory(base, dir_opt)
164
+ if dir_opt
165
+ case dir_opt
166
+ when :self
167
+ dir = base
168
+ when :parent
169
+ dir = File.dirname(base)
170
+ else
171
+ raise ArgumentError, "Invalid directory option."
172
+ end
173
+ FileUtils.mkdir_p(dir)
174
+ end
175
+ end
176
+ private :create_directory
177
+
163
178
  # The options are following:
164
179
  # [:extension (String of extension)]
165
180
  # If we want to change extension, we set the value of the option.
@@ -170,12 +185,11 @@ class FileName
170
185
  # * :auto - If the file exists, we add.
171
186
  # * :prohibit - Even if the file exists, we do not add.
172
187
  #
173
- # [:directory (true or false)]
174
- # If the value is true and the parent directory does not exist,
175
- # we create the directory.
188
+ # [:directory (:self, :parent, or nil)]
189
+ # If the value is :self, we make directory of created filename.
190
+ # If the value is :parent, we make parent directory of created filename.
176
191
  def create(opts = {})
177
192
  base = get_basepath(get_option_create(opts, :extension))
178
- FileUtils.mkdir_p(File.dirname(base)) if get_option_create(opts, :directory)
179
193
  opt_add = get_option_create(opts, :add)
180
194
  if addition = get_addition(opt_add, base)
181
195
  path = add_addition(base, addition)
@@ -188,8 +202,10 @@ class FileName
188
202
  end
189
203
  path
190
204
  else
191
- base
205
+ path = base
192
206
  end
207
+ create_directory(path, get_option_create(opts, :directory))
208
+ path
193
209
  end
194
210
 
195
211
  # If @format is a Proc object, we can not dump a FileName object.
@@ -1,7 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe FileName do
4
- def check_create_directory(filename, basename, path)
4
+ def check_creation_of_parent_directory(basename, path)
5
5
  dir = File.dirname(basename)
6
6
  File.exist?(dir).should be_true
7
7
  Dir.rmdir(dir)
@@ -125,8 +125,16 @@ describe FileName do
125
125
  it "should create parent directory" do
126
126
  basename = File.join(File.dirname(__FILE__), 'abc/def')
127
127
  filename = FileName.new(basename)
128
- path = filename.create(:directory => true)
129
- check_create_directory(filename, basename, path)
128
+ path = filename.create(:directory => :parent)
129
+ check_creation_of_parent_directory(basename, path)
130
+ end
131
+
132
+ it "should create directory of filename" do
133
+ basename = File.join(File.dirname(__FILE__), 'abc/def')
134
+ filename = FileName.new(basename)
135
+ path = filename.create(:directory => :self)
136
+ File.exist?(path).should be_true
137
+ FileUtils.rm_r(File.dirname(path))
130
138
  end
131
139
 
132
140
  it "should conbine a few arguments" do
@@ -181,10 +189,20 @@ describe FileName do
181
189
 
182
190
  it "should create parent directory" do
183
191
  basename = File.join(File.dirname(__FILE__), 'abc/def')
184
- filename = FileName.new(basename, :directory => true)
192
+ filename = FileName.new(basename, :directory => :parent)
193
+ NUMBER_TEST_REPEAT.times do |i|
194
+ path = filename.create
195
+ check_creation_of_parent_directory(basename, path)
196
+ end
197
+ end
198
+
199
+ it "should create directory of filename" do
200
+ basename = File.join(File.dirname(__FILE__), 'abc/def')
201
+ filename = FileName.new(basename, :directory => :self, :add => :always)
185
202
  NUMBER_TEST_REPEAT.times do |i|
186
203
  path = filename.create
187
- check_create_directory(filename, basename, path)
204
+ File.exist?(path).should be_true
205
+ FileUtils.rm_r(File.dirname(path))
188
206
  end
189
207
  end
190
208
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: filename
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
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-02 00:00:00 Z
13
+ date: 2011-04-09 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: -4477579115920714388
97
+ hash: 3878045730836072065
98
98
  segments:
99
99
  - 0
100
100
  version: "0"
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  requirements: []
108
108
 
109
109
  rubyforge_project:
110
- rubygems_version: 1.7.1
110
+ rubygems_version: 1.7.2
111
111
  signing_key:
112
112
  specification_version: 3
113
113
  summary: Filename generator