filename 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +76 -4
  2. data/VERSION +1 -1
  3. data/filename.gemspec +2 -2
  4. data/lib/filename.rb +30 -38
  5. metadata +3 -3
data/README.md CHANGED
@@ -3,14 +3,87 @@
3
3
  The class to create filename that is not duplicated.
4
4
  We select type of additional part of filename: number or time.
5
5
 
6
- ## Example
6
+ ### Filenames with sequential numbers
7
7
 
8
- basename = 'base.txt'
9
- filename = FileName.new(basename)
8
+ Default options add suffixes and use additional parts of sequential numbers.
9
+
10
+ require 'filename'
11
+ filename = FileName.new('base.txt')
12
+ # Same as FileName.new('base.txt', :type => :number, :digit => 2, :position => :suffix, :start => 0, :delimiter => '.')
10
13
  p filename.create(:add => :always) # => "/path/to/base.txt.00"
11
14
  p filename.create(:add => :always) # => "/path/to/base.txt.01"
12
15
  p filename.create(:add => :always) # => "/path/to/base.txt.02"
13
16
 
17
+ ### Filenames with time strings
18
+
19
+ require 'filename'
20
+ filename = FileName.new('base.txt', :type => :time)
21
+ p filename.create(:add => :always) # For example, returns "/path/to/base.txt.20110326_073247_078247"
22
+
23
+ ### Cantrol adding additional parts
24
+
25
+ require 'filename'
26
+ filename = FileName.new('base.txt')
27
+ p filename.create(:add => :always) # Always returns "/path/to/base.txt.00"
28
+ p filename.create(:add => :prohibit) # Always returns "/path/to/base.txt"
29
+ p filename.create(:add => :auto) # If the file exist, returns "/path/to/base.txt.00". Otherwise, "/path/to/base.txt"
30
+
31
+ ### Add additinal parts to prefix or middle position
32
+
33
+ require 'filename'
34
+ filename = FileName.new('base.txt', :position => :prefix)
35
+ p filename.create(:add => :always) # => "/path/to/00_base.txt"
36
+
37
+ filename = FileName.new('base.txt', :position => :middle)
38
+ p filename.create(:add => :always) # => "/path/to/base_00.txt"
39
+
40
+ ### Change delimiters
41
+
42
+ require 'filename'
43
+ filename = FileName.new('base.txt', :position => :prefix, :delimiter => '')
44
+ p filename.create(:add => :always) # => "/path/to/00base.txt"
45
+
46
+ filename = FileName.new('base.txt', :position => :prefix, :delimiter => '-')
47
+ p filename.create(:add => :always) # => "/path/to/00-base.txt"
48
+
49
+ ### Change extensions
50
+
51
+ require 'filename'
52
+ filename = FileName.new('base.txt')
53
+ p filename.create(:add => :always, :extension => 'log') # => "/path/to/base.log.00"
54
+
55
+ ### Create parent directory
56
+
57
+ require 'filename'
58
+ filename = FileName.new('base.txt')
59
+ p filename.create(:add => :always, :directory => true)
60
+
61
+ ### Change starting numbers and digits of sequential numbers
62
+
63
+ require 'filename'
64
+ filename = FileName.new('base.txt', :digit => 4, :start => 1000)
65
+ p filename.create(:add => :always) # => "/path/to/base.txt.1000"
66
+ p filename.create(:add => :always) # => "/path/to/base.txt.1001"
67
+
68
+ ### Change formats of sequential numbers
69
+
70
+ require 'filename'
71
+ filename = FileName.new('base.txt', :format => "@%03d@")
72
+ p filename.create(:add => :always) # => "/path/to/base.txt.@000@"
73
+
74
+ filename = FileName.new('base.txt', :type => :time, :format => lambda { |n| sprintf("%03d", n * n) })
75
+ p filename.create(:add => :always) # => "/path/to/base.txt.100"
76
+ p filename.create(:add => :always) # => "/path/to/base.txt.121"
77
+
78
+ ### Change formats of time strings
79
+
80
+ require 'filename'
81
+ filename = FileName.new('base.txt', :format => "%H%M%S")
82
+ p filename.create(:add => :always) # For example, returns "/path/to/base.txt.075130"
83
+
84
+ filename = FileName.new('base.txt', :start => 10, :format => lambda { |t| t.usec.to_s })
85
+ p filename.create(:add => :always) # For example, returns "/path/to/base.txt.849963"
86
+
14
87
  ## Contributing to filename
15
88
 
16
89
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
@@ -25,4 +98,3 @@ We select type of additional part of filename: number or time.
25
98
 
26
99
  Copyright (c) 2011 Takayuki YAMAGUCHI. See LICENSE.txt for
27
100
  further details.
28
-
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/filename.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{filename}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
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-25}
12
+ s.date = %q{2011-03-26}
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 = [
data/lib/filename.rb CHANGED
@@ -7,36 +7,31 @@ autoload :FileUtils, 'fileutils'
7
7
  class FileName
8
8
 
9
9
  # The options are following:
10
- # * :start
11
- # Fixnum
12
- # If ID string type is number, the ID starts from the specified number.
13
10
  #
14
- # * :digit
15
- # Fixnum
16
- # When we create additional part of a filename,
17
- # we use a string of ID number with specified digit.
11
+ # [:start (Fixnum)]
12
+ # If ID string type is number, the ID starts from the specified number.
18
13
  #
19
- # * :delimiter
20
- # String
21
- # We use specified string for delimiter between base name and
22
- # additional part. Default is '.' if position is suffix. Otherwise, '_'.
14
+ # [:digit (Fixnum)]
15
+ # When we create additional part of a filename,
16
+ # we use a string of ID number with specified digit.
23
17
  #
24
- # * :type
25
- # :number or :time
26
- # We specify type of additional part: :number or :time.
27
- # Default is :number.
18
+ # [:delimiter (String)]
19
+ # We use specified string for delimiter between base name and
20
+ # additional part. Default is '.' if position is suffix. Otherwise, '_'.
28
21
  #
29
- # * :format
30
- # String or Proc
31
- # We specify format string of additional part or
32
- # proc object to create additional part.
33
- # If type is :time, the format string is used by Time#strftime.
34
- # For :number type, the string is a farst argument of sprintf(format, number).
35
- # Proc object takes an object of Time or Integer for respective types.
22
+ # [:type (:number or :time)]
23
+ # We specify type of additional part: :number or :time.
24
+ # Default is :number.
36
25
  #
37
- # * :position
38
- # :prefix, :suffix, or :middle
39
- # We specify of position of additional part of filename.
26
+ # [:format (String or Proc)]
27
+ # We specify format string of additional part or
28
+ # proc object to create additional part.
29
+ # If type is :time, the format string is used by Time#strftime.
30
+ # For :number type, the string is a farst argument of sprintf(format, number).
31
+ # Proc object takes an object of Time or Integer for respective types.
32
+ #
33
+ # [:position (:prefix, :suffix, or :middle)]
34
+ # We specify of position of additional part of filename.
40
35
  def initialize(basepath, opts = {})
41
36
  @basepath = File.expand_path(basepath)
42
37
  @number = opts[:start] || 0
@@ -130,21 +125,18 @@ class FileName
130
125
  private :add_addition
131
126
 
132
127
  # The options are following:
133
- # * :extension
134
- # String of extension
135
- # If we want to change extension, we set the value of the option.
128
+ # [:extension (String of extension)]
129
+ # If we want to change extension, we set the value of the option.
136
130
  #
137
- # * :add
138
- # :always, :auto, or :prohibit
139
- # We specify if the additional part is used.
140
- # :always Always add.
141
- # :auto If specified file exists, add.
142
- # :prohibit Even if specified file exists, not add.
131
+ # [:add (:always, :auto, or :prohibit)]
132
+ # We specify if the additional part is used.
133
+ # * :always - We always add.
134
+ # * :auto - If the file exists, we add.
135
+ # * :prohibit - Even if the file exists, we do not add.
143
136
  #
144
- # * :directory
145
- # true or false
146
- # If the value is true and the parent directory does not exist,
147
- # we create the directory.
137
+ # [:directory (true or false)]
138
+ # If the value is true and the parent directory does not exist,
139
+ # we create the directory.
148
140
  def create(opts = {})
149
141
  base = get_basepath(opts[:extension])
150
142
  FileUtils.mkdir_p(File.dirname(base)) if opts[:directory]
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: filename
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
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-25 00:00:00 +09:00
13
+ date: 2011-03-26 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: -1320718372192769364
96
+ hash: -1256757600760491313
97
97
  segments:
98
98
  - 0
99
99
  version: "0"