Class: FileName::Manage
- Inherits:
-
Object
- Object
- FileName::Manage
- 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
- #cache_directory(*file) ⇒ Object
- #configuration(key, basepath, *rest) ⇒ Object
- #configuration_directory ⇒ Object
- #configuration_setting(key) ⇒ Object
- #delete_cache(key) ⇒ Object
- #list_cache ⇒ Object
- #list_configuration ⇒ Object
- #load_cache(key) ⇒ Object
- #load_configuration ⇒ Object
- #save_cache(key, filename) ⇒ Object
- #save_configuration_example ⇒ Object
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_directory ⇒ Object
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_cache ⇒ Object
415 416 417 |
# File 'lib/filename.rb', line 415 def list_cache Dir.glob(cache_directory + "/*").map { |s| File.basename(s) }.sort end |
#list_configuration ⇒ Object
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_configuration ⇒ Object
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_example ⇒ Object
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 |