Class: LX::File::Class
- Inherits:
-
Object
- Object
- LX::File::Class
- Defined in:
- lib/lx.rb
Instance Method Summary collapse
-
#atomic_write(path_final, content) ⇒ Object
Atomically writes content to the given path.
-
#initialize(clss) ⇒ Class
constructor
A new instance of Class.
-
#temp_path(opts = {}) ⇒ Object
Creates a temporary path.
Constructor Details
#initialize(clss) ⇒ Class
Returns a new instance of Class.
455 456 |
# File 'lib/lx.rb', line 455 def initialize(clss) end |
Instance Method Details
#atomic_write(path_final, content) ⇒ Object
Atomically writes content to the given path. Does so by creating a temp file, writing to it, then renaming the temp file to the final destination.
460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/lx.rb', line 460 def atomic_write(path_final, content) begin path_tmp = path_final + '.' + rand.to_s.sub(/\A.*\./mu, '') File.write path_tmp, content File.rename path_tmp, path_final ensure if File.exist?(path_tmp) File.delete path_tmp end end end |
#temp_path(opts = {}) ⇒ Object
Creates a temporary path. Does not create a file. If, after the ‘do` block, a file exists at that path, that file is deleted.
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/lx.rb', line 474 def temp_path(opts = {}) opts = {'delete'=>true}.merge(opts) # root if opts['root'] root = opts['root'].sub(/\/*\z/mu, '/') else root = './' end # full path path = root + LX.randstr # add extension if opts['ext'] ext = opts['ext'] ext = ext.sub(/\A\.*/mu, '.') path += ext end if block_given? begin yield path ensure if opts['delete'] and File.exist?(path) File.delete path end end else return path end end |