filename 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -91,7 +91,7 @@ Default options add suffixes and use additional parts of sequential numbers.
91
91
  filename = FileName.new('base.txt', :format => "@%03d@")
92
92
  p filename.create(:add => :always) # => "/path/to/base.txt.@000@"
93
93
 
94
- filename = FileName.new('base.txt', :type => :time, :format => lambda { |n| sprintf("%03d", n * n) })
94
+ filename = FileName.new('base.txt', :type => :number, :format => lambda { |n| sprintf("%03d", n * n) })
95
95
  p filename.create(:add => :always) # => "/path/to/base.txt.100"
96
96
  p filename.create(:add => :always) # => "/path/to/base.txt.121"
97
97
 
@@ -104,6 +104,15 @@ Default options add suffixes and use additional parts of sequential numbers.
104
104
  filename = FileName.new('base.txt', :start => 10, :format => lambda { |t| t.usec.to_s })
105
105
  p filename.create(:add => :always) # For example, returns "/path/to/base.txt.849963"
106
106
 
107
+ ### Use of variable for proc set by :format option
108
+
109
+ require 'filename'
110
+ filename = FileName.new('base.txt', :data => { :a => 3 },
111
+ :format => lambda { |n| s = sprintf("%03d", @a * n); @a += 2; s })
112
+ p filename.create(:add => :always) # => "/path/to/base.txt.000"
113
+ p filename.create(:add => :always) # => "/path/to/base.txt.005"
114
+ p filename.create(:add => :always) # => "/path/to/base.txt.014"
115
+
107
116
  ## How to use command 'filename-create'
108
117
 
109
118
  filename-create with 'new' and basename puts path of file.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
@@ -80,7 +80,7 @@ begin
80
80
  when 'cache'
81
81
  raise OptionParser::InvalidArgument if args.size != 1
82
82
  key = args[0]
83
- if fname = manage.load_cache(key)
83
+ if fname = manage.load_cache(key)
84
84
  $stdout.puts fname.create
85
85
  manage.save_cache(key, fname)
86
86
  exit(0)
@@ -99,7 +99,7 @@ begin
99
99
  exit(0)
100
100
  when 'config'
101
101
  raise OptionParser::InvalidArgument if args.size <= 1
102
- if fname = FileName.configuration(*args)
102
+ if fname = FileName.configuration(*args, options)
103
103
  if create_cache
104
104
  manage.save_cache(create_cache, fname)
105
105
  else
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{filename}
8
- s.version = "0.0.5"
8
+ s.version = "0.0.6"
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"]
@@ -6,6 +6,8 @@ autoload :FileUtils, 'fileutils'
6
6
  #
7
7
  class FileName
8
8
 
9
+ attr_accessor :configuration_key, :format
10
+
9
11
  OPTIONS_CREATE = [:extension, :add, :directory]
10
12
 
11
13
  # The options are following:
@@ -38,13 +40,19 @@ class FileName
38
40
  # [:path]
39
41
  # We sepecify if path created by FileName#create is absolute or relative.
40
42
  # Default is absolute.
41
- #
43
+ #
44
+ # [:data]
45
+ # We specify hash expressing instance variables for evaluation of format proc,
46
+ # which is set by an option :format.
47
+ # If we set { :a => 1, :b => 2 } for :data option,
48
+ # we can use @a and @b in proc object set by :format option.
49
+ #
42
50
  # [:extension]
43
51
  # Default value of the option of FileName#create.
44
52
  #
45
53
  # [:add]
46
54
  # Default value of the option of FileName#create.
47
- #
55
+ #
48
56
  # [:directory]
49
57
  # Default value of the option of FileName#create.
50
58
  def initialize(basepath, *rest)
@@ -72,6 +80,13 @@ class FileName
72
80
  @default_create[key] = val
73
81
  end
74
82
  end
83
+ @configuration_key = nil
84
+ @data = Object.new
85
+ if opts[:data]
86
+ opts[:data].each do |key, val|
87
+ @data.instance_variable_set("@#{key}", val)
88
+ end
89
+ end
75
90
  end
76
91
 
77
92
  def get_basepath(extension = nil)
@@ -94,7 +109,7 @@ class FileName
94
109
  when String
95
110
  t.strftime(@format)
96
111
  when Proc
97
- @format.call(t)
112
+ @data.instance_exec(t, &@format)
98
113
  else
99
114
  t.strftime("%Y%m%d_%H%M%S_") + sprintf("%06d", t.usec)
100
115
  end
@@ -106,7 +121,7 @@ class FileName
106
121
  when String
107
122
  sprintf(@format, number)
108
123
  when Proc
109
- @format.call(number)
124
+ @data.instance_exec(number, &@format)
110
125
  else
111
126
  sprintf("%0#{@digit}d", number)
112
127
  end
@@ -209,8 +224,20 @@ class FileName
209
224
  end
210
225
 
211
226
  # If @format is a Proc object, we can not dump a FileName object.
227
+ # But, even if @format is Proc object, the object created from configuration
228
+ # can be dumped.
212
229
  def dump
213
- Marshal.dump(self)
230
+ if not Proc === @format
231
+ dumped = Marshal.dump(self)
232
+ elsif @configuration_key
233
+ tmp = @format
234
+ @format = nil
235
+ dumped = Marshal.dump(self)
236
+ @format = tmp
237
+ else
238
+ raise "Can not dump."
239
+ end
240
+ dumped
214
241
  end
215
242
 
216
243
  def save_to(path)
@@ -220,7 +247,12 @@ class FileName
220
247
  end
221
248
 
222
249
  def self.load(str)
223
- Marshal.load(str)
250
+ filename = Marshal.load(str)
251
+ if key = filename.configuration_key
252
+ opts = self.manage.configuration_setting(key)
253
+ filename.format = opts[:format]
254
+ end
255
+ filename
224
256
  end
225
257
 
226
258
  def self.load_from(path)
@@ -272,10 +304,19 @@ class FileName
272
304
  # $SAFE = old_safe_level
273
305
  end
274
306
 
275
- def configuration(key, basepath, *rest)
307
+ def configuration_setting(key)
276
308
  load_configuration unless @@configuration
277
- if opts = @@configuration[key.intern]
278
- return FileName.new(basepath, *rest, opts)
309
+ @@configuration[key.intern]
310
+ end
311
+
312
+ def configuration(key, basepath, *rest)
313
+ if opts = configuration_setting(key)
314
+ if Hash === rest[-1]
315
+ opts = opts.merge(rest.delete_at(-1))
316
+ end
317
+ filename = FileName.new(basepath, *rest, opts)
318
+ filename.configuration_key = key
319
+ return filename
279
320
  end
280
321
  return nil
281
322
  end
@@ -11,4 +11,19 @@ describe FileName do
11
11
  FileName.load(s).should be_an_instance_of FileName
12
12
  end
13
13
 
14
+ it "should dump and load" do
15
+ s = FileName.new('abc', :data => { :a => 1 }).dump
16
+ s.should be_an_instance_of String
17
+ filename = FileName.load(s)
18
+ filename.should be_an_instance_of FileName
19
+ data = filename.instance_eval { @data }
20
+ data.instance_variable_get(:@a).should == 1
21
+ end
22
+
23
+ it "should not be able to dump" do
24
+ filename = FileName.new('abc', :format => lambda { |n| sprintf("%02d", n * n) })
25
+ lambda do
26
+ filename.dump
27
+ end.should raise_error
28
+ end
14
29
  end
@@ -34,6 +34,16 @@ describe FileName do
34
34
  FileName.configuration(@conf, 'base.path').should be_an_instance_of(FileName)
35
35
  end
36
36
 
37
+ it "should return key" do
38
+ FileName.configuration(@conf, 'base.path').configuration_key.should == @conf
39
+ end
40
+
41
+ it "should dump and load" do
42
+ s = FileName.configuration(@conf, 'base.path').dump
43
+ s.should be_an_instance_of String
44
+ FileName.load(s).should be_an_instance_of FileName
45
+ end
46
+
37
47
  it "should return nil" do
38
48
  FileName.configuration(:not_exist, 'base.path').should be_nil
39
49
  end
@@ -53,6 +53,12 @@ describe FileName do
53
53
  filename.create.should match(/\.009$/)
54
54
  end
55
55
 
56
+ it "should set variables for evaluation of format proc" do
57
+ filename = FileName.new(__FILE__, :start => 3, :data => { :a => 'hello', :b => 'world' },
58
+ :format => lambda { |n| sprintf("%03d_#{@a}_#{@b}", n * n) })
59
+ filename.create.should match(/\.009_hello_world$/)
60
+ end
61
+
56
62
  it "should raise error" do
57
63
  filename = FileName.new(__FILE__, :format => "")
58
64
  lambda do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: filename
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.5
5
+ version: 0.0.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Takayuki YAMAGUCHI
@@ -94,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
- hash: 3878045730836072065
97
+ hash: -1246538817859403112
98
98
  segments:
99
99
  - 0
100
100
  version: "0"