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



357
358
359
# File 'lib/filename.rb', line 357

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

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



378
379
380
381
382
383
384
385
386
387
388
# File 'lib/filename.rb', line 378

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



353
354
355
# File 'lib/filename.rb', line 353

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

#configuration_setting(key) ⇒ Object



373
374
375
376
# File 'lib/filename.rb', line 373

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

#delete_cache(key) ⇒ Object



408
409
410
411
412
413
# File 'lib/filename.rb', line 408

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

#list_cacheObject



415
416
417
# File 'lib/filename.rb', line 415

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

#list_configurationObject



390
391
392
# File 'lib/filename.rb', line 390

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

#load_cache(key) ⇒ Object



400
401
402
403
404
405
406
# File 'lib/filename.rb', line 400

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

#load_configurationObject



361
362
363
364
365
366
367
368
369
370
371
# File 'lib/filename.rb', line 361

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



394
395
396
397
398
# File 'lib/filename.rb', line 394

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

#save_configuration_exampleObject



419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/filename.rb', line 419

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