Class: FileName::Manage

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

Constant Summary collapse

CONF_DIRECTORY =
'conf'
CACHE_DIRECTORY =
'cache'
SAMPLE_CONF_NAME =
'process.rb.example'
@@filename_directory =
File.join(ENV['HOME'], '.filename_gem')
@@configuration =
nil

Instance Method Summary collapse

Instance Method Details

#cache_directory(*file) ⇒ Object



291
292
293
# File 'lib/filename.rb', line 291

def cache_directory(*file)
  File.join(@@filename_directory, CACHE_DIRECTORY, *file)
end

#configuration(key, basepath, *rest) ⇒ Object



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

def configuration(key, basepath, *rest)
  if opts = configuration_setting(key)
    if Hash === rest[-1]
      opts = opts.merge(rest.delete_at(-1))
    end
    filename = FileName.new(basepath, *rest, opts)
    filename.configuration_key = key
    return filename
  end
  return nil
end

#configuration_directoryObject



287
288
289
# File 'lib/filename.rb', line 287

def configuration_directory
  File.join(@@filename_directory, CONF_DIRECTORY)
end

#configuration_setting(key) ⇒ Object



307
308
309
310
# File 'lib/filename.rb', line 307

def configuration_setting(key)
  load_configuration unless @@configuration
  @@configuration[key.intern]
end

#delete_cache(key) ⇒ Object



342
343
344
345
346
347
# File 'lib/filename.rb', line 342

def delete_cache(key)
  path = cache_directory(key)
  if File.exist?(path)
    FileUtils.rm(path)
  end
end

#list_cacheObject



349
350
351
# File 'lib/filename.rb', line 349

def list_cache
  Dir.glob(cache_directory + "/*").map { |s| File.basename(s) }.sort
end

#list_configurationObject



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

def list_configuration
  Dir.glob(configuration_directory + "/*.rb").map { |s| File.basename(s).sub(/\.rb$/, '') }.sort
end

#load_cache(key) ⇒ Object



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

def load_cache(key)
  path = cache_directory(key)
  if File.exist?(path)
    return FileName.load_from(path)
  end
  return nil
end

#load_configurationObject



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

def load_configuration
  @@configuration = {}
  # old_safe_level = $SAFE
  # $SAFE = 2
  Dir.glob(configuration_directory + '/*.rb') do |path|
    if key = path.match(/\/([^\/]*)\.rb$/)[1]
      @@configuration[key.intern] = eval(File.read(path))
    end
  end
  # $SAFE = old_safe_level
end

#save_cache(key, filename) ⇒ Object



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

def save_cache(key, filename)
  dir = cache_directory
  FileUtils.mkdir_p(dir)
  filename.save_to(File.join(dir, key))
end

#save_configuration_exampleObject



353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/filename.rb', line 353

def save_configuration_example
  dir = configuration_directory
  FileUtils.mkdir_p(dir)
  open(File.join(dir, SAMPLE_CONF_NAME), 'w') do |f|
    f.print <<SAMPLE
{
  :type => :number,
  :position => :middle,
  :path => :relative,
  :format => lambda { |n| sprintf("%05d_%02d", Process.pid, n) }
}
SAMPLE
  end
end