Class: FileName

Inherits:
Object
  • Object
show all
Defined in:
lib/filename.rb

Overview

The class to create filename that is not duplicated. We select type of additional part of filename: number or time.

Defined Under Namespace

Classes: Manage

Constant Summary collapse

OPTIONS_CREATE =
[:extension, :add, :directory, :file]
@@manage =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basepath, *rest) ⇒ FileName

The options are following:

:start (Fixnum)

If ID string type is number, the ID starts from the specified number.

:digit (Fixnum)

When we create additional part of a filename, we use a string of ID number with specified digit.

:delimiter (String)

We use specified string for delimiter between base name and additional part. Default is ‘.’ if position is suffix. Otherwise, ‘_’.

:type (:number or :time)

We specify type of additional part: :number or :time. Default is :number.

:format (String or Proc)

We specify format string of additional part or proc object to create additional part. If type is :time, the format string is used by Time#strftime. For :number type, the string is a farst argument of sprintf(format, number). Proc object takes an object of Time or Integer for respective types.

:position (:prefix, :suffix, or :middle)

We specify of position of additional part of filename.

:path

We sepecify if path created by FileName#create is absolute or relative. Default is absolute.

:data

We specify hash expressing instance variables for evaluation of format proc, which is set by an option :format. If we set { :a => 1, :b => 2 } for :data option, we can use @a and @b in proc object set by :format option.

:extension

Default value of the option of FileName#create.

:add

Default value of the option of FileName#create.

:directory

Default value of the option of FileName#create.

:file

Default value of the option of FileName#create.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/filename.rb', line 61

def initialize(basepath, *rest)
  if Hash === rest[-1]
    opts = rest.delete_at(-1)
  else
    opts = {}
  end
  path = File.join(basepath, *rest)
  if opts[:path] == :relative
    @basepath = path
  else
    @basepath = File.expand_path(path)
  end
  @number = opts[:start] || 0
  @digit = opts[:digit] || 2
  if @digit < 1
    raise ArgumentError, "Number of digit must be positive."
  end
  @type = opts[:type] || :number
  @position = opts[:position] || :suffix
  @delimiter = opts[:delimiter] || (@position == :suffix ? '.' : '_')
  @format = opts[:format]
  @last_addition = nil
  @default_create = {}
  opts.each do |key, val|
    if OPTIONS_CREATE.include?(key)
      @default_create[key] = val
    end
  end
  @configuration_key = nil
  @data = Object.new
  if opts[:data]
    opts[:data].each do |key, val|
      @data.instance_variable_set("@#{key}", val)
    end
  end
end

Instance Attribute Details

#configuration_keyObject

Returns the value of attribute configuration_key.



9
10
11
# File 'lib/filename.rb', line 9

def configuration_key
  @configuration_key
end

#formatObject

Returns the value of attribute format.



9
10
11
# File 'lib/filename.rb', line 9

def format
  @format
end

Class Method Details

.configuration(*args) ⇒ Object



310
311
312
# File 'lib/filename.rb', line 310

def self.configuration(*args)
  self.manage.configuration(*args)
end

.create(basepath, *rest) ⇒ Object

Executing FileName.new and FileName.create, we get new filename. The same options of FileName.new are available.



299
300
301
# File 'lib/filename.rb', line 299

def self.create(basepath, *rest)
  self.new(basepath, *rest).create
end

.load(str) ⇒ Object



284
285
286
287
288
289
290
291
# File 'lib/filename.rb', line 284

def self.load(str)
  filename = Marshal.load(str)
  if key = filename.configuration_key
    opts = self.manage.configuration_setting(key)
    filename.format = opts[:format]
  end
  filename
end

.load_from(path) ⇒ Object



293
294
295
# File 'lib/filename.rb', line 293

def self.load_from(path)
  self.load(File.read(path))
end

.manageObject



305
306
307
308
# File 'lib/filename.rb', line 305

def self.manage
  @@manage = FileName::Manage.new unless @@manage
  @@manage
end

Instance Method Details

#create(opts = {}) ⇒ Object

The options are following:

:extension (String of extension)

If we want to change extension, we set the value of the option.

:add (:always, :auto, or :prohibit)

We specify if the additional part is used.

  • :always - We always add.

  • :auto - If the file exists, we add.

  • :prohibit - Even if the file exists, we do not add.

:directory (:self, :parent, or nil)

If the value is :self, we make directory of created filename. If the value is :parent, we make parent directory of created filename. If the value is nil, we do nothing.

:file

If the value is :overwrite, we create a new empty file. If the value is :write and the file does not exist, we create an empty file. If the value is nil, we do nothing.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/filename.rb', line 240

def create(opts = {})
  base = get_basepath(get_option_create(opts, :extension))
  opt_add = get_option_create(opts, :add)
  if addition = get_addition(opt_add, base)
    path = add_addition(base, addition)
    while File.exist?(path)
      if addition = get_addition(opt_add, base)
        path = add_addition(base, addition)
      else
        raise "Can not create new filename."
      end
    end
    path
  else
    path = base
  end
  create_directory(path, get_option_create(opts, :directory))
  write_file(path, get_option_create(opts, :file))
  path
end

#dumpObject

If @format is a Proc object, we can not dump a FileName object. But, even if @format is Proc object, the object created from configuration can be dumped.



264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/filename.rb', line 264

def dump
  if not Proc === @format
    dumped = Marshal.dump(self)
  elsif @configuration_key
    tmp = @format
    @format = nil
    dumped = Marshal.dump(self)
    @format = tmp
  else
    raise "Can not dump."
  end
  dumped
end

#save_to(path) ⇒ Object



278
279
280
281
282
# File 'lib/filename.rb', line 278

def save_to(path)
  open(path, 'w') do |f|
    f.print dump
  end
end