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]
@@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.



58
59
60
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
# File 'lib/filename.rb', line 58

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
  @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



275
276
277
# File 'lib/filename.rb', line 275

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.



264
265
266
# File 'lib/filename.rb', line 264

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

.load(str) ⇒ Object



249
250
251
252
253
254
255
256
# File 'lib/filename.rb', line 249

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



258
259
260
# File 'lib/filename.rb', line 258

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

.manageObject



270
271
272
273
# File 'lib/filename.rb', line 270

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.



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/filename.rb', line 206

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))
  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.



229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/filename.rb', line 229

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



243
244
245
246
247
# File 'lib/filename.rb', line 243

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