filename 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -20,7 +20,7 @@ Default options add suffixes and use additional parts of sequential numbers.
20
20
  filename = FileName.new('base.txt', :type => :time)
21
21
  p filename.create(:add => :always) # For example, returns "/path/to/base.txt.20110326_073247_078247"
22
22
 
23
- ### Cantrol adding additional parts
23
+ ### Control adding additional parts
24
24
 
25
25
  require 'filename'
26
26
  filename = FileName.new('base.txt')
@@ -55,8 +55,8 @@ Default options add suffixes and use additional parts of sequential numbers.
55
55
  ### Create parent directory
56
56
 
57
57
  require 'filename'
58
- filename = FileName.new('base.txt')
59
- p filename.create(:add => :always, :directory => true)
58
+ filename = FileName.new('dir/base.txt')
59
+ p filename.create(:add => :always, :directory => true) # Create 'dir' directory.
60
60
 
61
61
  ### Change starting numbers and digits of sequential numbers
62
62
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{filename}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
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-26}
12
+ s.date = %q{2011-03-29}
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.extra_rdoc_files = [
@@ -6,6 +6,8 @@ autoload :FileUtils, 'fileutils'
6
6
  #
7
7
  class FileName
8
8
 
9
+ OPTIONS_CREATE = [:extension, :add, :directory]
10
+
9
11
  # The options are following:
10
12
  #
11
13
  # [:start (Fixnum)]
@@ -32,8 +34,25 @@ class FileName
32
34
  #
33
35
  # [:position (:prefix, :suffix, or :middle)]
34
36
  # We specify of position of additional part of filename.
37
+ #
38
+ # [:path]
39
+ # We sepecify if path created by FileName#create is absolute or relative.
40
+ # Default is absolute.
41
+ #
42
+ # [:extension]
43
+ # Default value of the option of FileName#create.
44
+ #
45
+ # [:add]
46
+ # Default value of the option of FileName#create.
47
+ #
48
+ # [:directory]
49
+ # Default value of the option of FileName#create.
35
50
  def initialize(basepath, opts = {})
36
- @basepath = File.expand_path(basepath)
51
+ if opts[:path] == :relative
52
+ @basepath = basepath
53
+ else
54
+ @basepath = File.expand_path(basepath)
55
+ end
37
56
  @number = opts[:start] || 0
38
57
  @digit = opts[:digit] || 2
39
58
  @type = opts[:type] || :number
@@ -41,6 +60,12 @@ class FileName
41
60
  @delimiter = opts[:delimiter] || (@position == :suffix ? '.' : '_')
42
61
  @format = opts[:format]
43
62
  @last_addition = nil
63
+ @default_create = {}
64
+ opts.each do |key, val|
65
+ if OPTIONS_CREATE.include?(key)
66
+ @default_create[key] = val
67
+ end
68
+ end
44
69
  end
45
70
 
46
71
  def get_basepath(extension = nil)
@@ -124,6 +149,11 @@ class FileName
124
149
  end
125
150
  private :add_addition
126
151
 
152
+ def get_option_create(opts, key)
153
+ opts.has_key?(key) ? opts[key] : @default_create[key]
154
+ end
155
+ private :get_option_create
156
+
127
157
  # The options are following:
128
158
  # [:extension (String of extension)]
129
159
  # If we want to change extension, we set the value of the option.
@@ -138,12 +168,13 @@ class FileName
138
168
  # If the value is true and the parent directory does not exist,
139
169
  # we create the directory.
140
170
  def create(opts = {})
141
- base = get_basepath(opts[:extension])
142
- FileUtils.mkdir_p(File.dirname(base)) if opts[:directory]
143
- if addition = get_addition(opts[:add], base)
171
+ base = get_basepath(get_option_create(opts, :extension))
172
+ FileUtils.mkdir_p(File.dirname(base)) if get_option_create(opts, :directory)
173
+ opt_add = get_option_create(opts, :add)
174
+ if addition = get_addition(opt_add, base)
144
175
  path = add_addition(base, addition)
145
176
  while File.exist?(path)
146
- if addition = get_addition(opts[:add], base)
177
+ if addition = get_addition(opt_add, base)
147
178
  path = add_addition(base, addition)
148
179
  else
149
180
  raise "Can not create new filename."
@@ -156,20 +187,9 @@ class FileName
156
187
  end
157
188
 
158
189
  # Executing FileName.new and FileName.create, we get new filename.
159
- # The options for both FileName.new and FileName#create are available.
190
+ # The same options of FileName.new are available.
160
191
  def self.create(basepath, opts = {})
161
- opts_new = {}
162
- opts_create = {}
163
- opts.each do |key, val|
164
- case key
165
- when :extension, :add, :directory
166
- opts_create[key] = val
167
- else
168
- opts_new[key] = val
169
- end
170
- end
171
- fname = self.new(basepath, opts_new)
172
- fname.create(opts_create)
192
+ self.new(basepath, opts).create
173
193
  end
174
194
 
175
195
  end
@@ -1,9 +1,22 @@
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)
5
+ dir = File.dirname(basename)
6
+ File.exist?(dir).should be_true
7
+ Dir.rmdir(dir)
8
+ end
9
+
10
+ NOT_EXIST_FILE_PATH = 'abc.txt'
11
+
4
12
  it "should return unchanged filename" do
5
- filename = FileName.new("abc.txt")
6
- filename.create.should == File.expand_path(File.dirname('.') + '/abc.txt')
13
+ filename = FileName.new(NOT_EXIST_FILE_PATH)
14
+ filename.create.should == File.expand_path(File.dirname('.') + '/' + NOT_EXIST_FILE_PATH)
15
+ end
16
+
17
+ it "should return unchanged filename with relative path" do
18
+ filename = FileName.new("abc.txt", :add => :auto, :path => :relative)
19
+ filename.create.should == NOT_EXIST_FILE_PATH
7
20
  end
8
21
 
9
22
  it "should return new filename with number" do
@@ -102,13 +115,69 @@ describe FileName do
102
115
  name.should match(Regexp.new("_\\d+\\#{ext}"))
103
116
  end
104
117
 
118
+ it "should change extension" do
119
+ filename = FileName.new(__FILE__)
120
+ path = filename.create(:add => :prohibit, :extension => 'txt')
121
+ path.should_not == __FILE__
122
+ path.should match(/\.txt$/)
123
+ end
124
+
105
125
  it "should create parent directory" do
106
126
  basename = File.join(File.dirname(__FILE__), 'abc/def')
107
127
  filename = FileName.new(basename)
108
128
  path = filename.create(:directory => true)
109
- dir = File.dirname(basename)
110
- File.exist?(dir).should be_true
111
- Dir.rmdir(dir)
129
+ check_create_directory(filename, basename, path)
130
+ end
131
+
132
+ context "when we set the default options of FileName#create" do
133
+ NUMBER_TEST_REPEAT = 3
134
+
135
+ it "should prohibit addition" do
136
+ filename = FileName.new(__FILE__, :add => :prohibit)
137
+ NUMBER_TEST_REPEAT.times do |i|
138
+ filename.create.should == __FILE__
139
+ end
140
+ end
141
+
142
+ it "should add always" do
143
+ filename = FileName.new(__FILE__, :add => :always)
144
+ NUMBER_TEST_REPEAT.times do |i|
145
+ filename.create.should_not == __FILE__
146
+ end
147
+ end
148
+
149
+ it "should add automatically" do
150
+ filename = FileName.new(__FILE__, :add => :auto)
151
+ NUMBER_TEST_REPEAT.times do |i|
152
+ filename.create.should_not == __FILE__
153
+ end
154
+ end
155
+
156
+ it "should return as it is" do
157
+ name_not_exit = __FILE__ + Time.now.to_i.to_s
158
+ filename = FileName.new(name_not_exit, :add => :auto)
159
+ NUMBER_TEST_REPEAT.times do |i|
160
+ filename.create.should == name_not_exit
161
+ end
162
+ end
163
+
164
+ it "should create parent directory" do
165
+ basename = File.join(File.dirname(__FILE__), 'abc/def')
166
+ filename = FileName.new(basename, :directory => true)
167
+ NUMBER_TEST_REPEAT.times do |i|
168
+ path = filename.create
169
+ check_create_directory(filename, basename, path)
170
+ end
171
+ end
172
+
173
+ it "should change extension" do
174
+ filename = FileName.new(__FILE__, :extension => 'txt', :add => :prohibit)
175
+ NUMBER_TEST_REPEAT.times do |i|
176
+ path = filename.create
177
+ path.should_not == __FILE__
178
+ path.should match(/\.txt$/)
179
+ end
180
+ end
112
181
  end
113
182
 
114
183
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: filename
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
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-03-26 00:00:00 +09:00
13
+ date: 2011-03-29 00:00:00 +09:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
- hash: -1256757600760491313
96
+ hash: 1626148105231843710
97
97
  segments:
98
98
  - 0
99
99
  version: "0"