Class: FileName

Inherits:
Object
  • Object
show all
Defined in:
lib/filename.rb,
lib/filename/version.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]
VERSION =
"0.1.2"
@@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.

:filter

We specify filters :before and :after for basename without suffix, which is got by File.basename(path, “.*”)

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



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
97
98
99
100
101
102
# File 'lib/filename.rb', line 65

def initialize(basepath, *rest)
  if Hash === rest[-1]
    opts = rest.delete_at(-1)
  else
    opts = {}
  end
  path = File.join(basepath, *rest)
  @relative_path_p = (opts[:path] == :relative)
  if @relative_path_p
    @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
  @filter = opts[:filter] || {}
end

Instance Attribute Details

#configuration_keyObject

Returns the value of attribute configuration_key.



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

def configuration_key
  @configuration_key
end

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

Class Method Details

.configuration(*args) ⇒ Object



341
342
343
# File 'lib/filename.rb', line 341

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.



330
331
332
# File 'lib/filename.rb', line 330

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

.load(str) ⇒ Object



315
316
317
318
319
320
321
322
# File 'lib/filename.rb', line 315

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



324
325
326
# File 'lib/filename.rb', line 324

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

.manageObject



336
337
338
339
# File 'lib/filename.rb', line 336

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. Note that if we specify “txt” as :extension option, generated filesame is the format “SOME_STRING.txt”; that is, this method adds “.” + (specified extension).

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



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/filename.rb', line 264

def create(opts = {})
  basepath = @basepath
  if @filter[:before]
    basepath = filter_exec(@filter[:before], basepath)
  end
  base = get_basepath(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
  if @filter[:after]
    path = filter_exec(@filter[:after], path)
  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.



295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/filename.rb', line 295

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

#relative_path?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/filename.rb', line 119

def relative_path?
  @relative_path_p
end

#save_to(path) ⇒ Object



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

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